我一直在尝试使用cv2.matchshapes()比较livefeed的轮廓和图像的轮廓。我用的是 image
这是我正在使用的代码(很抱歉格式太草率):
import numpy as np
import cv2
cap = cv2.VideoCapture(2)
img1 = cv2.imread('prod1_gray.jpg',0)
ret, thresh_img1 = cv2.threshold(img1, 230, 255, cv2.THRESH_BINARY)
kernel = np.ones((3, 3), np.uint8)
thresh_img1 = cv2.dilate(thresh_img1, kernel, iterations=2)
thresh_img1 = cv2.erode(thresh_img1, kernel, iterations=3)
_, contours_img1, hierarchy = cv2.findContours(thresh_img1,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours_img1:
cntimg = contours_img1[0]
while True:
_, frame = cap.read()
frame = frame[127:470, 80:550]
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(frame_gray, 230, 255, cv2.THRESH_BINARY)
kernel = np.ones((3, 3), np.uint8)
thresh = cv2.dilate(thresh, kernel, iterations=2)
thresh = cv2.erode(thresh, kernel, iterations=3)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours:
cnt = contours[0]
area = cv2.contourArea(cnt)
if area > 30:
cv2.drawContours(frame, contours, -1, (0, 255, 255), 3)
ret = cv2.matchShapes(cnt[0], cntimg[0], 1, 0.0)
print(ret)
cv2.imshow("Original", frame)
cv2.imshow("thresh",thresh)
cv2.imshow("gray", frame_gray)
cv2.imshow("prod1",img1)
k = cv2.waitKey(30)
if k == 27:
cv2.imwrite("pen.jpg", img1)
print("cntimg, ",cntimg[0])
print("cnt, ",cnt[0])
break
cap.release()
cv2.destroyAllWindows()
由于某些原因,cv2.matchShapes()总是输出0.0,无论相机前面是什么(没有轮廓)还是像pen这样的随机对象。但它应该输出一个数字,因为笔的形状与原始对象不同。在
我做错什么了?在
目前没有回答
相关问题 更多 >
编程相关推荐