python - 在图像中找到矩形,最好使用 skimage

标签 python opencv image-processing scikit-image

我有一个 Image从相机(我的大众 T5 货车的内部部分)获得。我想知道我能多准确地重现这些形状。我使用 lensfun 和 gimp 校正了镜头失真。我想通过使用矩形特征来量化剩余的失真。我可以使用 match_template 检测矩形,但我不知道如何检测矩形。

最后我想给它们贴上标签并测量宽度和长度。为了更好地可视化中心,我尝试定位中心,但这并不像我想象的那么简单,因为一个简单的阈值会多次计算相同的点。如何找到所有检测到的矩形的中心?

这可以用 skimage 完成吗?或者我应该使用 opencv 吗?

import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from scipy.ndimage.filters import maximum_filter
from scipy.ndimage import center_of_mass
from skimage.color import rgb2grey
from skimage.feature import match_template
from skimage import feature, io
from skimage import img_as_float
%matplotlib inline

image = io.imread('../lensfun_cut.JPG')
imgray = rgb2grey(image)

    template = imgray[1365:1390,445:470]
    result = match_template(imgray, template)

    # 
    hit = np.where(result>0.90)
    fig, ax = plt.subplots(ncols=1,figsize=(8,8))
    ax.imshow(result,cmap=plt.cm.spectral)

    # loop over the detected regions and draw a circe around them
    for i in range(len(hit[0])):
        rect = plt.Circle((hit[1][i],hit[0][i]), 20,linewidth=1, edgecolor='r', facecolor='none')
        ax.add_patch(rect)

enter image description here

最佳答案

抱歉,我手边有更快的 opencv 解决方案。

  • 我建议使用自适应阈值从具有不同光照的背景中正确分割矩形
  • drawcontours 允许您绘制轮廓
  • 我给了你一个轮廓特征列表和一个定义链接,这可以帮助你选择合适的矩形
  • 我正在使用一些轮廓特征来选择合适的 Blob ,您可能需要对此进行微调。

结果看起来是这样的:

enter image description here

代码如下:

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

img = cv2.imread('lensfun_cut.jpg',0)

# Adaptive threshold to detect rectangles independent from background illumination
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,31,5)

# Detect contours
_, contours, hierarchy = cv2.findContours( th3.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

# Draw contours
h, w = th3.shape[:2]
vis = np.zeros((h, w, 3), np.uint8)
cv2.drawContours( vis, contours, -1, (128,255,255), 3)

# Print Features of each contour and select some contours
contours2=[]
for i, cnt in enumerate(contours):
    cnt=contours[i]
    M = cv2.moments(cnt)

    if M['m00'] != 0:
        # for definition of features cf http://docs.opencv.org/3.1.0/d1/d32/tutorial_py_contour_properties.html#gsc.tab=0
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        area = cv2.contourArea(cnt)
        x,y,w,h = cv2.boundingRect(cnt)
        aspect_ratio = float(w)/h
        rect_area = w*h
        extent = float(area)/rect_area        

        print i, cx, cy, area, aspect_ratio, rect_area, extent

        if area < 80 and area > 10:
            contours2.append(cnt)

#Draw selected contours
h, w = th3.shape[:2]
vis2 = np.zeros((h, w, 3), np.uint8)
cv2.drawContours( vis2, contours2, -1, (128,255,255), 3)

titles = ['Original Image', 'Adaptive Gaussian Thresholding', 'Contours', 'Selected Contours']
images = [img, th3, vis, vis2]
for i in xrange(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

关于python - 在图像中找到矩形,最好使用 skimage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36635124/

相关文章:

python - 如何在此数组中的每个 ID 前后添加字符?

python - 如果特定列中存在重复值,则删除整行

reactjs - React.js + OpenCV.js ReferenceError:未定义cv

python - 如何修复我的 pandas 数据框中的索引,使其不只将值保持为零,而是增加值?

matlab - 为每个图像执行与新向量的卷积的最佳方法?

c++ - 圆圈检测: parameters for houghcricles

image-processing - 用于 3D 重建的图切割算法类型

python - 使用 Tkinter 的二次方程计算器。定义变量时遇到问题

python - Scrapy + 飞溅 : connection refused

python - 在不下载视频的情况下提取 youtube 视频的特定帧