我正在学习使用OpenCV进行实时应用程序的图像处理。我对图像进行了一些阈值处理,并希望将轮廓标记为绿色,但是由于我的图像是黑白图像,所以它们没有显示为绿色。
在程序的早期,我曾经gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)将RGB转换为灰度,但是回过头来,我很困惑,该函数backtorgb = cv2.cvtColor(gray,cv2.CV_GRAY2RGB)给出了:
AttributeError:“模块”对象没有属性“ CV_GRAY2RGB”。
下面的代码似乎不是用绿色绘制轮廓。这是因为它是灰度图像吗?如果是这样,我可以将灰度图像转换回RGB,以绿色显示轮廓吗?
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    ret, gb = cv2.threshold(gray,128,255,cv2.THRESH_BINARY)
    gb = cv2.bitwise_not(gb)
    contour,hier = cv2.findContours(gb,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contour:
        cv2.drawContours(gb,[cnt],0,255,-1)
    gray = cv2.bitwise_not(gb)
    cv2.drawContours(gray,contour,-1,(0,255,0),3)
    cv2.imshow('test', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()


