我正在学习使用OpenCV进行实时应用的图像处理。我对图像做了一些阈值处理,想用绿色标记轮廓,但它们没有用绿色显示,因为我的图像是黑白的。
在程序的早期,我使用了gray=cv2.cvtColor(frame,cv2.COLOR戋BGR2GRAY)将RGB转换为grayscale,但是返回时我感到困惑,函数backtorgb=cv2.cvtColor(gray,cv2.CV戋GRAY2RGB)给出了AttributeError:“module”对象没有属性“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()