我正在用opencv和python开发一个虚拟键盘。我需要检测最大的轮廓,以获得键盘的边界和角落的坐标。你知道吗
以下是我真正需要的:
这是我的密码:
import numpy as np
import cv2
import requests
url = 'http://192.168.1.100:8080/shot.jpg'
while (True):
imgResp = requests.get(url)
imgNp = np.array(bytearray(imgResp.content), dtype=np.uint8)
frame = cv2.imdecode(imgNp, -1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret1, thresh1 = cv2.threshold(hsv, 100, 255, cv2.THRESH_BINARY)
thresh1, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(frame, contours, -1, (0, 255, 0), 3)
cv2.imshow('thresh1', thresh1)
cv2.imshow('frame', frame)
# Q Quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
您需要在最大轮廓周围找到边界框,而不是绘制轮廓本身。你知道吗
找到最大的轮廓并使用
cv2.boundingRect()
绘制边界框:其中
cnt
是图像中最大的轮廓,矩形的4个角是x, y, x+w, y+h
查看这个LINK中给出的边界矩形部分来帮助您。你知道吗
不要使用
cv2.RETR_LIST
参数,而是使用cv2.RETR_EXTERNAL
查看this教程以获取更多信息。你知道吗
相关问题 更多 >
编程相关推荐