2

I am trying to convert an RGB image to YUV space and then I am trying to add color thresholds to create a binary image.

I am doing this as below:

img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)

b_channel = cv2.cvtColor(img, cv2.COLOR_RGB2Lab)[:, :, 2]
l_channel = cv2.cvtColor(img, cv2.COLOR_RGB2LUV)[:, :, 0]
s_channel = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)[:, :, 2]

s_binary = np.zeros_like(s_channel)
s_binary[(s_channel > s_thresh_min) & (s_channel <= s_thresh_max)] = 1

b_binary = np.zeros_like(b_channel)
b_binary[(b_channel > b_thresh_min) & (b_channel <= b_thresh_max)] = 1

l_binary = np.zeros_like(l_channel)
l_binary[(l_channel > l_thresh_min) & (l_channel <= l_thresh_max)] = 1

combined_binary = np.zeros_like(b_binary)
combined_binary[(l_binary == 1) | (b_binary == 1) | (s_binary == 1)] = 1
f, ax1 = plt.subplots(1, 1, figsize=(10, 5))

ax1.imshow(combined_binary)

This all works well and I can see the binary image on the screen.

However, when I try to save it on the disk using cv2.imwrite, I get a black image.

cv2.imwrite(file_name, combined_binary)

I am really confused why this change between the imshow and imwrite.

Any hints?

Images in different color space, displayed using plt.imshow()

Image written to the disk

Thanks

5
  • I am confused, why you pass it to YUV and then to 3 other colorspaces (from RGB and not YUV)? is the purple and yellow image the combined_binary ? what is filename extension? is combined_binary of type np.uint8`? Commented Apr 13, 2018 at 12:19
  • I was just trying conversions into different color spaces. I even tried to use only RGB ( no color to YUV) but same issue. Commented Apr 13, 2018 at 12:38
  • 2
    "I get a black image." -- No, you don't. You get an image using 2 colours -- completely black (intensity 0) and "just a little less completely black" (intensity 1). Of course, those are rather difficult for a human to distinguish by sight. The different behaviour of the two functions is due to their different intent. One is meant to store the image as is, the other is meant to visualize it so a human can see it well (hence the false colour). Commented Apr 13, 2018 at 12:41
  • yes you are right. As suggest in the answer below, i multiplied the numpy array with 255 and was able to see the image. I just wanted to make sure the post processing steps works and I get a valid image, before i feed it to the network. thanks for the suggestion. Commented Apr 13, 2018 at 12:44
  • @blackbug that is why i asked about the type. Probably it is a float number and it has values from 0.0-1.0 . Or maybe because is binary... it is true or false values (that in a lot of cases is 0 and 1) Commented Apr 13, 2018 at 12:50

1 Answer 1

4

May need to multiply your output array by 255. The thresholding usually returns 0 and 1. Not sure about opencv, but I had similar problem using other packages. It may looks black but actually 0s and 1s.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. You are right. I multiplied it with 255 and was able to see the pixels.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.