How do I extract size (in terms of pixels) from an image?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Andrea Labudzki
le 22 Nov 2021
Commenté : Andrea Labudzki
le 1 Déc 2021
Hello everyone. I have an image like the following:
It is converted to grayscale and binarized. I would like to extract information about the size of the white "holes" and the black "lines." I know that the image consists of a matrix with the 0 and 1 values, but how can I extract this information to figure out the size of each individual hole and line? (I've been staring at this problem for like a week and my brain is frozen...pls help).
2 commentaires
DGM
le 22 Nov 2021
Do you need the size of every individual hole and wire, or just some typical (e.g. mean) size? Do you need this to be automated for many images, or just this one?
You may run into problems making a robust automated solution for the "every hole" case unless you can come up with a good way to identify the debris.
Réponse acceptée
Image Analyst
le 22 Nov 2021
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'grid.PNG';
grayImage = imread(fileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the blue channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
subplot(2, 2, 2);
imhist(grayImage);
grid on;
low = 133;
high = 255;
% Interactively/visually select the threshold
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [low, high] = threshold(low, high, grayImage)
mask = grayImage > low;
% Fill holes.
mask = imfill(mask, 'holes');
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(mask, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
% Display the image.
subplot(2, 2, 4);
imshow(coloredLabelsImage);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
props = regionprops(labeledImage, grayImage, 'Area');
numberOfBlobs = size(props, 1);
% Get a list of all the areas
allAreas = [props.Area];
% Compute the mean area.
meanArea = mean(allAreas)
% Show the histogram.
figure
histogram(allAreas);
grid on;
xline(meanArea, 'Color', 'r', 'LineWidth', 3)
caption = sprintf('Distribution of %d Areas. Mean Area = %.2f pixels.', numberOfBlobs, meanArea)
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Area', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');
7 commentaires
Image Analyst
le 1 Déc 2021
Yes. If you have the stats toolbox, simply call pdist2() to get the distance between centroids
props = regionprops(labeledImage, grayImage, 'Area', 'BoundingBox', 'Centroid');
% Get bounding boxes.
bb = vertcat(props.BoundingBox);
widths = bb(:, 3);
heights = bb(:, 4);
centroids = vertcat(props.Centroid);
distancesMatrix = pdist2(centroids, centroids);
histogram(distancesMatrix);
There should be peaks in the distribution around the spacing (for neighbors up, down, left, right) and sqrt(2) times the spacing (for diagonal neighbors).
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!