Converting a PIL image to skimage?

6.3k views Asked by At

I have an image loaded in my code (very long and unnecessary, won't post here) that I need to work on with skimage to detect blobs in the image. However for some reason all of the images and attempts that I have used don't work.

The image is modified by PIL in the code above to make the objects I am trying to find only white (255, 255, 255), and all other pixels black. This code only needs to identify the positions of these. I have tried many different things to get this to work (converting them to a numpy array, not converting them to a numpy array ect.)

I am following these instructions : http://scikit-image.org/docs/dev/auto_examples/plot_blob.html for the basics, but it is not really a tutorial, more an example.

I think that the rgb2gray function is being problematic as it returns nothing but 0, whilst the print(numpy.array(img)) returns values between 0 and 1, suggesting that the loss of information is when the rgb2gray is called.

Here is the code that seems to be problematic:

img_gray = rgb2gray(numpy.array(img)) # Convert to numpy array for skimage
print(img_gray)
print(numpy.array(img))
img_blobs = blob_doh(img_gray, threshold=0.01, max_sigma=500)
print(img_blobs)

How can I fix this?

EDIT:

Here are what each of the images, arrays and lists print:

[[ 0.          0.          0.         ...,  0.          0.          0.        ] #img_gray
 [ 0.          0.00392157  0.01568627 ...,  0.          0.          0.        ]
 [ 0.          0.01176471  0.05882353 ...,  0.          0.          0.        ]
 ..., 
 [ 1.          0.99607843  0.96078431 ...,  0.          0.          0.        ]
 [ 1.          1.          0.98039216 ...,  0.          0.          0.        ]
 [ 1.          1.          0.99215686 ...,  0.          0.          0.        ]]
[[[  0   0   0] #img
  [  0   0   0]
  [  0   0   0]
  ..., 
  [  0   0   0] #img
  [  0   0   0]
  [  0   0   0]]

 [[  0   0   0] #img
  [  1   1   1]
  [  4   4   4]
  ..., 
  [  0   0   0] #img
  [  0   0   0]
  [  0   0   0]]

 [[  0   0   0] #img
  [  3   3   3]
  [ 15  15  15]
  ..., 
  [  0   0   0] #img
  [  0   0   0]
  [  0   0   0]]

 ..., 
 [[255 255 255] #img
  [254 254 254]
  [245 245 245]
  ..., 
  [  0   0   0] #img
  [  0   0   0]
  [  0   0   0]]

 [[255 255 255] #img
  [255 255 255]
  [250 250 250]
  ..., 
  [  0   0   0] #img
  [  0   0   0]
  [  0   0   0]]

 [[255 255 255]
  [255 255 255]
  [253 253 253]
  ..., 
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]]
[] #img_blobs
1

There are 1 answers

0
keithpjolley On

This will read image files (using PIL) into scimage:

from skimage import io
img = io.imread("./path/to/image.png")

Since you already had the images read and wanted to do the conversion of the data yourself you can look at how the plugin authors did it here: https://github.com/scikit-image/scikit-image/blob/master/skimage/io/_plugins/pil_plugin.py