图片轮廓的颜色是黑白
img0 = cv.imread('test_contours01.jpg')
img = cv.cvtColor(img0, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(img, 127, 255, 0) # 转化为二值
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # 找轮廓
# img = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
cv.drawContours(img, contours, -1, (0, 0, 255), 3)
cv.imshow('Image', img)
cv.waitKey(0)
cv.destroyAllWindows()
错误描述:上面代码绘制出来的图像轮廓颜色是灰色,不是red
原图:
错误图:
解决方法1:绘制彩色轮廓时,要将图像转化为BGR
img = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
cv.drawContours(img, contours, -1, (0, 0, 255), 3)
解决方法2:直接以原来的BGR图片作为cv.drawContours()输入参数
cv.drawContours(img0, contours, -1, (0, 0, 255), 3)
cv.imshow('Image', img0)