bat*_*ike 5 python opencv computer-vision image-segmentation opencv-contour
我正在尝试将最小边界框适合下面显示的每个“斑点”。作为图像处理管道的一部分,我使用 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)
Run Code Online (Sandbox Code Playgroud)
** 编辑:根据 Sturkman 的建议,绘制所有可能的轮廓似乎涵盖了所有视觉上可检测的特征。
我知道问题是关于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)
Run Code Online (Sandbox Code Playgroud)
'''
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)
Run Code Online (Sandbox Code Playgroud)
本质上,不存在“最好的方法”。例如,虽然边缘检测可以很好地检测位置,但某些结构仍然开放。另一方面,轮廓查找会产生闭合结构,但中心有偏差,您必须尝试调整参数。如果您的图像有一些令人不安的背景,您可以使用膨胀来减去背景。以下是有关如何执行扩张的一些信息。有时,关闭操作也很有用。
从您发布的图像来看,您的阈值似乎太高或背景太嘈杂。降低阈值和/或扩张可能会有所帮助。