Ask Your Question

How to put colors over a 1 channel gray image?

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

Brixus gravatar image


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

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

cv::cvtColor is your friend:

edit flag offensive delete link more



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

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)    
edit flag offensive delete link more


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

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

Question Tools


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

Seen: 21,472 times

Last updated: Oct 31 '14