Threshold function does not work OpenCV C++ - c++

I just want to do convert a gray image to a binary image. But threshold function gives me a totaly black image as a binary image. I want to get dark gray object.
What is wrong here?
Code:
Mat theFrame = imread("C:\\asdsss.png"); // opencv
Mat gray,binary;
cvtColor(theFrame, gray, CV_BGR2GRAY);
threshold(gray, binary, 150, 255, THRESH_BINARY);
imwrite("result.jpg",binary);
İnput image:

The code works perfectly fine. I ran you exact code on the image provided. There is no issue with it.
I got the following output by running your code. The only problem I can think of could be loading of image. Try to see your image using cv::imshow after loading it. Also try to convert your image into jpg format and then try loading it again. You can also try compiling and running the opencv thresholding sample.

Related

goodFeaturesToTrack() works only on greyscale?

I am new to working in openCV. I was trying to detect features in an image using goodFeaturesToTrack() in c++ in a colored image but it gave me the following error:
" OpenCV Error: Assertion failed (src.type() == CV_8UC1 || src.type() == CV_32FC1) in cv::cornerEigenValsVecs, file D:\cs436\opencv-3.2.0\modules\imgproc\src\corner.cpp, line 287 "
Then I tried it on a greyscale image and it found the features. Can anyone please tell if we can use the function on a colored image? and what does the error mean?
From the documentation of goodFeaturesToTrack(), the image parameters takes in a 8-bit or 32-bit single channel image. single channel implies grayscale.
So yes, the function only works for grayscale images. The error message also indicates that by saying that the src.type() must have 1 channel as indicated by CV_8UC1 and CV_32FC1
That right the function is defined for grayscale images only. To apply colored images you have to convert the rgb image to a grayscaled image by:
cv::cvtColor(rgbImage, grayImage, CV_BGR2GRAY);

converting color image to gray scale image

I am facing a problem with writing and reading a gray scale image in open cv using c++. This is my code to read a color image as gray scale image and to write it in a file:
cv::Mat source = cv::imread("c:\\users\\selva\\desktop\\newimage.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cv::imwrite("c:\\users\\selva\\desktop\\grayscal.jpg",source);
Then i tried to read that image in a new project,
cv::Mat gray = cv::imread("c:\\users\\selva\\desktop\\grayscal.jpg",CV_LOAD_IMAGE_UNCHANGED);
But when i execute this code,
std::cout<<gray.channels(); The output in console is 3.
I am using opencv 2.4.7, i tried with .png format, but that does not help.
console value for std::cout<<source.channels(); is 1.
If my newimage.jpg is a gray scale image and i am reading as,
cv::Mat source = cv::imread("c:\\users\\selva\\desktop\\newimage.jpg",0);
the problem is same.
I am sure that i am loading correct image.
Please anyone answer why it happens. Thanks in advance.

OpenCV - JPEG compression on arm/android phone doesn't result the right image

I have written an android app which uses OpenCV to manipulate with images. I'm using the below code to write a cv::Mat object to JPG file.
cv::imwrite("<sd card path>/img.jpg", <some mat object>);
I do see the image being saved on my sd card, however, the colors are not right. It has some bluish color all over the image.
Does anyone know what I'm missing here?
The above comment by Haris resolved this issue. I modified the code to change the color space as below:
cv::Mat mat;
// Initialize mat
cv::cvtColor(mat, mat, CV_BGR2RGB);
cv::imwrite("<sd card path>/img.jpg", mat);
I was able to see the right image being saved with this.

OpenCV WarpPerspective issue

I am currently trying to implement a basic image stitching C++ (OpenCV) code in Eclipse. The feature detection part shows great results for SURF Features. However, when I attempt to warp the 2 images together, I get only half the image as the output. I have tried to find a solution everywhere but to no avail. I even tried to offset the homography matrix , like in this answer OpenCV warpperspective . Nothing has helped so far.
I'll attach the output images in the comments since I don't have enough reputation points.
For feature detection and homography, I used the exact code from here
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
And then I added the following piece of code after the given code,
Mat result;
warpPerspective(img_object,result,H, Size(2*img_object.cols,img_object.rows));
Mat half(result,Rect(0,0,img_scene.cols,img_scene.rows));
img_scene.copyTo(half);
imshow( "Warped Image", result);
I'm quite new at this and just trying to put the pieces together. So I apologize if there's some basic error.
If you're only trying to put the pieces together, you cold try the built in OpenCV image stitcher class: http://docs.opencv.org/modules/stitching/doc/high_level.html#stitcher
I found a related question here Stitching 2 images in opencv and implemented the additional code given. It worked!
For reference, the edited code I wrote was
Mat result;
warpPerspective(img_scene, result, H, Size(img_scene.cols*2, img_scene.rows*2), INTER_CUBIC);
Mat final(Size(img_scene.cols + img_object.cols, img_scene.rows*2),CV_8UC3);
Mat roi1(final, Rect(0, 0, img_object.cols, img_object.rows));
Mat roi2(final, Rect(0, 0, result.cols, result.rows));
result.copyTo(roi2);
img_object.copyTo(roi1);

cv::findContours is modifying source image OpenCV 2.3

From OpenCV documentation, source image in cv::findContours is aquired as const, but something strange is going on with my application. I'm using cv::inRange function to get thresholded image over specific color, and after that, using cv::moments, I can get the center of white pixels in thresholded image and this is working ok.
In addition, I would like to implement the code for finding biggest contour and locating central moment in that contour. After adding just cv::findContours in the code, I spotted strange behavior in the output and after that I wanted to check what is going on with source image using this code:
cv::Mat contourImage;
threshedImage.copyTo(contourImage); // threshedImage is the output from inRange
cv::findContours(threshedImage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cv::Point(0,0));
cv::Mat temp;
cv::absdiff(threshedImage,contourOutput, temp);
cv::namedWindow("absdiff");
cv::imshow("absdiff",temp);
After this, output is showing that there is a difference between threshedImage and contourImage. How this is possible? Does anyone have similar results with cv::findContours?
Wrong! The docs clear states that:
Source image is modified by this function.
So if you need the original image intact, make a copy of this image and pass the copy to cv::findContours().