5

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

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

图 1 图 2

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 的建议,绘制所有可能的轮廓似乎涵盖了所有视觉可检测的特征。

在此处输入图像描述

4

2 回答 2

2

我知道问题是关于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)

Canny 边缘检测

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

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

于 2015-12-29T09:04:33.300 回答
0

正如 Moritz 所建议的,尝试使用SimpleBlobDetector是个好主意。

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

于 2015-12-29T07:37:00.153 回答