GLCM stats with a mask
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Donnell Perkins
le 12 Mar 2020
Commenté : Walter Roberson
le 14 Mar 2020
I would like to evaluate the glcm features of the region of interest that in inside the mask. To only consider what is inside the mask I am attempting to multiply the original image by the mask and then take the glcm. I keep getting the same error.
I = rgb2gray(imread('5.jpg'))
imshow(I)
hFH = imfreehand()
xy = hFH.getPosition;
binaryImage = hFH.createMask();
blackMaskedImage = I;
blackMaskedImage(~binaryImage) = 0;
binaryImage=blackMaskedImage*blackMaskedImage(~binaryImage)
glcm_binaryImage=graycomatrix(binaryImage)
imshow(blackMaskedImage);
stats_binaryImage = graycoprops(glcm_binaryImage)
2 commentaires
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 12 Mar 2020
I = rgb2gray(imread('5.jpg'))
subplot(1,2,1)
imshow(I)
hFH = imfreehand()
xy = hFH.getPosition;
binaryImage = hFH.createMask();
blackMaskedImage = double(I);
first_unused = max(blackMaskedImage) + 1;
blackMaskedImage(~binaryImage) = first_unused;
subplot(1,2,2);
h = imshow(blackMaskedImage);
h.AlphaData = blackMaskedImage ~= first_unused;
drawnow();
glcm_binaryImage = graycomatrix(blackMaskedImage);
glcm_binaryImage = glcm_binaryImage(1:first_unused-1, 1:first_unused-1);
stats_binaryImage = graycoprops(glcm_binaryImage)
When you use the masks the way you were using them, you were introducing pixels that were 0, which resulted an more 0's being present in the image then there should be. graycomatrix was not going to ignore the 0s because the 0's are valid pixel values.
This code that I present changes the masked entries to a pixel value that would otherwise not exist. With that in hand you can calculate the co-occurance matrix, and then throw away the parts of the co-occurance matrix that relate to the mask pixel value.
I carried over the variable names you were using, but they are inaccurate in several cases. Your line
blackMaskedImage = I;
does not create a binary image: it copies the values from I, whatever data type they were, very likely uint8 for .jpg . If you think that your image is a binary image, then chances are that really it contains values 0 and 255, not 0 and 1, and your co-occurance matrix would include that entire 0 to 255 range. But as an outsider, I must assume that you are starting with an image with multiple shades that should not be binarized, becuase you did not binarize it.
2 commentaires
Walter Roberson
le 14 Mar 2020
I should have used
first_unused = max(blackMaskedImage(:)) + 1;
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!