-1

I have a labeled matrix enter image description here obtained from scipy.measure.label. Now for each feature I need to obtain its outer contour. I tried cv2.findContours but contour it find not consist with labels. I think there should be a simple algorithm to do this, but unfortunately, could not find.

A. Bykov
  • 288
  • 1
  • 16
  • 1
    "contour it find not consist with labels": what you you mean ??? –  Aug 17 '17 at 15:40
  • I'm loking for a contour which is 2xN array, consisting of N pairs x,y, such that (x1, y1)-(x2, y2) may take a values (-1, 0), (0, -1), (1, 0), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1) and M[x, y]=l where M is labeled matrix and l is considered label. For example, for feature, which labeled with number one, contour coordinates (x, y) correspond to cells labeled with one. – A. Bykov Aug 17 '17 at 18:06
  • `cv2.findContours` returns a vector of coordinates. Converting to a vector of deltas and retrieving the label is trivial. –  Aug 17 '17 at 19:04
  • I tried this but coordinates it return not coinside with labels. I wrote about this in the first post. Please read starting posts carefully every time. – A. Bykov Aug 17 '17 at 19:27
  • Are you referring to "contour it find not consist with labels" ? Please write carefully. –  Aug 17 '17 at 20:41

1 Answers1

0

I'm sorry, I cofused the rows and the columns(why they in reverse order in opencv?) Here is the code:

#!/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
A. Bykov
  • 288
  • 1
  • 16