图像中的正方形/矩形检测

问题描述

我正在尝试检测此图像的第一层和第二层的窗户,即正方形的窗户。

enter image description here

我遵循了此链接,并在尝试以下操作时从中获得了一些启发:

将图像转换为灰度和中值模糊以平滑图像

锐化图像以增强边缘
阈值
进行形态转换
查找轮廓并使用最小/最大阈值区域进行过滤

此外,这是我收到的。我可以得到本质上想要变黑的窗户,但是在那之后,我的脚本根本无法在原始图片中检测到这些窗户。

enter image description here

这是我使用以下脚本尝试的操作:

    import cv2
import numpy as np

building_picture = r"C:\Users\Desktop\URA (Fall 2020)\view-2.jpg"
image = cv2.imread(building_picture)

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray,5)
sharpen_kernel = np.array([[-1,-1,-1],[-1,9,-1]])
sharpen = cv2.filter2D(blur,sharpen_kernel)

thresh = cv2.threshold(sharpen,100,300,cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
close = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=2)

cnts = cv2.findContours(close,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]


min_area = 30
max_area = 500
image_number = 0
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area and area < max_area:
        x,y,w,h = cv2.boundingRect(c)
        ROI = image[y:y+h,x:x+h]
        #cv2.imwrite('ROI_{}.png'.format(image_number),ROI)
        cv2.rectangle(image,(x,y),(x + w,y + h),(36,255,12),2)
        image_number += 1

cv2.imshow('sharpen',sharpen)
cv2.imshow('close',close)
cv2.imshow('thresh',thresh)
cv2.imshow('image',image)
cv2.waitKey()

有人可以帮助我检测这些窗口吗?我也尝试过模板匹配,但该技术似乎效果不佳。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)