Search code examples
opencvrgbhsv

OpenCV - RGB to HSV and back to RGB?


I have an image that i want to convert from RGB to HSV, and then back to RGB.

This is how i do it:

Mat frame1, frame2, frame3;
cvtColor(frame1, frame2, CV_RGB2HSV);
cvtColor(frame2, frame3, CV_HSV2BGR);
namedWindow("origin", CV_WINDOW_AUTOSIZE);
namedWindow("rgb2hsv", CV_WINDOW_AUTOSIZE);
namedWindow("hsv2rgb", CV_WINDOW_AUTOSIZE);
imshow("origin", frame1);
imshow("rgb2hsv", frame2);
imshow("hsv2rgb", frame3);

I believe i get the correct result when i convert from RGB to HSV. However, when i convert that converted image back to RGB, it gives me an incorrect result.

Any idea why? Thanks!


Solution

  • cvtColor(frame1, frame2, CV_RGB2HSV);
    cvtColor(frame2, frame3, CV_HSV2BGR);
    

    should be (notice it is BGR2HSV instead of RGB2HSV)

    cvtColor(frame1, frame2, CV_BGR2HSV);
    cvtColor(frame2, frame3, CV_HSV2BGR);