How to count white pixels in Watershed segmented image?

조회 수: 1 (최근 30일)
Tawsif Mostafiz
Tawsif Mostafiz 2021년 8월 4일
댓글: Image Analyst 2021년 8월 5일
I am using watershed method to this image:
And got the output:
Now I want to convert the segmented part within the yellow boundary to black and white, and calculate its white and black pixels within the boundary. How can I do it?
The part of the code is as follows:
[B,L]=bwboundaries(tumor,'noholes'); %draw boundaries around the tumor
imshow(image);
hold on
for i=1:length(B)
plot(B{i}(:,2),B{i}(:,1), 'y' ,'linewidth',1.45);
end
title('Segmented Tumor');
hold off;

채택된 답변

Image Analyst
Image Analyst 2021년 8월 5일
What is the intensity that distinguishes between your "black" and "white"? 128? Don't use image as the name of your variable -- it's already the name of a built-in function.
Anyway to get the pixel values for blob in L:
pixelValues = grayScaleImage(L == 1); % A 1-D vector of all the pixel values in the region.
% Then sum up black and white
threshold = 128; % Whatever you want.
numBlackPixels = sum(pixelValues < threshold)
numWhitePixels = sum(pixelValues >= threshold)
  댓글 수: 2
Tawsif Mostafiz
Tawsif Mostafiz 2021년 8월 5일
편집: Tawsif Mostafiz 2021년 8월 5일
@Image Analyst I have changed the code a little bit for better understanding. I have converted it to black and white using following:
bw=im2bw(grayScaleImage,0.15); %0.15 is the threshold value
[B,L]=bwboundaries(tumor,'noholes'); %draw boundaries around the tumor
imshow(bw);
hold on
for i=1:length(B)
plot(B{i}(:,2),B{i}(:,1), 'r' ,'linewidth',1.45);
end
title('Segmented Tumor');
hold off;
Now, the output is following:
Now, I want to count the white pixels and black pixels within the red border. Extremely sorry for being vague previously. How do I do that?
Image Analyst
Image Analyst 2021년 8월 5일
Try this:
thisBoundary = B{1}; % Whatever boundary you want.
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
redMask = poly2mask(x, y, rows, columns);
pixelsInRedMask = binaryImage(redMask);
numBlack = sum(pixelsInRedMask == 0);
numWhite = sum(pixelsInRedMask == 1);

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

추가 답변 (1개)

darova
darova 2021년 8월 4일
Here is an example with using bwlabel
A = zeros(100);
t = linspace(0,2*pi,1000); % a lot of points
[x,y] = pol2cart(t,1); % circle coordinates
ind = sub2ind(size(A),round(y*15)+30,round(x*25)+50); % convert coordinates into [rows,cols]
A(ind) = 1; % fill [rows,cols] with ones
A1 = imdilate(A,ones(2)); % make line thicker (bwlabel doesn't work for thin lines)
[B,n] = bwlabel(~A1); % separate each region
imshow(B,hot(2)) % show separated region
line(x*25+50,y*15+30) % show original curve

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by