필터 지우기
필터 지우기

Measuring percentage of black pixels

조회 수: 4 (최근 30일)
Aero Descerazes
Aero Descerazes 2022년 2월 5일
편집: Aero Descerazes 2022년 2월 6일
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.
Aero Descerazes
Aero Descerazes 2022년 2월 6일
@Simon Chan Thank you very much for the suggestion!

댓글을 달려면 로그인하십시오.


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