OpenCV load CIE L*a*b* image - c++

I'm trying to load a CIE Lab* image using openCV in C++.
Online I can find only examples that load an RGB image and convert it into a LAB image but I already have the LAB image so how can I load it and than access to the values of L, a and b?
The only way I find is to load the LAB image considering it an RGB image and convert it into a Lab image using:
cvtColor(source, destination, CV_BGR2Lab);
But I think this is not a good way to solve the problem because if I do this, the converted image looks very different from the original.
With a test image and the following code:
originalImage = imread(originalImagePath, CV_LOAD_IMAGE_UNCHANGED);
cout << originalImage.type() << endl;
Mat originalImageSplitted[3];
split(originalImage, originalImageSplitted);
cout << originalImageSplitted[0] << endl;
cout << originalImageSplitted[1] << endl;
cout << originalImageSplitted[2] << endl;
I get the result:
0
[]
[]
[]

Not really an answer, but too much for a comment.
You can make a Lab colourspace TIF file for testing like this with ImageMagick from the Terminal in Linux, macOS or Windows:
convert -depth 8 xc:black xc:white xc:red xc:lime xc:blue +append -colorspace Lab result.tif
That will look like this if I scale it up as it is currently only 5 pixels wide and 1 pixel tall:
You can then dump the pixels to see their values and hopefully work out what OpenCV is doing:
convert result.tif txt:
Sample Output
# ImageMagick pixel enumeration: 5,1,65535,cielab
0,0: (0,-0.5,-0.5) #000000 cielab(0%,-0.000762951%,-0.000762951%) <--- black pixel
1,0: (65535,-0.5,-0.5) #FF0000 cielab(100%,-0.000762951%,-0.000762951%) <--- white pixel
2,0: (34952,20559.5,17218.5) #885043 cielab(53.3333%,31.3718%,26.2737%) <--- red pixel
3,0: (57568,-22102.5,21330.5) #E00053 cielab(87.8431%,-33.7263%,32.5483%) <--- green pixel
4,0: (21074,20302.5,-27756.5) #524F00 cielab(32.1569%,30.9796%,-42.3537%) <--- blue pixel
Looking at the red pixel, you get:
L=53.33%
a=31.37% of 256, i.e. 80.3
b=26.27% of 256, i.e. 67.2

To keep the image unchanged you should read it into a Mat image similarly:
Mat image;
image = imread(<path_of_image>, CV_LOAD_IMAGE_UNCHANGED)
In this case the second argument should preserve your image color channels as is.

With #DanMašek using #MarkSetchell image we solved the problem.
Using imread function the image is automatically converted into an RGB image so it's needed to convert it into a Lab image again.
Another problem is releated to 8bit images. The resulted image has modified values of L,a and b following this rule:
L * 255/100
a as a+128
b as b+128
So I solved doing the following:
originalImage = imread(originalImagePath, CV_LOAD_IMAGE_UNCHANGED);
Mat originalImageLab;
cvtColor(originalImage, originalImageLab, COLOR_RGB2Lab);
Mat originalImageSplitted[3];
split(originalImageLab, originalImageSplitted);
Thank you all!

Related

How to increase the saturation values of an image using HSV (in OpenCV using C++)?

I was looking for a way to increase the saturation of some of my images using code and found the strategy of splitting a material with HSV and then increasing the S channel by a factor. However, I ran into some issues where the split channels were still in BGR (I think) because the output was just a greener tinted version of the original.
//Save original image to material
Mat orgImg = imread("sunset.jpg");
//Resize the image to be smaller
resize(orgImg, orgImg, Size(500, 500));
//Display the original image for comparison
imshow("Original Image", orgImg);
Mat g = Mat::zeros(Size(orgImg.cols, orgImg.rows), CV_8UC1);
Mat convertedHSV;
orgImg.convertTo(convertedHSV, COLOR_BGR2HSV);
Mat saturatedImg;
Mat HSVChannels[3];
split(convertedHSV, HSVChannels);
imshow("H", HSVChannels[0]);
imshow("S", HSVChannels[1]);
imshow("V", HSVChannels[2]);
HSVChannels[1] *= saturation;
merge(HSVChannels, 3, saturatedImg);
//Saturate the original image and save it to a new material.
//Display the new, saturated image.
imshow("Saturated", saturatedImg);
waitKey(0);
return 0;
This is my code and nothing I do makes it actually edit the saturation, all the outputs are just green tinted photos.
Note saturation is a public double that is usually set to around 1.5 or whatever you want.
Do not use cv::convertTo() here. It changes the bitdepth (and representation, int vs. float) of the image, not what you are trying to achieve, the color space.
Using it like that does not throw a warning or error though, because both type indicators (CV_8U, ...) and the colorspace indicators (COLOR_BGR2HSV,...) can be resolved as integers, one is a #define, the other a old style enum.
Following the example here, it is possible to do with cv::cvtColor(). Don't forget to revert back before showing the image, imshow() and imwrite() both expect an BGR format.
// Convert image from BGR -> HSV:
// orgImg.convertTo(convertedHSV, COLOR_BGR2HSV); // <- this wrong, do not use
cvtColor(orgImg, convertedHSV, COLOR_BGR2HSV); // <- this does the trick instead
// to the split, multiplication, merge
// [...]
// Convert image back HSV -> BGR:
cvtColor(saturatedImg, saturatedImg, COLOR_HSV2BGR);
//Display the new, saturated image.
imshow("Saturated", saturatedImg);
Note that oCV does not care about color representation when working with a 3 channel Mat: Could be RGB, HSV or anything else. Only for displaying (or saving to an image format) does the given color space matter.

Opencv setting a color pixel ends up blurring to neighboring pixels

I'm trying to set the pixel value of a CV_8UC3 type image in OpenCV. I know how to do this with a single channel image CV_8UC1, but when doing the same thing with a three channel image the pixel value ends up blurring to the neighboring pixels even though they were not changed.
This is how I do it with a single channel image:
Mat tmp(5, 5, CV_8UC1, Scalar(0));
uchar *tmp_p = tmp.ptr();
tmp_p[0] = (uchar)255;
imwrite("tmp.jpg", tmp);
The resulting image is as you would expect, just the very first pixel has been changed from black to white, while all of the other pixels were left alone.
The following is how I'd expect to do it with a three channel image:
Mat tmp(5, 5, CV_8UC3, Scalar(0));
uchar *tmp_p = tmp.ptr();
tmp_p[0] = (uchar)255;
imwrite("tmp.jpg", tmp);
The expected result from this process should yield a single blue pixel in the top left corner of the image. However the neighboring 3 pixels have seemed to "blur" with the pixel value I set.
If anyone knows why this blurring of pixels is happening I'd very much appreciate any help I can get.
It turns out the problem was in the image file format. I was outputting the image as .jpg which was modifying pixels. When changing the file type to .png this problem was corrected.
Here is my code now with prints to the console of the original image before outputting to a file as well as after re-reading in the file that was output.
// create a small black image and
// change the color of the first pixel to blue
Mat tmp(5, 5, CV_8UC3, Scalar(0));
uchar *tmp_p = tmp.ptr();
tmp_p[0] = (uchar)255;
// output values to the console
cout << tmp << endl << endl;
// write out image to a file then re-read back in
#define CORRECTMETHOD // comment out this line to see what was wrong before
#ifdef CORRECTMETHOD
imwrite("tmp.png", tmp);
tmp = imread("tmp.png");
#else
imwrite("tmp.jpg", tmp);
tmp = imread("tmp.jpg");
#endif
// print out values to console (these values
// should match the original image created above)
cout << tmp << endl;

OpenCV image conversion goes wrong

I have an algorithm that does some stuff. Among them, there is a conversion that works fine if I'm working on a CV_8UC3 image but goes wrong if the file type is C_16UC3.
This is some code:
//new image is created
Mat3w img(100,100,Vec3w(1000,0,0));
//Image Conversion - ERROR!
cv::Mat inputSource;
//saving the image here will work
img.convertTo(inputSource, CV_64FC3);
//saving the image here will not work -> black image
The problem is that the CV_16UC3 image's processing result is an image of the right dimensions but fully black.
The problem is in the conversion because saving the image right before will give a legit one while saving it right after will give an almost completely white one.
EDIT:
I made some changes: cut off some useless code and added the inputSource declaration.
Now, while I was trying stuff, I arrived at the conclusion that either I haven't understood the CV Types, or something strange is happening.
I always thought that the number in the type was indicating the number of bits per channel. So, in my head, CV_16UC3 is a 3 channel with 16bits per channel. That idea is strengthened by the fact that the image I save during as tests (before the img.convertTo) actually had matching bits per channel number. The strange thing, is that the saved inputSource (type CV_64FC3) is an 8bpc image.
What's am I missing?
You get confused with the way imwrite and imread work in OpenCV. From the OpenCV documentation
imwrite
The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see imread() for the list of extensions). Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function. If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.
imread
The function imread loads an image from the specified file and returns it. Possible flags are:
IMREAD_UNCHANGED : If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE : If set, always convert image to the single channel grayscale image.
IMREAD_COLOR : If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH : If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR : If set, the image is read in any possible color format.
So for your case, CV_16U are saved without conversion, while CV_64F is converted and saved as CV_8U. If you want to store double data, you should use FileStorage.
You should also take care to use imread the image with the appropriate flag.
This example should clarify:
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
// Create a 16-bit 3 channel image
Mat3w img16UC3(100, 200, Vec3w(1000, 0, 0));
img16UC3(Rect(0, 0, 20, 50)) = Vec3w(0, 2000, 0);
// Convert to 64-bit (double) 3 channel image
Mat3d img64FC3;
img16UC3.convertTo(img64FC3, CV_64FC3);
// Save to disk
imwrite("16UC3.png", img16UC3); // No conversion
imwrite("64FC3.png", img64FC3); // Converted to CV_8UC3
FileStorage fout("64FC3.yml", FileStorage::WRITE);
fout << "img" << img64FC3; // No conversion
fout.release();
Mat img_maybe16UC3_a = imread("16UC3.png" /*, IMREAD_COLOR*/); // Will be CV_8UC3
Mat img_maybe16UC3_b = imread("16UC3.png", IMREAD_ANYDEPTH); // Will be CV_16UC1
Mat img_maybe16UC3_c = imread("16UC3.png", IMREAD_UNCHANGED); // Will be CV_16UC3
Mat img_maybe64FC3_a = imread("64FC3.png" /*, IMREAD_COLOR*/); // Will be CV_8UC3
Mat img_maybe64FC3_b = imread("64FC3.png", IMREAD_ANYDEPTH); // Will be CV_8UC1
Mat img_maybe64FC3_c = imread("64FC3.png", IMREAD_UNCHANGED); // Will be CV_8UC3
Mat img_mustbe64FC3;
FileStorage fin("64FC3.yml", FileStorage::READ);
fin["img"] >> img_mustbe64FC3; // Will be CV_64FC3
fin.release();
return 0;
}

Open CV image saving in smaller size without compressions

The question i have is the following:
I have a camera( with resolution Resolution : 640 x 480 px) and I get an image from that camera (I get an 8 bit/ pixel grayscale image) after the image acquisition I save the image in a bmp format. My code is the followig :
Mat img2(640,480,CV_8UC1,0);
cap.read(img2);
bool succes = imwrite("D:\\TestImage3.bmp",img2);
if(!succes){
cout << "Failed to save the image";
return -1;
}
namedWindow("myWindow",CV_WINDOW_AUTOSIZE);
imshow("myWindow",img2);
The saved image is very large almost 1 MB and i want a smaller image without losing any information (without compresing the image)???
The second question on this topic is:
even if the image is gray some times I still get some rgb noise, its like I would have set a 3 channel setting instead of 1 channel setting for my image
If anyone knows the answer please let me know, I would be very grateful
Thanks for your time!
You can save your image as PNG which is an lossless image compression format.
bool succes = imwrite("D:\\TestImage3.png",img2);
With the cv::imwrite function you can pass additional parameters depending on the image format.
PNG is a lossless image format but you can still chose the level of compression for example :
Mat img2;
cap.read(img2);
cvtColor(img2, img2, CV_BGR2GRAY); // Convert to single channel
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
bool succes = imwrite("D:\\TestImage3.bmp", img2, compression_params);
if(!succes)
{
cout << "Failed to save the image"; return -1;
}
imshow("myWindow",img2);
waitKey(0);
Just use the default constructor for Mat with no params.
Mat img2;
cap.read(img2);
cvtColor(img2, img2, CV_BGR2GRAY); // Convert to single channel
bool succes = imwrite("D:\\TestImage3.bmp", img2);
if(!succes)
{
cout << "Failed to save the image"; return -1;
}
imshow("myWindow",img2);
waitKey(0);
Also, bmp is known for its large uncompressed size. Use .png instead.

How can i change a pixel value from a grayscaled image using Opencv 2.3?

When i read a grayscaled image using for example in Opencv 2.3:
Mat src = imread("44.png" ,0);
How can i access the pixel value of it?
I know if its RGB i can use:
std::cout << src.at<cv::Vec3b>(i,j)[0].
Thanks in advance.
Since a grayscale image contains only one component instead of 3, the resulting matrix/image is of type CV_8UC1 instead of CV_8UC3. And this in turn means, that individual pixels are not 3-vectors of bytes (cv::Vec3b) but just single bytes (unsigned char or OpenCV's uchar). So you can just use:
src.at<unsigned char>(i, j)