I made a simple code to find the contour of an object in an image.
img = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
blur = cv2.GaussianBlur(img_enhanced,(25,25),0) # apply blur for contour
ret, binary = cv2.threshold(blur,1,255,cv2.THRESH_BINARY) # apply threshold to blur image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # find countour
obj_index = contours.index(max(contours, key=len)) # find index of largest object
contour_img = cv2.drawContours(img, contours, obj_index, (0,255,0), 3) # draw coutour on original image
plt.imshow(contour_img)
plt.show()
The original image is already grayscale but anyway I applied cv2.IMREAD_GRAYSCALE when I import the image.
And I thought if I apply the grayscale image when I 'Draw' contour, with below syntax,
contour_img = cv2.drawContours(img, contours, obj_index, (0,255,0), 3)
I can have a grayscale image with colored contour, but it shows weird colored image.
How can I draw a colored contour line on the grayscale image?
Thanks in advance
image sample: black background and white object