0

I tried out the following example. This should show image processing results.

from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from scipy import misc
import numpy as np
import cv2

from skimage.morphology import watershed, disk
from skimage import data
from skimage.filters import rank
from skimage.util import img_as_ubyte

from skimage import io; io.use_plugin('matplotlib')

image = img_as_ubyte('imagepath.jpg')

# denoise image
denoised = rank.median(image, disk(2))

# find continuous region (low gradient -
# where less than 10 for this image) --> markers
# disk(5) is used here to get a more smooth image
markers = rank.gradient(denoised, disk(5)) < 10
markers = ndi.label(markers)[0]

# local gradient (disk(2) is used to keep edges thin)
gradient = rank.gradient(denoised, disk(2))

# process the watershed
labels = watershed(gradient, markers)

# display results
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8),
                         sharex=True, sharey=True)
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray, interpolation='nearest')
ax[0].set_title("Original")

ax[1].imshow(gradient, cmap=plt.cm.nipy_spectral, interpolation='nearest')
ax[1].set_title("Local Gradient")

ax[2].imshow(markers, cmap=plt.cm.nipy_spectral, interpolation='nearest')
ax[2].set_title("Markers")

ax[3].imshow(image, cmap=plt.cm.gray, interpolation='nearest')
ax[3].imshow(labels, cmap=plt.cm.nipy_spectral, interpolation='nearest', alpha=.7)
ax[3].set_title("Segmented")

for a in ax:
    a.axis('off')

fig.tight_layout()
plt.show()

I get the follofing error.

Traceback (most recent call last):
  File "/home/workspace/calculate_watershed.py", line 15, in <module>
    image = img_as_ubyte('koralle0.jpg')
  File "/home/workspace/venv/lib/python3.5/site-packages/skimage/util/dtype.py", line 409, in img_as_ubyte
    return convert(image, np.uint8, force_copy)
  File "/home/workspace/venv/lib/python3.5/site-packages/skimage/util/dtype.py", line 113, in convert
    .format(dtypeobj_in, dtypeobj_out))
ValueError: Can not convert from <U12 to uint8.

The path to the image is a valued one. Do You have any idea how to solve this problem? Thanks in advance

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66

1 Answers1

1

The problem is that the ndarray returned from your image has dtype <U12 which cannot be converted to dtype uint8. To check the dtype of your image file, convert it to a numpy array. I get a <U38 dtype for my image:

np.array('CAPTURE.jpg')
#array('Capture.JPG', dtype='<U38')

You should first read the image with skimage.io.imread(image_path). This will return an ndarray of MxN, MxNx3 or MxNx4. Then, reshape the resultant ndarray to 2D if its 3D or 4D. This conversion is required because skimage.filters.rank.median(image) accepts an image ndarray of 2D shape. In the following code, I've used my sample image to perform these steps before passing to img_as_ubyte(sk_image). The rest of the code remains the same.

from skimage.io import imread
#<---code--->
sk_image = imread('CAPTURE.jpg') #read the image to convert to skimage ndarray
sk_image = sk_image.transpose(1,0,2).reshape(130,-1) #convert to 2D array
image = img_as_ubyte(sk_image) #Convert image to 8-bit unsigned integer format.
#<---code--->  

I get the following images:

skimages

You should consider the following points:

  • Check the shape of the image array returned from imread: After reading the image with sk_image = imread('CAPTURE.jpg'), check the shape of the array with sk_image.shape. For my image, I get the shape as (74, 130, 3) which shows its a 3D array.
  • To reshape to 2D, first get the strides with sk_image.strides. For my image, I get (390, 3, 1), then transpose with sk_image.transpose(1,0,2). You can also check the strides after transposing and you will notice the values have been swapped sk_image.transpose(1,0,2).strides: (3, 390, 1). Then, use reshape: sk_image.transpose(1, 0, 2).reshape(130,-1) to reshape to a 2D array. You will notice that the reshape dimensions have been roughly calculated from the stride value(390/2).

P.S: You can read more about 3D to 2D reshaping of numpy arrays here.

amanb
  • 5,276
  • 3
  • 19
  • 38