【问题标题】:Improving accuracy of findContour in OpenCV Python提高 OpenCV Python 中 findContour 的准确性
【发布时间】:2016-04-02 00:01:34
【问题描述】:

我正在尝试为下面显示的每个“斑点”设置最小边界框。作为图像处理管道的一部分,我使用 findContours 检测数据中的轮廓,然后在给定一组已发现轮廓的情况下绘制一个最小边界框。

最小边界框不是很准确 - 一些特征明显遗漏,而其他特征未能完全“封装”一个完全连接的特征(而是被分割成几个小的最小边界框)。我玩过检索模式(如下所示的RETR_TREE)和轮廓近似方法(如下所示的CHAIN_APPROX_TC89_L1),但找不到我真正喜欢的东西。有人可以提出一个更强大的策略来使用 OpenCV Python 更准确地捕获这些轮廓吗?

import numpy as np
import cv2

# load image from series of frames
for x in range(1, 20):
    convolved = cv2.imread(x.jpg)
    original = convolved.copy

    #convert to grayscale   
    gray = cv2.cvtColor(convolved, cv2.COLOR_BGR2GRAY)

    #find all contours in given frame, store in array
    contours, hierarchy = cv2.findContours(gray,cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_L1)
    boxArea = []

    #draw minimum bounding box around each discovered contour
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 2 and area < 100:
            rect = cv2.minAreaRect(cnt)
            box = cv2.cv.BoxPoints(rect)
            box = np.int0(box)
            cv2.drawContours(original,[box], 0, (128,255,0),1)           
            boxArea.append(area)

    #save box-fitted image        
    cv2.imwrite('x_boxFitted.jpg', original)
    cv2.waitKey(0)

** 编辑:根据 Sturkman 的建议,绘制所有可能的轮廓似乎涵盖了所有视觉可检测的特征。

【问题讨论】:

  • 请发布您的代码
  • 请看新的编辑。
  • 我不知道opencv的这种方法,但通常你可以玩一些阈值。可能是精巧的边缘检测。另一种策略是使用 blob 检测。
  • 这是检测的问题,还是drawContours 方法的问题?
  • 尝试cv2.drawContours( original, contours,-1, (0,0,255), 2) 测试绘制所有轮廓

标签: python opencv computer-vision image-segmentation opencv-contour


【解决方案1】:

我知道这个问题是关于 opencv 的。但是由于我习惯了skimage,这里有一些想法(在opencv中肯定有)。

import numpy as np
from matplotlib import pyplot as plt
from skimage import measure
from scipy.ndimage import imread
from skimage import feature
%matplotlib inline

'''
Contour detection using a marching square algorithm.
http://scikit-image.org/docs/dev/auto_examples/plot_contours.html
Not quite sure if this is the best approach since some centers are
biased. Probably, due to some interpolation issue.
'''

image = imread('irregular_blobs.jpg')
contours = measure.find_contours(image,25,
                                 fully_connected='low',
                                positive_orientation='high')
fig, ax = plt.subplots(ncols=1)
ax.imshow(image,cmap=plt.cm.gray)
for n, c in enumerate(contours):
    ax.plot(c[:,1],c[:,0],linewidth=0.5,color='r')

ax.set_ylim(0,250)
ax.set_xlim(0,250)
plt.savefig('skimage_contour.png',dpi=150)

'''
Personally, I would start with some edge detection. For example,
Canny edge detection. Your Image is really nice and it should work.
'''
edges = feature.canny(image, sigma=1.5)
edges = np.asarray(edges)
# create a masked array in order to set the background transparent
m_edges = np.ma.masked_where(edges==0,edges)
fig,ax = plt.subplots()
ax.imshow(image,cmap=plt.cm.gray,alpha=0.25)
ax.imshow(m_edges,cmap=plt.cm.jet_r)


plt.savefig('skimage_canny_overlay.png',dpi=150)

本质上,没有“最好的方法”。例如,边缘检测可以很好地检测位置,但某些结构仍然是开放的。另一方面,轮廓查找会产生封闭结构,但中心有偏差,您必须尝试使用​​参数。如果您的图像有一些令人不安的背景,您可以使用膨胀来减去背景。 Here 是一些如何执行扩张的信息。有时,关闭操作也很有用。

从您发布的图片来看,您的阈值似乎太高或背景太嘈杂。降低阈值和/或扩张可能会有所帮助。

【讨论】:

  • 谢谢莫里茨。检测轮廓对我来说不是目的,而是一种测量每个“斑点”沿其主轴的长度的方法。我认为拟合一个最小边界框可以让我估计它的长度。你会碰巧知道沿着每个检测到的轮廓拟合多项式曲线的方法吗?
  • 你可以研究特征检测
【解决方案2】:

按照 Moritz 的建议,尝试使用 SimpleBlobDetector 是个好主意。

使用不同的选项,您可能会找到适合您问题的设置。

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2021-06-30
    • 2014-12-23
    • 1970-01-01
    • 2017-04-03
    • 2019-05-14
    相关资源
    最近更新 更多