How to count the black pixels in a segmented section of image?

4 ビュー (過去 30 日間)
Tawsif Mostafiz
Tawsif Mostafiz 2021 年 8 月 4 日
コメント済み: Rik 2021 年 8 月 4 日
I am using this code to show a segmented image using active contour method.
iteration=50;
img=im2bw(imageArray,threshlod_value); %making it black&white
mask=roipoly(img);
bw=activecontour(img,mask,iteration,'Chan-Vese');
imshow(imageArray)
hold on
visboundaries(bw,'Color','r');
hold off
And the output is this:
I want to calculate the black pixels within the segmented boundary after it is turned black and white. How do I do that?

回答 (1 件)

Rik
Rik 2021 年 8 月 4 日
activecontour returns a logical array. That means you can count the black pixels by inverting and using sum (or nnz).
  2 件のコメント
Tawsif Mostafiz
Tawsif Mostafiz 2021 年 8 月 4 日
@Rik can you pleae elaborate? How do I write it in code?
Rik
Rik 2021 年 8 月 4 日
If you don't share the source image along with your code, why do you expect a turn-key solution?
Inverting a logical array is as simple as using the not operator on your variable. In Matlab you can either use the ~ or you can use the not function.
Counting the number of white pixels is a question of either counting the number of non-zero values (nnz), or using the implicit conversion of true to 1 and using sum.
sum(~bw,'all')
That's it.
If you want more explicit help, remember to share the source data.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by