视频帧中drawContour()
功能不正常。它通过使用这个堆栈溢出答案来处理图像。输出的是没有任何轮廓的清晰图像。
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
ret,thresh1 = cv2.threshold(frame,127,255,cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(thresh1,kernel,iterations = 1)
edges = cv2.Canny(erosion, 100 ,200)
imCopy = edges.copy()
laplacian = cv2.Laplacian(edges, cv2.CV_8UC1)
sobely = cv2.Sobel(laplacian,cv2.CV_8UC1, 0, 1, ksize=5)
im2, contours, hierarchy = cv2.findContours(sobely,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
frame = cv2.drawContours(im2, contours, -1, (255,0,0), 3)
cv2.imshow('window',frame)
print len(contours)
k = cv2.waitKey(5) & 0xFF
if k==27:
break
cv2.destroyAllWindows()
cap.release()
绘制的轮廓不会着色,因为您是在单通道图像上绘制它们。这里是如何绘制彩色的。(假设frame
= BGR
。如果原来的frame
是灰度,使用cvtColor
将下面代码中的frame_copy
转换为BGR
)。
_, frame = cap.read()
frame_copy = np.copy(frame)
.
.
.
cv2.drawContours(frame_copy, contours, -1, (255,0,0), 3)
cv2.imshow('window',frame_copy)
use
frame = cv2.drawContours(frame, contours, -1, (255,0,0), 3)
cv2.imshow('window',frame)