是否有任何快速算法可以找到图的最大外轮廓?

时间:2017-08-17 15:15:11

标签: python image algorithm contour marching-cubes

我有一个从scipy.measure.label获得的标记矩阵enter image description here。现在,我需要获得每个特征的外轮廓。我尝试了cv2.findContours,但轮廓找不到标签。我认为应该有一个简单的算法来做到这一点,但不幸的是,找不到。

1 个答案:

答案 0 :(得分:0)

对不起,我混淆了行和列(为什么它们在opencv中的顺序相反?) 这是代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import scipy
import numpy as np
from skimage import filters, feature, io
from skimage.color import rgb2grey
def find_bounds(im):
    edges1 = filters.scharr(im)
    gt=filters.threshold_otsu(edges1)
    edges1 = edges1>gt
    return scipy.ndimage.measurements.label(edges1)
def object_filter(img, considered):
    def func(t): return 255 if t==considered else 0
    func=np.vectorize(func)
    return func(img).astype('uint8')
im=io.imread('gesture.jpg')
im=rgb2grey(im)
labels, num_of_labels=find_bounds(im)
contours,_ = cv2.findContours(object_filter(labels, 1),2,1)
cnt = contours[0]

hull = cv2.convexHull(cnt,returnPoints = False)
contour_matrix=np.zeros(im.shape, 'uint8')
for t in cnt:
    print t, labels[t[0][1]][t[0][0]]
    contour_matrix[t[0][1]][t[0][0]]=1
print '====================='
print contour_matrix