3 views (last 30 days)
Show older comments
Andrea Labudzki on 22 Nov 2021
Commented: Andrea Labudzki on 1 Dec 2021
Accepted Answer: Image Analyst
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 Comments Show NoneHide None
Show NoneHide None
DGM on 22 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1847204
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.
Andrea Labudzki on 22 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1847259
I need it for every hole and wire, though I am able to omit certain parts of the image if they are problematic. I need it to work for various images. The user should be able to input their image and I want to eventually be able to provide analysis for whether the hole and wire sizes fulfill certain certification standards.
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Image Analyst on 22 Nov 2021
⋮
Open in MATLAB Online
- grid.png
- testGrayImage.m
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 Comments Show 5 older commentsHide 5 older comments
Show 5 older commentsHide 5 older comments
Andrea Labudzki on 22 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1847354
you are amazing! i'm going to try this out tomorrow and i'll provide some more feedback :D
Andrea Labudzki on 24 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1851289
Hi! This was a very, very helpful code for me. Thank you for taking the time to write it all out. I have two questions:
- imshow(grayImage, []); I read a bit about including these brackets [], but I'm not sure why they are used for the images here? What difference would it make to not include them?
- Your code returned the areas of all the blobs. I rather would need something like width and height. I know that there is some noise in the image and so the blobs are not exact rectangles, so in this case something like average width and height of each blob would be perfect (I can then omit some anomalies in the data analysis). How can I do that?
Image Analyst on 24 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1851479
Open in MATLAB Online
The [] scaled the image so that the max value shows up at max brightness on your screen (255) and the min value shows up at 0. If you don't have that then a uint8 image will show the min as the min and the max as the max, unscaled. So no brackets would show a min of 120 at 120 and a max of 130 at 130. Using brackets would show 120 at 0, and 130 at 255, making the low contrast image easier to see, while not actually changing any values in the variable itself (just in the display).
To get bounding boxes, try this:
props = regionprops(labeledImage, grayImage, 'Area', 'BoundingBox');
% Get bounding boxes.
bb = vertcat(props.BoundingBox);
widths = bb(:, 3);
heights = bb(:, 4);
If you use bounding boxes, make sure your grid lines are aligned with the edges of the image because tilted boxes would give bounding boxes larger than the actual box.
Andrea Labudzki on 24 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1851714
thank you! you have no idea how helpful you've been.
Andrea Labudzki on 1 Dec 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1863274
Hi, is it possible to extract distance between blobs?
Image Analyst on 1 Dec 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1863440
Open in MATLAB Online
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).
Andrea Labudzki on 1 Dec 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1593094-how-do-i-extract-size-in-terms-of-pixels-from-an-image#comment_1863590
thank you!
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABGraphicsImages
Find more on Images 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office