


# Fill a contour on both the single channel and three channel imageġ year, 5 months ago Related Topics python image image-processing opencv computer-vision Comments 8 years, 8 months ago You could draw in the original 'frame' itself instead of using gray image 8 years, 8 months ago that actually didn't work. # Merge channels to create color image (3 channels) After merging the grayscale image into three channels, we can now apply color onto the imageĬontour = np.array(,, , ])Ĭv2.fillPoly(gray,, )Ĭv2.fillPoly(gray_three,, ) The left image showcases the problem you're experiencing since you're trying to display three channels on a single channel image. We now draw a filled contour onto the single channel grayscale image (left) with shape (200,200,1) and the three channel grayscale image with shape (200,200,3) (right). Next we convert it to grayscale and create another image using cv2.merge() with three gray channels gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Image = (np.random.standard_normal() * 255).astype(np.uint8)

We create a color image with dimensions (200,200,3)

Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) To fix this, you can simply merge the three single channels image = cv2.imread('image.png') This is because you're trying to display three channels on a single channel image. I did some thresholding on an image and want to label the contours in green, but they aren't showing up in green because my image is in black and white. This effectively transforms a grayscale image of shape (height, width, 1) into (height, width, 3) We pass in a list of the three color channel layers - all the same in this case - and the function returns a single image with those color channels. Ret, gb = cv2.threshold(gray,128,255,cv2.THRESH_BINARY)Ĭontour,hier = cv2.findContours(gb,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)Ĭv2.drawContours(gray,contour,-1,(0,255,0),3)Īlternatively, cv2.merge() can be used to turn a single channel binary mask layer into a three channel color image by merging the same layer together as the blue, green, and red layers of the new image. Gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) Is this because it's a grayscale image? If so, can I convert the grayscale image back to RGB to visualize the contours in green? import numpy as np The code below does not appear to be drawing contours in green. I did some thresholding on an image and want to label the contours in green, but they aren't showing up in green because my image is in black and white.Įarly in the program I used gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) to convert from RGB to grayscale, but to go back I'm confused, and the function backtorgb = cv2.cvtColor(gray,cv2.CV_GRAY2RGB) is giving:ĪttributeError: 'module' object has no attribute 'CV_GRAY2RGB'. I'm learning image processing using OpenCV for a realtime application.
