【发布时间】:2020-06-17 10:57:45
【问题描述】:
我正在使用以下代码来检测明亮的灯。照明可能会有所不同。我正在使用以下代码来检测相同的内容。
img = cv2.imread("input_img.jpg")
rgb = img.copy()
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
while True:
th3 = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
cv2.THRESH_BINARY, 11, 2)
cv2.imshow("th3",th3)
edged = cv2.Canny(th3, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)
cv2.imshow("edge", edged)
cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
areaArray = []
for i, c in enumerate(cnts):
area = cv2.contourArea(c)
areaArray.append(area)
sorteddata = sorted(zip(areaArray, cnts), key=lambda x: x[0], reverse=True)
thirdlargestcontour = sorteddata[2][1]
x, y, w, h = cv2.boundingRect(thirdlargestcontour)
cv2.drawContours(rgb, thirdlargestcontour, -1, (255, 0, 0), 2)
cv2.rectangle(rgb, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("rgb", rgb)
if cv2.waitKey(1) == 27:
break
上面的代码有效,但是,
- 它只给出包围灯的矩形。如何精确获取灯的四个角点?
- 如何改进检测?目前我正在选择第三大轮廓,但不能保证它始终是灯,因为环境构成挑战?
ApproxPolydp 在轮廓完整时工作,但如果轮廓不完整,ApproxPolydp 不会返回正确的坐标。例如在下图中 approxpolydp 返回一个错误的坐标。
【问题讨论】:
-
“灯”是白色的长方形物体吗?
-
是的..它是白色的矩形对象
-
@nathancy 有什么想法可以准确地找到角点吗?
-
您是否使用霍夫线变换来检测“灯”的边界?如果是这样,您可以使用 Hough 给出的线并近似两条线的可能交点......这发生在每个角落。如果你推断点来计算线,如果你不能完美地检测到每个边界(就像在你的第二张图像中一样),那将不是什么大问题,因为你仍然可以很好地猜测角落将使用交叉点的位置的外推线。
标签: python opencv corner-detection image-enhancement