GLCM stats with a mask

3 views (last 30 days)
Donnell Perkins
Donnell Perkins on 12 Mar 2020
Commented: Walter Roberson on 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 Comments
Walter Roberson
Walter Roberson on 12 Mar 2020
What is the error that you get?

Sign in to comment.

Accepted Answer

Donnell Perkins
Donnell Perkins on 13 Mar 2020
%%Working Code
fontSize = 20;I= rgb2gray(imread('C:\Users\x\Desktop\5.jpg'))
subplot(1, 2, 1);
imshow(I);
title('Original Image', 'FontSize', fontSize);
subplot(1, 2, 2);
imshow(I);
title('Draw Region Of Interest', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')subplot(1, 2, 2);
hFH = imfreehand()
maskImage = hFH.createMask();subplot(1, 2, 2);blackMaskedImage = I;
blackMaskedImage(~maskImage) = NaN;imshow(blackMaskedImage);title('Masked Image', 'FontSize', fontSize);maskedImage = I.* cast(maskImage, class(I));glcm_maskedImage=graycomatrix(maskedImage)
stats_maskImage = graycoprops(glcm_maskedImage)

More Answers (1)

Walter Roberson
Walter Roberson on 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 Comments
Donnell Perkins
Donnell Perkins on 13 Mar 2020
Thank you so much for explaining my error. I understand what I did wrong now. But I am still continuing to get an error when I try to define anything with (~binaryImage) in it. It has happened to me several times while troubleshooting as well
Walter Roberson
Walter Roberson on 14 Mar 2020
I should have used
first_unused = max(blackMaskedImage(:)) + 1;

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!