Ask Your Question
2

How to put colors over a 1 channel gray image?

asked 2014-10-31 05:21:55 -0500

Brixus gravatar image

Hello,

I have a grayscale image (CV_8UC1) and I use findContours to identify contours. These contours I would like to draw colored into the grayscale image.

I tried to merge two empty Mats(CV_8UC1) together with the grayscale image and this works not really well. So I have my wished colors, but the original image is blue, red or green depending on the merged channel position.

How can I achieve the grayscale image staying gray with having colored contoures on top?

Thank you very much :-)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2014-10-31 06:02:37 -0500

cv::cvtColor is your friend:

cv::cvtColor(gray_img,color_img,CV_GRAY2BGR)
edit flag offensive delete link more

Comments

1

Thank you very much. :-) Sometimes one does not see all the trees because of the forest ;-)

Brixus gravatar imageBrixus ( 2014-10-31 06:16:56 -0500 )edit
0

answered 2014-10-31 06:46:21 -0500

ej gravatar image

Suppose you already have the contours BGR image, by using drawContours for example. Then you can combine your 2 images by Bitwise Operations (The answesr it taken basically from the python tutorials).

Just make sure first to convert your original grayscale image to BGR and then you can do something like this:

img1 = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)    
img2 = contours_img

# Create a mask of contours and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of contours in original image
img1_bg = cv2.bitwise_and(img1, img1, mask = mask_inv)

# Take only region of contours from contours image.
img2_fg = cv2.bitwise_and(img2, img2, mask = mask)

# Put contours in image and modify the main image
dst = cv2.add(img1_bg,img2_fg)    
cv2.imshow('res',dst)
edit flag offensive delete link more

Comments

Thanks, this works, but is to much work ;-) I just needed to convert my image as FooBar stated and then I used drawContours on that :-)

Brixus gravatar imageBrixus ( 2014-11-05 16:37:49 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-10-31 05:21:55 -0500

Seen: 13,761 times

Last updated: Oct 31 '14