Measuring percentage of black pixels

4 次查看(过去 30 天)
I have four ROIs labelled as a mask. I want to measure the number and percentage of black pixels (black pixels/ total ROI area) in the 'binary' image inside the individual ROIs defined above.
Second question - I have multiple 'binary' images to analyze but the ROIs will be the same. Any suggestions on batch processing?

回答(2 个)

Simon Chan
Simon Chan 2022-2-5
Try the following:
Variables 'dark_number' and 'dark_percent' reports the number and percentage of black pixels in each ROI respectively.
Each row refers to each binary image and each column refers to each ROI. So the number of columns are always 4 while row number varies
clear; clc;
[filename_roi,pathname_roi] = uigetfile('*.png','Select ROI png Files','MultiSelect', 'on');
if ~iscell(filename_roi)
filename_roi = {filename_roi};
end
Nroi = length(filename_roi);
ROI = cell(1,Nroi);
for r = 1:Nroi
ROI{1,r} = imread(fullfile(pathname_roi,filename_roi{r}));
end
%
[filename,pathname] = uigetfile('*.png','Select One or More png Files','MultiSelect', 'on');
if ~iscell(filename)
filename = {filename};
end
Nz = length(filename);
white_mask = cellfun(@nnz,ROI);
dark_number = zeros(Nz,Nroi);
for k = 1:Nz
I = imread(fullfile(pathname,filename{k}));
white_number = cellfun(@(x) sum((I & x),'all'),ROI);
dark_number(k,:) = white_mask - white_number;
end
dark_percent = 100*dark_number./white_mask
  5 个评论
Simon Chan
Simon Chan 2022-2-6
Actually, both results are already stored in table T1 (Number of Dark Pixel) and T2 (Percentage of Dark Pixel). You can just type T1 and T2 and results as follows. However, if you increase the number of ROIs continously, the same issue will happen again.
Then you have to think about how would you like to present your results. May be function fprintf suggested by yanqi liu is better if you have lots of ROIs.

请先登录,再进行评论。


yanqi liu
yanqi liu 2022-2-6
clc; clear all;
rois = {'https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/884800/ROI%201.png',...
'https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/884805/ROI%202.png',...
'https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/884810/ROI%203.png',...
'https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/884815/ROI%204.png'};
% make all your image filenames
filenames = {'https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/884795/binary.png',...
'https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/884795/binary.png'};
bws = [];
for i = 1 : length(rois)
bws{i} = im2bw(imread(rois{i}));
end
for i = 1 : length(filenames)
imi = im2bw(imread(filenames{i}));
% mask
black_pixels = length(find(imi.*bws{i}));
total_ROI_area = length(find(bws{i}));
rate = black_pixels/total_ROI_area;
fprintf('\n%d image---rate=%.2f',i,rate);
end
1 image---rate=0.72 2 image---rate=0.65

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by