Filter löschen
Filter löschen

Measuring percentage of black pixels

4 Ansichten (letzte 30 Tage)
Aero Descerazes
Aero Descerazes am 5 Feb. 2022
Bearbeitet: Aero Descerazes am 6 Feb. 2022
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?

Antworten (2)

Simon Chan
Simon Chan am 5 Feb. 2022
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 Kommentare
Simon Chan
Simon Chan am 6 Feb. 2022
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.
Aero Descerazes
Aero Descerazes am 6 Feb. 2022
@Simon Chan Thank you very much for the suggestion!

Melden Sie sich an, um zu kommentieren.


yanqi liu
yanqi liu am 6 Feb. 2022
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

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by