Ask Your Question
2

How to put colors over a 1 channel gray image?

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

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 -0600

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 -0600 )edit
0

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

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 -0600 )edit

Help me to conver that to java I have this but no work

    Mat img1 = new Mat();
    Imgproc.cvtColor(img, img1, Imgproc.COLOR_GRAY2BGR);
    Mat img2 = detectedEdges.clone();

    Mat img2gray = new Mat();
    Imgproc.cvtColor(img2, img2gray, Imgproc.COLOR_BGR2GRAY);

    Mat mask = new Mat();
    Mat mask_inv = new Mat();
    Imgproc.threshold(img2gray, img2gray, 60, 25, Imgproc.THRESH_BINARY);

    Core.bitwise_not(mask_inv, mask_inv, mask);
    Core.bitwise_and(img1, img1, mask);
    Core.bitwise_and(img2, img2, mask_inv);
    Core.addWeighted(img1, 0.4, img2, 0.4, 0.0, img2);

    Bitmap bmp = Bitmap.createBitmap(img1.cols(), img1.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(img1, bmp);
manedragon gravatar imagemanedragon ( 2020-01-30 11:45:03 -0600 )edit

Question Tools

Stats

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

Seen: 28,066 times

Last updated: Oct 31 '14