【问题标题】:OpenCV (Python): Construct Rectangle from thresholded imageOpenCV(Python):从阈值图像构造矩形
【发布时间】:2015-09-11 09:51:23
【问题描述】:

下图显示了房屋街区的航拍照片(重新定向,最长边垂直),以及经过自适应阈值高斯差的同一图像>.

Images: Base; Adaptive Thresholding; Difference of Gaussians

房子的屋顶印在 AdThresh 图像上很明显(对人眼来说):这是连接一些明显的点的问题。在示例图像中,找到下面的蓝色边界框 -

Image with desired rectangle marked in blue

我在实现HoughLinesP()findContours() 方面有所突破,但没有得到任何明智的结果(可能是因为我遗漏了一些细微差别)。像蓝框一样远程找不到任何东西的python script-chunk如下:

import cv2
import numpy as np
from matplotlib import pyplot as plt

# read in full (RGBA) image - to get alpha layer to use as mask
img = cv2.imread('rotated_12.png', cv2.IMREAD_UNCHANGED)
grey = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Otsu's thresholding after Gaussian filtering
blur_base = cv2.GaussianBlur(grey,(9,9),0)
blur_diff = cv2.GaussianBlur(grey,(15,15),0)
_,thresh1 = cv2.threshold(grey,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

thresh = cv2.adaptiveThreshold(grey,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)


DoG_01 = blur_base - blur_diff
edges_blur = cv2.Canny(blur_base,70,210)

# Find Contours
(ed, cnts,h) = cv2.findContours(grey, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:4]
for c in cnts:
    approx = cv2.approxPolyDP(c, 0.1*cv2.arcLength(c, True), True)
cv2.drawContours(grey, [approx], -1, (0, 255, 0), 1)

# Hough Lines
minLineLength = 30
maxLineGap = 5

lines = cv2.HoughLinesP(edges_blur,1,np.pi/180,20,minLineLength,maxLineGap)
print "lines found:", len(lines)
for line in lines:
    cv2.line(grey,(line[0][0], line[0][1]),(line[0][2],line[0][3]),(255,0,0),2)

# plot all the images
images = [img, thresh, DoG_01]
titles = ['Base','AdThresh','DoG01']

for i in xrange(len(images)):
    plt.subplot(1,len(images),i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i]), plt.xticks([]), plt.yticks([])
plt.savefig('a_edgedetect_12.png')

cv2.destroyAllWindows()

我正在尝试在没有过多参数化的情况下进行设置。我对只为这张图像“定制”算法持谨慎态度,因为这个过程将在数十万张图像上运行(不同颜色的屋顶/屋顶可能与背景的区别不大)。也就是说,我很想看到一个“击中”蓝框目标的解决方案——这样我至少可以找出我做错了什么。

如果有人有一种快速而简单的方法来做这种事情,那么让 Python 代码 sn-p 来工作会很棒。

“基础”图像 ->

Base Image

【问题讨论】:

  • canny edge 怎么样?
  • 做好自适应阈值处理、打开和关闭(或您自己的膨胀和腐蚀量)、应用精确边缘检测、应用查找计数以及过滤至少 4 个角的轮廓。
  • 我也许可以帮助你,但我在工作。这可能需要大约半小时左右。我会尝试在 10 小时后返回。
  • 您是否尝试过使用不同的色彩空间?

标签: python image opencv image-processing computer-vision


【解决方案1】:

您应该应用以下内容:
1.对比度受限自适应直方图均衡-CLAHE并转换为灰度。
2. @bad_keypoints 提到的高斯模糊和Morphological transforms(拨号、腐蚀等)。这将帮助您摆脱背景噪音。这是最棘手的步骤,因为结果将取决于您应用的顺序(首先是高斯模糊,然后是形态变换,反之亦然)以及您为此目的选择的窗口大小。
3. 应用自适应阈值
4. 应用 Canny 的边缘检测
5. 找到具有四个角点的轮廓

如前所述,您需要调整这些函数的输入参数,还需要使用其他图像验证这些参数。因为它可能适用于这种情况,但不适用于其他情况。根据反复试验,您需要修复参数值。

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 2017-06-23
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多