How to crop circular area and measure the ratio of black and white from the binarized image?

1 view (last 30 days)
Hello, I have to do a image processing and analysis of X-ray Diffraction Image of metal sample.
As you may see, I made it to binarizing the Image with command from 'image processing toolbox' provided by Matlab.
However, I have to find out the percentage of black area inside the circle for 20 photos that has same frames.
Therefore, I think I have to crop the circlular center of the image and calculate the pixel using bwarea function. But having trouble cropping the center part.
Is there any way to crop the same circular area automatically and calculate the ratio of black and white area?

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 29 Jul 2019
Edited: KALYAN ACHARJYA on 29 Jul 2019
Measure the ratio of black and white from the binarized image?
Here bw_image is a input binary image.
[rows colm]=size(bw_image);
white_pixels=sum(bw_image(:));
black_pixels=~bw_image;
black_pixels=sum(black_pixels(:));
result=bwareafilt(~bw_image,1,'Largest');
outer_black=sum(result(:));
% Area with respect to circle only
black_pixels_area=(black_pixels-outer_black)/((rows*colm)-outer_black);
fprintf('The Percentage of black pixels covered area is %.2f ',black_pixels_area);
fprintf('\n The ratio black/white is %.2f',black_pixels/white_pixels);
  2 Comments
Dongwon Kim
Dongwon Kim on 30 Jul 2019
Edited: Dongwon Kim on 30 Jul 2019
Thank you for the advice. So this one calculate the ratio of pixel between White and black only in the circular region, right? (Excluding the back background?)

Sign in to comment.

More Answers (1)

KSSV
KSSV on 29 Jul 2019
Edited: KSSV on 29 Jul 2019
I = imread('image.jpeg') ;
I1 = rgb2gray(I) ;
I1 = imcrop(I1) ; % crop the square part which exactly covers/ inscribes the circle
[y,x] = find(I1) ; % get white pixel locations
% Get circle
C = mean([x y]) ; % center
R = round(size(I1,1)/2) ; % RAdius
th = linspace(0,2*pi) ;
xc = C(1)+R*cos(th) ;
yc = C(2)+R*sin(th) ;
imshow(I1)
hold on
plot(xc,yc,'r')
% Get all points inside the circle
[nx,ny] = size(I1) ;
[X,Y] = meshgrid(1:nx,1:ny) ;
idx = inpolygon(X(:),Y(:),xc,yc) ;
% white pixles
n_white = nnz(I1(idx)==1) ;
n_black = nnz(I1(idx)==0) ;

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!