drawContour()
功能在视频帧中无法正常工作。它使用此stack-overflow answer来处理图像。输出只是精确的图像,没有绘制任何轮廓。
这是我的代码:
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()
答案 0 :(得分:0)
使用
frame = cv2.drawContours(frame, contours, -1, (255,0,0), 3)
cv2.imshow('window',frame)
答案 1 :(得分:0)
绘制的轮廓不会被着色,因为您在单个通道图像上绘制它们。这是如何绘制彩色的。 (假设frame
为BGR
。如果原始frame
为灰度,请使用frame_copy
将以下代码中的BGR
转换为cvtColor
。
_, frame = cap.read()
frame_copy = np.copy(frame)
.
.
.
cv2.drawContours(frame_copy, contours, -1, (255,0,0), 3)
cv2.imshow('window',frame_copy)