我知道我的问题可能听起来有点笼统,但我读了很多关于这方面的博客和问题,仍然找不到任何接近我想要做的事情的解决方案。我有附加的样本IG屏幕截图。我的目标是获得我用绿色突出显示的元素的坐标。Canny edges似乎不会做任何事情(另请参阅https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html)。我读到了霍格变换可能有帮助(https://pysource.com/2018/03/07/lines-detection-with-hough-transform-opencv-3-4-with-python-3-tutorial-21/),但仍然没有运气。我所需要的就是找到一种方法来得到这些部分并得到它们的坐标。如果有人能简单地指出哪些技术可以帮助我,那将是很有帮助的。

发布于 2018-09-06 06:03:33
假设它们在实际数据中不是绿色的!
标准的方法是边缘检测(canny或adaptiveThreshold),然后是轮廓。使用approxPolyDP简化轮廓,然后在轮廓列表中搜索具有正确长度和形状的边。
提示:使用cv2.minAreaRect()检查可能是长方体大小/形状/对齐的轮廓
您可以使用probablistic Hough查找线段,但由于源图像非常干净,因此只需查看轮廓会更容易
发布于 2018-09-06 05:49:24
你好,我想你可以做下一步:
由于您已经真正确定了边界矩形的颜色,因此一旦使用image_green
1. Loop over the image\_bin matrix and find all the "corners" that will be all the couple of pixels that match this condition `image_bin[i:i+2,j]/max(image_bin.flatten()) == [1,0]` for any `j` (column), and `i` (row).
2. Store this coordinates (`[i,j]`). As I mentioned before this values represent 2 corners of the rectangles: The upper-left corner and the lower-right corner.
这种方法是用numpy数组完成的,所以你可以想象你需要在步骤3之前将图像转换成数组。如果你在这方面仍然有问题,请让我知道,只要我有空闲时间,我就会发布一个代码来做这件事。
祝好运!
发布于 2021-02-19 19:08:39
这种边界检测可以使用普通的Canny边缘检测器来完成。我们只需要在应用Canny之前对图像进行锐化。
代码如下:
import cv2
import numpy as np
image = cv2.imread('test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Sharpen the image
kernel = np.array([[-1, -1, -1],[-1, 8, -1],[-1, -1, 0]], np.float32)
dst = cv2.filter2D(gray, -1, kernel)
#Canny Edge detection
canny = cv2.Canny(dst, 30, 200, 1)
# Find contours
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Iterate thorugh contours and draw rectangles around contours
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (255,0,255), 1)
#write output
cv2.imwrite('resultimage.png', image)https://stackoverflow.com/questions/52193545
复制相似问题