converting color image to gray scale image - c++

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.

Related

Applying adaptive threshold to a grayscale image

I have a png image which is in grayscale 'test.png'. I need apply adaptive threshold to this image. I am using OpenCV.
image = cv2.imread('test_big.png')
im = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
I am not able to apply adaptive threshold since the image is not in grayscale.
So I tried to read the image as grayscale:
image = cv2.imread('test_big.png',1)
Now I can apply adaptive threshold but the output will be a blue and red image instead of black and white. Can anyone help?
The fault lies in the second code snippet:
image = cv2.imread('test_big.png',1)
Although you have said that test_big.png is a grayscale image, you have declared it as a color image (RGB) with three channels.
Hence you have to change the code to
image = cv2.imread('test_big.png', 0)
0 -> grayscale image
1 -> color image
You can also try:
cv2.imread('test_big.png', cv2.IMREAD_GRAYSCALE)
The bottom line is: although the image being read is a grayscale image, the system will not recognize it until it is explicitly specified. In your case, your image was a grayscale image, but since you declared it as a color image it considered the image to have three channels (RGB) and hence the subsequent adaptive threshold function did not execute.

Threshold function does not work OpenCV 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.

In open cv, how can i convert gray scale image back in to RGB image(color)

In open cv to remove background, using current frame and former frame, i applied absdiff function and created a difference image in gray scale. However, i would like to covert the gray scale image back in to RGB with actual color of the image, but i have no idea how to operate this back in.
I'm using C++.
Could any one knowledgeable of open cv help me?
You cannot covert the gray scale image back into RGB with actual color of the image again as coverting RGB to gray scale is a data-losing process.
Instead, as #MatsPetersson suggested, you can take the use of the grayscale image to create a mask, e.g. by further applying a thresholding process. Then you can easily get the ROI color image by:
cv::Mat dst;
src.copyTo(dst, mask);

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.

How do I add a watermark to an image with OpenCV?

Is there any way to add a watermark image to another image using the OpenCV library?
I am working with OpenCV and C++.
Take a look at this tutorial.
Here is some (non-tested) code:
Mat watermark = imread("watermarkimage.png")
Mat img = imread("yourimage.jpg");
//assuming the watermark has same number of channel and size as your_image
Mat watermarked = (0.8 * img) + (0.2 * watermark)
You can add text watermark on the image using cv::putText function of opencv.
Simple: blend the original image (90%) and the copy with the watermark text/graphics added (10%).