Skip to content Skip to sidebar Skip to footer

Python Opencv Drawcontour Error

It work, but my contour's color is black. How to change it to red or green? import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('1.j

Solution 1:

First:

contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2:]

Second

ret , threshold = cv2.threshold(res,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 
# ...
cv2.drawContours(threshold,contours,-1,(0,255,0),3)

You draw in color (0,255,0) on binary threshed image, then it will always be the first element 0, that's black. You should convert gray to BGR first, then draw color.

canvas = cv2.cvtColor(threshold, cv2.COLOR_GRAY2BGR)
cv2.drawContours(canvas,contours,-1,(0,255,0),3)

Post a Comment for "Python Opencv Drawcontour Error"