Determine the percentage of black pixels within a circle

조회 수: 1 (최근 30일)
Vance Blake
Vance Blake 2020년 5월 18일
편집: Vance Blake 2020년 5월 18일
Hello, I am trying to determine the total number of pixels within the blue circle (radius = 80 dimensionless units) and the percentage of them that are black. I have multiple files that I would like to analyze, and I wanted to code a way to read in each picture in the folder one by one and then store each image name with its coresponding percentage of black pixels in a matrix. The image sizes are not standardized since they were created using the snipping tool
I have tried to implement the methods in shown in the solution links but each one is more piece of what I need and Im struggling to put them all together. Any help would be greatly appreciated.
pathname = uigetdir;
allfiles = dir(fullfile(pathname,'*.png'));
image_percent = [];
for i=1:size(allfiles,1)
x=imread([pathname '\\' allfiles(i).name]);
image_percent=[image_percent; x]; % storage
end
  댓글 수: 6
darova
darova 2020년 5월 18일
Do you know where the center circle is?
A = imread('image.png');
I = im2bw(A,0.1);
[m,n] = size(I);
[x,y] = ndgrid(1:m,1:n);
I1 = hypot(x-mean(x(:)),y-mean(y(:))) < 150;
imshowpair(I,I1)
Vance Blake
Vance Blake 2020년 5월 18일
편집: Vance Blake 2020년 5월 18일
Hi darova the center of the blue circle is the coordinates [0,0] on the plot. I don't know how that translates to the pixels of the png file itself though

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

답변 (1개)

Image Analyst
Image Analyst 2020년 5월 18일
Please post the original image, not a screenshot. Is the actual image a real gray scale image, or is it that RGB computer graphics diagram? Then, how are you getting the black and white (binary, logical) image from the original image?
Anyway, once you have the image segmented so that you have a mask with only black pixels and white pixels, then you can use sum() and nnz() to do the count. Assume circleMask is the mask of the overall, outer circle, and mask is your segmented, binary image of stuff inside you're interested in, then
numPixelsInCircleMask = sum(circleMask(:))
numWhitePixelsInMask = nnz(mask)
numBlackPixelsInMask = nnz(~mask) % or (numPixelsInCircleMask - numWhitePixelsInMask)
whiteAreaFraction = numWhitePixelsInMask / numWhitePixelsInMask
blackAreaFraction = 1 - whiteAreaFraction
  댓글 수: 2
Vance Blake
Vance Blake 2020년 5월 18일
편집: Vance Blake 2020년 5월 18일
Hi Image Analyst, I attached the images when I was rying to post last night but my computer froze and forced me to restart the question and now I get a warning saying that I cant attach them since Ive reached my daily upload limit. The images are png files that i stored after snipping them from the matlab plot window.
To get the black and white image I was gonna use imbinazie which i found using darvoa's suggestion of im2bw https://www.mathworks.com/help/images/ref/imbinarize.html
Is there a way to do what you shown this using a for loop (I have 67 total images) and then storing all the information in an array or is it a manual process?
Also how do I segment the image, Ive looked at your demo function on the file exchange but Im unclear on what steps and lines of code apply to my situation?

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

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by