# calculate area and parameter cell

47 views (last 30 days)
izyan hanum on 15 Mar 2015
Commented: Image Analyst on 28 Dec 2019 at 15:07
i have sputum image. after segmentation, i need to count the area and parameter. this is my coding. after that i dont know how to start calculate area of my image.please help me
%get image from file
image1 =image;
figure(1),subplot(3,3,1),imshow(image1),title('original image');
%to grayscale
red=image1(:,:,1);
green=image1(:,:,2);
blue=image1(:,:,3);
image_gray = rgb2gray(image);
figure (1),subplot (3,3,2), imshow(image_gray), title('grayscale');
%tobinary
level = graythresh(image_gray);
BW = im2bw (image_gray,level);
figure(1),subplot(3,3,3),imshow(BW), title('binary');
%closing
se = strel('disk',10);
afterclosing = imclose(BW,se);
figure(1), subplot (3,3,4), imshow(afterclosing), title('closing');
%opening
afteropen = imopen(afterclosing,se);
figure(1), subplot (3,3,5),imshow(afteropen), title('opening');

John BG on 10 Feb 2017
Edited: John BG on 11 Feb 2017
Hizyan Hanum Hi
Murk Hassan Memon Hi
If you want me to further develop any specific part of my answer just let me know.
1.
Capture
title('initial image');
2.
sometimes the variance between RGB layers shows a clear boundary
figure(5);varA=surf(var(double(A),0,3));
.
B=varA.CData;
B(B<1000)=0;
B(B>=1000)=255;
figure(6);imshow(B)
.
3.
RGB split
Because with the variance the cells look 'dehydrated', the RGB split shows
ColorList={'Red' 'Green' 'Blue'};
N=255;gr=0:1/(N-1):1;
figure(1);imshow(A);
cMap=zeros(N,3);cMap(:,1)=gr;
figure(2);hr=imshow(ind2rgb(A(:,:,1),cMap));title(ColorList{1});
% filtering greens
cMap=zeros(N,3);cMap(:,2)=gr;
figure(3);hg=imshow(ind2rgb(A(:,:,2),cMap));title(ColorList{2});
% filtering blues
cMap=zeros(N,3);cMap(:,3)=gr;
figure(4);hb=imshow(ind2rgb(A(:,:,3),cMap));title(ColorList{3});
C=hg.CData;
figure(7);imshow(C);
C=C(:,:,2);C=255*C;
C(C>150)=255;
C(C<150)=0;
figure(8);imshow(C);
.
4.
total area, in pixels
numel(C)
=
198660
area in pixels
numel(nonzeros(~C))
=
6172
same as
[cx,cy]=find(~C);
numel(cx)
=
6172
in percentage
100*numel(nonzeros(~C))/numel(C)
=
3.106815664955200
there is a 3.1% are of red tagged cells
5.
and following the MATLAB example using RGB to L*a*b and K-means clustering
figure(1);imshow(A)
cform = makecform('srgb2lab'); % RGB to L*a*b color space
lab_he = applycform(A,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3; % sort out with K-means clustering
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', 'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols); % label all pixels !?
figure(2);imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors % sub-images segmenting A by color
color = A;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
figure(3);imshow(segmented_images{1}), title('objects in cluster 1');
figure(4);imshow(segmented_images{2}), title('objects in cluster 2');
. If the tagging has caught all the cells of interest and only the cells of interest (assuming the small specks do not contain red tagging) then a more accurate area percentage would be
C=segmented_images{2};
C1=C(:,:,1);imshow(C1)
C1(C1>100)=255;C1(C1<100)=0;
100*numel(nonzeros(C1))/numel(C1)
=
2.281787979462398
thanks for time and attention, any feedback welcome awaiting answer
John BG

#### 1 Comment

murk hassan memon on 2 Mar 2018
well done .. thank u very much

Image Analyst on 15 Mar 2015
See my Image Segmentation Tutorial http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 and you'll know how to do things like:
labeledImage = bwlabel(afteropen);
measurements = regionprops(labeledImage, 'Area', 'Perimeter');
allAreas = [measurements.Area];
allPerimeters = [measurements.Perimeters];

Show 1 older comment
izyan hanum on 16 Mar 2015
i understand, now i change the variable image to sputum. its will more okay right.? so i need to do red blue green before grayscale or after grascale? im not understand with the color. can you help me.?
Image Analyst on 16 Mar 2015
I gave you a full script in your duplicate question.
izyan hanum on 17 Mar 2015
thank you so much :) i will try my best

murk hassan memon on 10 Feb 2017
I want code for the infected cells from blood sample images if any one have any idea regarding this topic then kindly help me out

Natalia Matviychuk on 27 Dec 2019 at 19:33
Anyone knows how to calculate the cytoplasm area (all area- nucleus area)??

Image Analyst on 27 Dec 2019 at 23:20
Did you try the image segmentation tutorial in my answer? If not, why not? It shows you how to use regionprops to measure areas.
Natalia Matviychuk on 28 Dec 2019 at 1:02
Yes, the tutorial is very nice, I understand how to measure all area of the coin but I wanted to measure a cytoplasm area(not all area of the cell). For example i have this image, and I need to measure the grey area
Image Analyst on 28 Dec 2019 at 2:39
I think you didn't try to adjust the thresholds, did you? See this code, which produces:
allreas =
3440 3987
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
% Check that user has the Image Processing Toolbox installed.
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a demo image.
folder = pwd
baseFileName = '37NT.bmp';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
fprintf('This image is RGB. I will change it to gray scale.\n');
grayImage = grayImage(:, :, 2);
end
% Display the original image.
subplot(1, 2, 1);
imshow(grayImage);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9], ...
'Name', 'Demo by Image Analyst', 'NumberTitle', 'Off');
% Get gray stuff only.
binaryImage = grayImage > 112 & grayImage < 240;
% Display the original image.
subplot(1, 2, 2);
imshow(binaryImage);
axis('on', 'image');
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize);
impixelinfo;
% Make area measurements.
props = regionprops(binaryImage, 'Area');
allreas = [props.Area]

Natalia Matviychuk on 28 Dec 2019 at 10:59
Thank you sooo much! And could you explain me please why did you used values 112 and 240 ?
binaryImage = grayImage > 112 & grayImage < 240;

#### 1 Comment

Image Analyst on 28 Dec 2019 at 15:07
I just took values that were half way between the gray and black, and the gray and white. I could have just said
binaryImage = grayImage == 235;
but that image didn't look like a read limage - it looked like computer graphics. Perhaps a synthetic image you made up for testing, and if so, a real image would not be just at one single gray level but would have a range of gray levels. So I took a range of gray levels to try to let it work should you ever get a real image. Of course you can adjust those values however you need to.