I'm trying to convert an bgr mat to an hsv mat for some detection, but the hsv image keeps coming out blocky. Here is my code in c++:
int main() {
const int device = 1;
VideoCapture capture(device);
Mat input;
int key;
if(!capture.isOpened()) {
printf("No video recording device under device number %i found. Aborting program...\n", device);
return -1;
}
namedWindow("Isolation Test", CV_WINDOW_AUTOSIZE);
while(1) {
capture >> input;
cvtColor(input, input, CV_BGR2HSV);
imshow("Isolation Test", input);
key = static_cast<int>(waitKey(10));
if(key == 27)
break;
}
destroyWindow("Isolation Test");
return 0;
}
Here is a snapshot of what the output looks like. the input does not look blocky when I comment out the cvtColor. What is the problem and what should I do to fix it?
I suggested an explanation in the comments part, but decided to actually verify my assumption and explain a little bit about the
HSVcolor space.There is no problem in the code nor in OpenCV's
cvtColor. the "blocky" artifacts exist in theRGBimage, but are not noticeable. All of the JPEG family compression algorithms produce these artifacts. The reason we usually don't see them is that the algorithms "exploit" weaknesses in our visual system and compress more stuff that we are not very sensitive to.I converted the image back to
RGBusing OpenCVscvtColor` and the artifacts magically disappeared (images are below).The
HSVcolor space in particular has several characteristics that exaggerate these artifacts. The important of which is probably the fact that wherever theVchannel (Value/Luminance) is very low, theH&Schannels are very unstable and are quite meaningless. In the extreme:[128,255,0] == [0,0,0].So very small and unnoticeable compression artifacts in the dark areas of the image become very prominent with the false colors of the
HSVcolor space.If you want to use the
HSVcolor space as feature space for color comparison keep in mind that ifVis very low,H & Sare quite meaningless. That is also true for very lowSvalues that make theHvalue meaningless ([0,0,100] == [128,0,100]).BTW. also keep in mind that the
Hchannel is cyclic and the difference betweenH == 0andH == 255is only one gray level.HSVimage posted in the questionRGBusingcvtColor