MATLAB Answers

Differentiate between oval and circle shaped images using MATLAB

7 views (last 30 days)
Neelima Dahal
Neelima Dahal on 20 Sep 2021
Answered: Image Analyst on 21 Sep 2021 at 16:44
I am using the following code to try to differentiate two types of cells. One is circular, and the other is oval to oblong. I have about 100 images of each cell type, and have attached one each .jpg image to this question.
When I run the following code for Ck1.jpg, the rgb2gray image shows that my cell no longer has a continuous boundary. As a result, the code cannot tell what shape this cell has. Unfortunately, all the images I have are not high enough resolution. I was wondering if there was someway to interpolate the cell boundary based on however much is preserved after converting rgb image to gray.
I would greatly appreciate any help or feedback. Thank you!
clear; close all; clc;
ipath = 'D:\3) Candida yeast measurement\4) Multi Frequency Measurement - 100 Data Points\5) Ca versus Ck\';
fid = 'Ck1.jpg';
image_read = imread(strcat(ipath,fid));
figure (1)
image_RGB = imshow(image_read);
image_gray = rgb2gray(image_read);
B_W = imbinarize(image_gray);
figure (2)
image_B_W = imshow(B_W);
BW = bwareaopen(B_W,10);
figure (3)
image_BW = imshow(BW);
[B,L] = bwboundaries(BW,'holes');
figure (6)
imshow(label2rgb(L,@jet,[.5 0 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2),boundary(:,1),'w','LineWidth',2)
end
stats = regionprops(L,'Area','Centroid');
threshold = 0.95;
% loop over the boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
% compute the roundness metric
metric = 4*pi*area/perimeter^2;
% display the results
metric_string = sprintf('%2.2f',metric);
% mark objects above the threshold with a black circle
if metric > threshold
metric = 0;
% centroid = stats(k).Centroid;
% plot(centroid(1),centroid(2),'ko');
end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','r',...
'FontSize',14,'FontWeight','bold')
end

Answers (2)

Simon Chan
Simon Chan on 20 Sep 2021
You may try function bwconvhull by adding the following 2 lines.
However, I am not sure how robust it is for other pictures.
B_W = imbinarize(image_gray);
CH = bwconvhull(~B_W); % use function bwconvhull
B_W = ~CH; % Convert back to B_W
  1 Comment
Neelima Dahal
Neelima Dahal on 20 Sep 2021
Thank you for responding to my question. While your solution works perfectly for Ck1.jpg file, it does not for the other attached image Ca1.jpg. I also tried it on other oval cell images I have, but it doesn't work for them either.

Sign in to comment.


Image Analyst
Image Analyst on 21 Sep 2021 at 16:44
Assuming you can get a mask for the blobs, use the 'Eccentricity' option in regionprops
props = regionprops(mask, 'Eccentricity');
Alternatively use bwferet().

Community Treasure Hunt

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

Start Hunting!