I have been using the SLIC implementation of skimage to segment images in superpixels. I would like to use GLCMs to extract additional features from these superpixels for a classification problem. These superpixels are not rectangular. In MATLAB you can set pixels to NaN and they will be ignored by the algorithm (link). I could use this to make bounding boxes around the superpixels and then just setting the unused pixels to NaN.
The greycomatrix function in skimage does not work entirely the same as the MATLAB implementation however. When setting pixels to NaN the function fails on an assert to check if all values are larger than 0.
Is there a Python implementation available which is able to work with nonrectangular ROIs?
Although
mahotasis also an excellent computer vision library, there's no need to stop usingskimageto do this.What is necessary, as @Tonechas has pointed out, is to set those NaN values to an integer, since
np.nanhas typefloatand thegreycomatrixfunction 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
skimagetells us about the output of thegreycomatrixfunction: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):
Now you could easily calculate the Haralick properties of your filtered GLCM. For example: