python - OpenCV( python ): Construct Rectangle from thresholded image

标签 python image opencv image-processing computer-vision

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

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 代码片段就太棒了。

“基础”图像 ->

Base Image

最佳答案

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

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

关于python - OpenCV( python ): Construct Rectangle from thresholded image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31039348/

相关文章:

python - Pandas:DataFrame 列表的单元格填充(方法 = 'pad' )

algorithm - 自动设置Canny边缘检测算法参数的实用方法

Java - getImage 和drawImage 不适用于本地文件

css - 如何根据内容是否在小屏幕上查看,在我的 Rails 应用程序中显示文本与图像?

Python - 从图像中检测二维码并使用 OpenCV 进行裁剪

python - 如何将 OpenNI VideoFrame 转换为 OpenCV Mat 数据结构?

python - 将 Python 与 OpenCV 结合使用时,哪些组件会做什么?

python - 404 错误页面 jinja2 的 TemplateNotFound

python - 我对 Blender 中的 Python 脚本有一个严重的问题

python - 从 numpy 中的函数返回对子矩阵的引用