首页 文章

OpenCV:在二进制图像中绘制对象的轮廓

提问于
浏览
1

按照http://docs.opencv.org/trunk/d4/d73/tutorial_py_contours_begin.html提供的示例,我在Spyder环境中使用python来绘制二进制图像中检测到的组件的轮廓 . 我的代码在这里:

im = cv2.imread('test.jpg') #raw RGB image
im2 = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) #gray scale image
plt.imshow(im2,cmap = 'gray')

图像显示如下:

enter image description here

然后,

thresh, im_bw = cv2.threshold(im2, 127, 255, cv2.THRESH_BINARY) #im_bw: binary image
im3, contours, hierarchy = cv2.findContours(im_bw,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(im2, contours, -1, (0,255,0), 3)
plt.imshow(im2,cmap='gray')  #without the code, only an array displayed in the console

由于某些原因,这些代码没有给出具有轮廓的图形 . 但如果我更改最后两行代码如下:

cv2.drawContours(im, contours, -1, (0,255,0), 3)
plt.imshow(im,cmap='gray')

它产生一个带轮廓的数字:

enter image description here

我对这些代码如何工作感到困惑? cv2.drawCoutours仅适用于GRB图像吗?希望不是 . 此外,值得注意的是,轮廓[0]给出了一个3D数组:

idx = contours[0]
print idx.shape

(392L, 1L, 2L)

idx应该存储所有轮廓点的像素坐标 . 但是如何解释每个维度代表什么,并从中获取每个轮廓点的像素坐标?然后我可以使用这些坐标来绘制轮廓而无需使用cv2.drawContours

1 回答

  • 1

    cv2.findContours和cv2.drawContours都是破坏性方法,它们修改原始输入图像,后者仅适用于绘图的RBG图像,这解释了在绘制的灰度图像中没有显示可见轮廓 .

    除了cv2.drawContours之外,还可以使用由cv2.findContours获得的“轮廓”(在上面的代码中)存储的边界像素的索引来容易地绘制轮廓 .

相关问题