Caliginous
Caliginous

Reputation: 1

Region growing with the watershed transform

I am trying out the code by adfoucart for Region growing with the watershed transform but I ran into some errors when identifying the markers for the image.

from skimage.filters import rank,gaussian
from skimage.morphology import disk
from skimage.feature import peak_local_max

def get_markers(img2, indices=False):
    im_ = gaussian(img2, sigma=4)
    gradr = rank.gradient(im_[:,:,0],disk(5)).astype('int')
    gradg = rank.gradient(im_[:,:,1],disk(5)).astype('int')
    gradb = rank.gradient(im_[:,:,2],disk(5)).astype('int')
    grad = gradr+gradg+gradb
    
    return peak_local_max(grad.max()-grad,threshold_rel=0.5, min_distance=60,indices=indices),grad

markers,grad = get_markers(img2, True)
plt.figure()
plt.imshow(grad, cmap=plt.cm.gray)
plt.plot(markers[:,1],markers[:,0],'b+')
plt.show()

and I am receiving this error.

IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17316/2204442073.py in <module>
     12     return peak_local_max(grad.max()-grad,threshold_rel=0.5, min_distance=60,indices=indices),grad
     13 
---> 14 markers,grad = get_markers(img2, True)
     15 plt.figure()
     16 plt.imshow(grad, cmap=plt.cm.gray)

~\AppData\Local\Temp/ipykernel_17316/2204442073.py in get_markers(img2, indices)
      5 def get_markers(img2, indices=False):
      6     im_ = gaussian(img2, sigma=4)
----> 7     gradr = rank.gradient(im_[:,:,0],disk(5)).astype('int')
      8     gradg = rank.gradient(im_[:,:,1],disk(5)).astype('int')
      9     gradb = rank.gradient(im_[:,:,2],disk(5)).astype('int')

IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed

Any help will be appreciated thanj you!

Upvotes: 0

Views: 93

Answers (1)

Adrien Foucart
Adrien Foucart

Reputation: 56

You are probably trying to run the code on a grayscale image, which will only have 2 dimensions (height and width), while the code was written expecting an RGB image with 3 dimensions (height, width and color channel).

On a grayscale image, the lines:

gradr = rank.gradient(im_[:,:,0],disk(5)).astype('int')
gradg = rank.gradient(im_[:,:,1],disk(5)).astype('int')
gradb = rank.gradient(im_[:,:,2],disk(5)).astype('int')
grad = gradr+gradg+gradb

Could be simply replaced by:

grad = rank.gradient(im_, disk(5))

Upvotes: 1

Related Questions