Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检测给定图像中的所有矩形框_Python_Opencv_Image Processing - Fatal编程技术网

Python 如何检测给定图像中的所有矩形框

Python 如何检测给定图像中的所有矩形框,python,opencv,image-processing,Python,Opencv,Image Processing,我尝试使用阈值、canny边缘和应用轮廓检测来检测图像中的所有矩形,但无法检测所有矩形。最后,我想到了使用hough变换检测相同的图像,但当我尝试检测图像中的线条时,我得到了所有线条。我只需要检测图像中的矩形框。有人能帮我吗?我是opencv的新手 输入图像 代码: 建议: 使用Hough,检测水平线。然后把每一条线依次考虑一个窗口一样大的线和一个很短的高度。使用Hough检测此窗口中的垂直线。这将给你的候选人角落。(您也可以在线条上方和下方各尝试一个窗口。) 然后,通过一些附加的局部处理(?

我尝试使用阈值、canny边缘和应用轮廓检测来检测图像中的所有矩形,但无法检测所有矩形。最后,我想到了使用
hough
变换检测相同的图像,但当我尝试检测图像中的线条时,我得到了所有线条。我只需要检测图像中的矩形框。有人能帮我吗?我是opencv的新手

输入图像

代码:


建议:

使用Hough,检测水平线。然后把每一条线依次考虑一个窗口一样大的线和一个很短的高度。使用Hough检测此窗口中的垂直线。这将给你的候选人角落。(您也可以在线条上方和下方各尝试一个窗口。)


然后,通过一些附加的局部处理(?),确认候选对象确实是长方体角点,并找到角点方向。最后,您应该能够以一种几何上合理的方式连接角点?

您可以使用以下代码作为起点

img =  cv2.imread('demo-hand-written.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

thresh_inv = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]

# Blur the image
blur = cv2.GaussianBlur(thresh_inv,(1,1),0)

thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# find contours
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

mask = np.ones(img.shape[:2], dtype="uint8") * 255
for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)
    if w*h>1000:
        cv2.rectangle(mask, (x, y), (x+w, y+h), (0, 0, 255), -1)

res_final = cv2.bitwise_and(img, img, mask=cv2.bitwise_not(mask))

cv2.imshow("boxes", mask)
cv2.imshow("final image", res_final)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:

图1:上图中检测到的矩形框

图2:在原始图像中检测到的矩形轮廓


这可能会有帮助:很好的解决方案,你能解释一下阈值化然后模糊然后再阈值化的思维过程吗?首先是使用大津的方法进行二值阈值化,只需反转背景和内容颜色。只需将此结果传递给
cv2.findContours
函数即可获得结果。但是,我故意在高斯滤波之后添加了大津的阈值,只是为了更好更一般的输出。在高斯模糊中改变核值似乎会影响结果,(1,1)在上述情况下效果很好。最后,建议在高斯模糊后应用otsu阈值。见此:
img =  cv2.imread('demo-hand-written.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

thresh_inv = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]

# Blur the image
blur = cv2.GaussianBlur(thresh_inv,(1,1),0)

thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# find contours
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

mask = np.ones(img.shape[:2], dtype="uint8") * 255
for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)
    if w*h>1000:
        cv2.rectangle(mask, (x, y), (x+w, y+h), (0, 0, 255), -1)

res_final = cv2.bitwise_and(img, img, mask=cv2.bitwise_not(mask))

cv2.imshow("boxes", mask)
cv2.imshow("final image", res_final)
cv2.waitKey(0)
cv2.destroyAllWindows()