Measuring percentage of black pixels
7 次查看(过去 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?
0 个评论
回答(2 个)
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
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/885615/image.jpeg)
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!