Opencv, BGR2HSV creates many artifacts

enter image description here

This image is just an example. Top right is the original image, top left is the hue, bottom left is saturation and lower right value. It is easy to see how both H and S are filled with artifacts. I want to reduce the brightness, so the result displays a lot of these artifacts.

What am I doing wrong?

My code is simple:

vc = cv2.VideoCapture( 0 ) # while true and checking ret ret, frame = vc.read() frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) cv2.imshow("h", frame_hsv[:,:,0]) cv2.imshow("s", frame_hsv[:,:,1]) cv2.imshow("v", frame_hsv[:,:,2]) 
+4
source share
2 answers

I feel that there is a misunderstanding in your question. Although Boyko Peranovโ€™s answer is certainly correct, there are no problems with the images you provided. The logic behind this is: your camera takes pictures in the RGB color space, which by definition is a cube. When you convert it to HSV color space, all pixels are mapped to the following cone: HSV Cone

Hue (the first HSV channel) is the angle on the cone, Saturation (the second HSV channel, called Chroma in the image) is the distance to the center of the cone and the value (third HSV channel) is the height on the cone.

The Hue channel is usually defined between 0-360 and starts with red from 0 (in the case of 8-bit images, OpenCV uses the range 0-180 to match the unsigned char, as indicated in the documentation ). But the fact is that two pixels with a value of 0 and 359 are really very close in color. This is easier to see when aligning the HSV cone, taking only the outer surface (when the saturation is maximum): Flattened HSV cone

Even if these values โ€‹โ€‹are perceptually close (completely red at 0 and red with a little violet at 359), these two values โ€‹โ€‹are far apart. This is the reason for the "artifacts" that you describe on the Hue channel. When OpenCV shows it to you in grayscale, it displays black at 0 and white at 359. They really look like similar colors, but when displayed in grayscale, they are displayed too far apart. There are two ways to get around this anti-intuitive fact: you can translate channel H into an RGB space with a fixed saturation and value, which will show a more accurate idea of โ€‹โ€‹our perception. You can also use another perceptual color space (such as Lab Color Space ) that will not give you these mathematical side effects.

The reason these squares of the artifact are square is explained by Boyko Peranov. JPEG compression works by replacing pixels with large squares that approximate the patch that it replaces. If you set the compression quality to a low level when creating jpg, you can see that these squares appear even in the RGB image. The lower the quality, the larger and more noticeable the squares. The average value of these squares is one value that, with red tones, can range from 0 to 5 (displayed as black) or 355 and 359 (displayed as white). This explains why the โ€œartifactsโ€ are square in shape.

We may also ask ourselves why there are more JPEG compression artifacts in the hue channel. This is due to the color cross , where perceptual studies have shown that our eyes are less likely to see rapid color changes than rapid intensity changes.Thus, when compressed, JPEG intentionally loses color information because we won't notice it.

The story is like saturation (your bottom left image) in white spots. You describe the pixels are almost black (at the tip of the cone). Consequently, the saturation value can vary greatly, but will not greatly affect the color of the pixel: it will always be next to black. It is also a side effect of the HSV color space, which is not purely perceptual.

Conversion between RGB (or BGR for OpenCV) and HSV (theoretically) lossless. You can verify this: Convert the HSV image to RGB, you will get the same image that you started with, no artifacts added.

+10
source

You work with compressed compressed image, therefore, rectangular artifacts. With video, you have low exposure time, may have bandwidth limitations, etc. Thus, the overall image quality is degraded. You can:

  • Use a series of still pictures using Capture instead of VideoCapture or
  • Retrieve 5-10 video frames and average them.
+3
source

Source: https://habr.com/ru/post/1487504/


All Articles