How to calcutate the area of each cell?

How do I calculate the area of each cell(marked by the polygon shaped white boundary)?

Respuestas (1)

Image Analyst
Image Analyst el 25 de Mayo de 2023

0 votos

How were the white boundaries obtained and in what form are they? You can probably use regionprops() if you can make a mask of your white boundaries.
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.

7 comentarios

Thank you for your response. I tried to implement your image segmentation tutorial. I am having an issue as you can see that the boundaries defined by the red color doesn't correspond similarly to the original diagram(where the boundaries are defined by white color). Any suggestions would be appreciated. I tried the code below:
a = rgb2gray(imread("thumbnail_image002.png"));
figure,imshow(a)
thresholdValue = 100;
binaryImage = a > thresholdValue;
props = regionprops(binaryImage, 'all');
numberOfBlobs = numel(props);
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is x.
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
for k = 1 : numberOfBlobs % Loop through all blobs.
blobArea = props(k).Area; % Get area.
fprintf(1,'#%2d %17.1f %11.1f %8.1f %8.1f %8.1f % 8.1f\n', k, blobArea);
end
Image Analyst
Image Analyst el 25 de Mayo de 2023
Again, attach "thumbnail_image002.png", which I assume is the original gray scale image, and code you have for generating the white borders.
Warid Islam
Warid Islam el 25 de Mayo de 2023
This is the original image. The white boundary was in the original image by default. I didn't do anything to generate it.
Image Analyst
Image Analyst el 25 de Mayo de 2023
Editada: Image Analyst el 25 de Mayo de 2023
I don't know. The bright white band running through the voronoi grid complicates finding the grid alone. I don't have time to try to segment out the grid alone. It could be quite complicated. I suggest you go back earlier in the process to see if there is something you can do to get the grid structure alone, like use UV, SWIR, or hyperspectral imaging.
Maybe try ridge-finding filters like Hessian, Frangi, or B-COSFIRE. You can find them in the File Exchange:
Warid Islam
Warid Islam el 13 de Jun. de 2023
Thank you for the links. Is it possible to do manual segmentation of each region and calculate the areas separately?
I tried the code below from your Image Segmentation Tutorial:
grayImage = rgb2gray(imread('flox2 (1).tif'));
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', 12);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = drawfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
% Now make it smaller so we can show more images.
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', 12);
% Display the freehand mask.
subplot(2, 3, 2);
imshow(binaryImage);
axis on;
title('Binary mask of the region', 'FontSize', 12);
% Label the binary image and computer the centroid and center of mass.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(binaryImage, grayImage, ...
'area', 'Centroid', 'WeightedCentroid', 'Perimeter');
area = measurements.Area
centroid = measurements.Centroid
centerOfMass = measurements.WeightedCentroid
perimeter = measurements.Perimeter
% Calculate the area, in pixels, that they drew.
numberOfPixels1 = sum(binaryImage(:))
% Another way to calculate it that takes fractional pixels into account.
numberOfPixels2 = bwarea(binaryImage)
% Get coordinates of the boundary of the freehand drawn region.
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
subplot(2, 3, 1); % Plot over original image.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);
drawnow; % Force it to draw immediately.
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage = grayImage;
burnedImage(binaryImage) = 255;
% Display the image with the mask "burned in."
subplot(2, 3, 3);
imshow(burnedImage);
axis on;
caption = sprintf('New image with\nmask burned into image');
title(caption, 'FontSize', fontSize);
% Mask the image and display it.
% Will keep only the part of the image that's inside the mask, zero outside mask.
blackMaskedImage = grayImage;
blackMaskedImage(~binaryImage) = 0;
subplot(2, 3, 4);
imshow(blackMaskedImage);
axis on;
title('Masked Outside Region', 'FontSize', 12);
% Calculate the mean
meanGL = mean(blackMaskedImage(binaryImage));
% Put up crosses at the centriod and center of mass
hold on;
plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 30, 'LineWidth', 2);
plot(centerOfMass(1), centerOfMass(2), 'g+', 'MarkerSize', 20, 'LineWidth', 2);
% Now do the same but blacken inside the region.
insideMasked = grayImage;
insideMasked(binaryImage) = 0;
subplot(2, 3, 5);
imshow(insideMasked);
axis on;
title('Masked Inside Region', 'FontSize', fontSize);
% Now crop the image.
leftColumn = min(x);
rightColumn = max(x);
topLine = min(y);
bottomLine = max(y);
width = rightColumn - leftColumn + 1;
height = bottomLine - topLine + 1;
croppedImage = imcrop(blackMaskedImage, [leftColumn, topLine, width, height]);
% Display cropped image.
subplot(2, 3, 6);
imshow(croppedImage);
axis on;
title('Cropped Image', 'FontSize', 12);
% Put up crosses at the centriod and center of mass
hold on;
plot(centroid(1)-leftColumn, centroid(2)-topLine, 'r+', 'MarkerSize', 30, 'LineWidth', 2);
plot(centerOfMass(1)-leftColumn, centerOfMass(2)-topLine, 'g+', 'MarkerSize', 20, 'LineWidth', 2);
% Report results.
message = sprintf('Mean value within drawn area = %.3f\nNumber of pixels = %d\nArea in pixels = %.2f\nperimeter = %.2f\nCentroid at (x,y) = (%.1f, %.1f)\nCenter of Mass at (x,y) = (%.1f, %.1f)\nRed crosshairs at centroid.\nGreen crosshairs at center of mass.', ...
meanGL, numberOfPixels1, numberOfPixels2, perimeter, ...
centroid(1), centroid(2), centerOfMass(1), centerOfMass(2));
msgbox(message);
I am trying to manually draw the regions using drawfreehand() function and calculate the individual areas of the regions separately. However, I am unable to draw more than one region together from the image. Any suggestions would be appreciated.
Image Analyst
Image Analyst el 14 de Jun. de 2023
See my attached demo.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Preguntada:

el 25 de Mayo de 2023

Comentada:

el 14 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by