13

I tried to find contour with cv2 python library in a skeletonized image created with scikit-image and i got this error:

    contours, hierarchy = cv2.findContours(skel,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
TypeError: <unknown> data type = 0 is not supported

My question is: What i have to do to convert to cv2 and viceversa?

I know that opencv use numpy.uint8 type to represent binary images instead scikit-image numpy.float64

I used also mahotas (numpy.bool) and pymorph libraries. How can i convert from scikit-image to these libraries and viceversa?

improc
  • 415
  • 2
  • 9
  • 17
  • I think i found myself solution! If i use: skel = np.array(skel, dtype=np.uint8) i easily convert in opencv2 type and so on for other libraries! – improc Apr 22 '13 at 23:05
  • 1
    http://scikit-image.org/docs/dev/user_guide/data_types.html#using-an-image-from-skimage-with-opencv – tidy Jul 06 '17 at 06:48

2 Answers2

19

scikit-image provides conversion routines between the different data-types that also correctly preserves scaling:

from skimage import img_as_ubyte

cv_image = img_as_ubyte(any_skimage_image)

Update: the scikit-image user guide now has a more detailed section on this: http://scikit-image.org/docs/stable/user_guide/data_types.html#working-with-opencv

Stefan van der Walt
  • 7,165
  • 1
  • 32
  • 41
0

This is working for me:

image = skimage.io.imread(mpath)
img = image[:, :, ::-1] # convert image from RGB (skimage) to BGR (opencv)
cv2.namedWindow('original', cv2.WINDOW_NORMAL)
cv2.imshow('original',img)
JJJ
  • 32,902
  • 20
  • 89
  • 102
oferb
  • 127
  • 1
  • 5