Although mahotas
is also an excellent computer vision library, there's no need to stop using skimage
to do this.
What is necessary, as @Tonechas has pointed out, is to set those NaN values to an integer, since np.nan
has type float
and the greycomatrix
function requires an array of integers.
The easiest option would be setting those NaN
's to zero but, if you already have zero values in your pixels and don't want to mix them, you can choose any other constant. After that, all you have to do is filter that chosen value (once again, generally zero) out of the GLCM.
To understand what this means, let's see what skimage
tells us about the output of the greycomatrix
function:
4-D ndarray
[...] The value P[i,j,d,theta] is the number of times that grey-level j occurs at a distance d and at an angle theta from grey-level i. If normed is False, the output is of type uint32, otherwise it is float64. The dimensions are: levels x levels x number of distances x number of angles.
In other words, the first two dimensions of the array define a matrix that tells us how many times two different values are a certain distant apart. Note that the GLCM does not keep the shape of the input array. Those rows and columns are telling us how the values relate.
Knowing this, it's easy to filter out the values outside our ROI (imagine we've set those NaN's to zero):
glcm = greycomatrix(img, [1], [0]) # Calculate the GLCM "one pixel to the right"
filt_glcm = glcm[1:, 1:, :, :] # Filter out the first row and column
Now you could easily calculate the Haralick properties of your filtered GLCM. For example:
greycoprops(filt_glcm, prop='contrast')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…