通过打开获取键盘的边界

2024-04-26 05:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用opencv和python开发一个虚拟键盘。我需要检测最大的轮廓,以获得键盘的边界和角落的坐标。你知道吗

以下是我真正需要的:

enter image description here

这是我的密码:

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()

Tags: importurlnp键盘requestscv2hsvframe
2条回答

您需要在最大轮廓周围找到边界框,而不是绘制轮廓本身。你知道吗

找到最大的轮廓并使用cv2.boundingRect()绘制边界框:

x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

其中cnt是图像中最大的轮廓,矩形的4个角是x, y, x+w, y+h

查看这个LINK中给出的边界矩形部分来帮助您。你知道吗

不要使用cv2.RETR_LIST参数,而是使用cv2.RETR_EXTERNAL

thresh1, contours, hierarchy = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(frame, contours, -1, (0, 255, 0), 3)

查看this教程以获取更多信息。你知道吗

相关问题 更多 >