gpt4 book ai didi

python - OpenCV findContours()仅在图像已保存并事先读取的情况下才检测轮廓

转载 作者:行者123 更新时间:2023-12-02 16:59:24 27 4
gpt4 key购买 nike

提供一些上下文:
我正在尝试获取这张图片中的盒子数量,
enter image description here
我上面的图像存储在ndarray blank_img中。
如果我运行以下命令:

v = np.median(blank_img)
sigma = 0.33
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edges = cv2.Canny(blank_img, lower, upper, 3)
edges = cv2.dilate(edges, np.ones((2, 2), dtype=np.uint8))

cnts= cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
len(cnts)等于1。
但是如果我在canny边缘检测和findContours之前保存数组blank_img,如下所示:
cv2.imwrite('boxes.jpg', blank_img)

blank_img = cv2.imread('boxes.jpg')
现在,当我运行我的findContour代码段时,len(cnts)>1。我想知道如何修复它,这样就无需节省图像开销。
~~编辑~~
查找以下用于创建blank_img的代码
blank_img = 255*np.zeros((height, width, 3), np.uint8)
for line in lines:
x1, y1, x2, y2 = line
cv2.line(blank_img,(x1,y1),(x2,y2),(255,255,255),1)
其中 lines是HoughLinesP返回的行的列表:
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, None,minlinelength,maxlinegap)

最佳答案

问题出在这里:
看到这张图片:
problem is here
保存图像并再次读取时,将在左下角创建一个小的绿色独立矩形。 cnts的实际长度应仅为1(外部大矩形)。
存储图像并再次读取后,此小矩形将出现在Canny边缘图像中。我不知道在存储图像然后读取图像时被检测到的原因。只需删除两行,即可直接使用图像。

关于python - OpenCV findContours()仅在图像已保存并事先读取的情况下才检测轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62845701/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com