Unable to Process an image transformed in OpenCV via scikit-image
Question: I want to skeletonize an image using the scikit-image module for
skeletonization. This image is pre processed by OpenCV library. Given an Image
'Feb_16-0.jpg', I convert it to gray scale, perform the morphological
transformation of opening the image, then apply the Gaussian Blur and adaptive
thresholding using OpenCV and Python:

    
    
    import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    from skimage.morphology import skeletonize
    from skimage.viewer import ImageViewer
    img = cv2.imread('Feb_16-0.jpg',0)
    kernel = np.ones((1,1),np.uint8)
    opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
    blur = cv2.GaussianBlur(opening,(1,1),0)
    ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    

I now want to skeletonize the image using scikit-image
skimage.morphology.skeletonize. I have tried writing code for performing
erosion and dilation to manually skeletonize the image using OpenCV and
Python. But, this proved to be a highly inefficient processing so i decided to
switch to the scikit-image library at this point. However, when I pass the
numpy array preprocessed by OpenCV to the scikit-image module using the code:

    
    
    skel = skeletonize(th4)
    

and try to view the results of the same, I end up with the error:

    
    
    Image contains values other than 0 and 1
    

I am unable to interpret the cause for the same. Can anyone kindly help me out
in resolving this datatype error?


Answer: The input matrix to `skeletonize()` needs to be binary with either 0/1 or
True/False as entries. The output of `cv2.threshold()` is binary, but with
values 0/255. To convert the th4 matrix to 0/1 form you can for example do:

`th4[th4 == 255] = 1`

