フィルターのクリア

How to find the percentage/number of pixels in each class of an indexed image?

2 ビュー (過去 30 日間)
I have a cropped indexed image with 7 classes and I have to find the percentage/number of pixels in each class.
fcls = readgeoraster('Cropped_FCLS.tif');
fcls(fcls==0)=NaN;
figure, imagesc(fcls)
colormap(jet(numel(classNames))); %% the classNames are a part of a larger code
colorbar('Ticks',1.5:0.9:numel(classNames),'TickLabels',classNames);
numberOfBins = max(fcls(:));
countsPercentage = 100 * hist(fcls(:), numberOfBins) / numel(fcls);
The above code is giving me incorrect values as they do not add upto 100. I values I am getting are :
countsPercentage =
0.1541 1.9319 0 0.0006 1.0463 0.0362 0.1464
I am unable to make the value of the background pixels zero as well (shown as blue in the image) which I think is the reason for not getting correct results for percentage.

採用された回答

Image Analyst
Image Analyst 2021 年 7 月 2 日
If you don't want to consider index 0 (class 0), just make a histogram and don't consider that
% fcls = 0:10 % Test data
maxIndex = max(fcls(:))
% Define histogram edges.
edges = 0 : maxIndex + 1
% Get counts of all classes, including 0.
counts = histcounts(fcls, edges)
% Let's ignore class 0
counts = counts(2:end)
% Normalize
countsPercentage = 100 * counts / sum(counts)
  2 件のコメント
Anwesha Sharma
Anwesha Sharma 2021 年 7 月 4 日
This is perfect. Thanks a lot.
If I may add another question here, is it possible to make the background pixel values zero in the image(I mean make the background either white or black)? If so, please suggest how should I do it? Or should I post it as another question?
Thanks a lot.
Image Analyst
Image Analyst 2021 年 7 月 4 日
Yes, you need to set the first row of the colormap to 0 for black or 1 for white:
maxIndex = max(fcls(:))
cmap = jet(maxIndex); % Jet colormap
% Make 0 data value show up as black
cmap(1, :) = 0;
imshow(fcls, 'Colormap', cmap);
colorbar;

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

その他の回答 (1 件)

Chunru
Chunru 2021 年 7 月 2 日
Remove the nans before the line 'numberOfBins = max(fcls(:))'. This way the hist will not count on nans.
fcls = fcls(:); fcls = fcls(~isnan);
  1 件のコメント
Anwesha Sharma
Anwesha Sharma 2021 年 7 月 4 日
Thanks a lot. However, fcls = fcls(~isnan); showed as 'not enough arguments'.

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

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by