c++opencvhsvbgr

OpenCV - cvtColor does not change colorspace, it changes colors


I am trying to make an object tracker using OpenCV 3.1.0 and C++ following this Python example: http://docs.opencv.org/3.1.0/df/d9d/tutorial_py_colorspaces.html#gsc.tab=0.

I have some problems with cvtColor() function, because it changes the colors of my images and not its colorspace. I have this code:

Mat original_image;
original_image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // The image is passed as arg
if (!original_image.data)
{
    printf("Problem!\n");
    return -1;
}

// From BGR to HSV
Mat hsv_image(original_image.rows, original_image.cols, original_image.type());
cvtColor(original_image, hsv_image, CV_BGR2HSV);
imwrite("hsv_image.png", hsv_image);

original_image is a CV_8UC3, compatible with cvtColor() and should be originally in BGR colorspace.

I made the test image below with GIMP:

Test image

And I get this image as result:

HSV image - result

I decided to try the conversion from BGR to RGB, changing BGR2HSV to BGR2RGB, and with the same test image, I get this result

RGB image - result

Here, it's more clear that the channels of the image are changed directly...

Has anybody any idea about what's happening here?


Solution

  • Function imwrite doesn't care what color space mat has and this information isn't stored. According to documentation it's BGR order.

    So before saving image you should be sure it is BGR.

    If you really want to save image as HSV use file storages