GLCM stats with a mask

조회 수: 3 (최근 30일)
Donnell Perkins
Donnell Perkins 2020년 3월 12일
댓글: Walter Roberson 2020년 3월 14일
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
Walter Roberson
Walter Roberson 2020년 3월 12일
What is the error that you get?
Donnell Perkins
Donnell Perkins 2020년 3월 13일

댓글을 달려면 로그인하십시오.

채택된 답변

Donnell Perkins
Donnell Perkins 2020년 3월 13일
%%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)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 3월 12일
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
Donnell Perkins
Donnell Perkins 2020년 3월 13일
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 2020년 3월 14일
I should have used
first_unused = max(blackMaskedImage(:)) + 1;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by