How can i count the sum of the gray level value in a segmented region

1 view (last 30 days)
I was segmented an image with level set method now i should calculte the sum of all the gray level values in this region which is represented in blue in the picture below

Answers (1)

Rik
Rik on 10 Jun 2018
The code below should work
IM=double(imread('corn.tif',3))/255;
polygon_x=[109;142;166;178;155;131;112;124;125;116;102;82;67;74;90;109];
polygon_y=[159;147;163;191;220;239;227;206;189;180;185;199;191;166;158;159];
[X,Y]=meshgrid(1:size(IM,2),1:size(IM,1));
in=inpolygon(X(:),Y(:),polygon_x,polygon_y);
IM_part=IM;IM_part(~in)=0;
sum_of_masked_area=sum(IM_part(:));
figure(1),clf(1)
subplot(1,2,1)
imshow(IM)
hold on
plot(polygon_x,polygon_y)
title('original gray scale image')
subplot(1,2,2)
imshow(IM_part)
title(sprintf('sum of values: %.1f',sum_of_masked_area))

Community Treasure Hunt

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

Start Hunting!