我是使用 python 的 opencv 新手,所以请多多包涵。 我有一个托盘,上面有不同大小的圆圈,就像链接中那样。 https://is.alicdn.com/img/pb/810/421/429/429421810_364.jpg 这不是我的实际图像,但它与上面的图像非常相似。 我必须检测图像中的托盘并找到托盘中所有孔(圆圈)的轮廓。托盘可能会倾斜,具体取决于拍摄图像的用户。 到目前为止,我在图像上使用了高斯模糊和精明边缘检测,并弥补了精明边缘检测中的差距。这是Canny边缘检测后的图像 这是形态学后的图像。 然后我使用 findcontours 并尝试找到具有 4 个顶点的最大轮廓,理想情况下它是托盘本身。 轮廓检测只能识别左垂直边框和顶部水平边框。它无法识别托盘的 4 个边缘。
到目前为止,这是我的代码:
import numpy as np
import cv2
import matplotlib.pyplot as plt
image = cv2.imread("img.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (3, 3), 0)
image_canny = cv2.Canny(image, 30, 200)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
gaps_closed = cv2.morphologyEx(image_canny, cv2.MORPH_CLOSE, kernel)
_, contours, _= cv2.findContours(gaps_closed.copy(), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key = cv2.contourArea, reverse = True)
[:5]
Cnt = None
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
if len(approx) == 4:
Cnt = approx
break
cv2.drawContours(image, [Cnt], -1, (0, 255, 0), 4)
plt.imshow(image)
plt.show()
最佳答案
调整 cv2.canny 的参数应该可以更容易地检测托盘的边缘。但是,使用 cv2.HoughLines 可能是检测托盘的更好方法,因为 HoughLines 不需要检测完整的边缘。
关于python - 使用opencv python在游戏板上进行矩形检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44814878/