
How do I use image processing to determine the length of an object in an image?
22 views (last 30 days)
Show older comments
Malcolm Armstrong
on 7 Aug 2017
Commented: K G V Kalyan Sreenivas Kumar
on 9 Nov 2019
I have two images, each with a white object of different lengths on a black background. I know the starting length but I need to calculate the final length using image processing. I have attached the images and the histograms. Is there a way of using MatLab to calculate the length of the two images or should I work out the difference in length of the two?
EDIT: Since posting this I have began my experiments. The actual images I am trying to process are the tyres with white stripes on them. The white stripes skew and increase in length in y but all I am interested in is the length in x direction


0 Comments
Accepted Answer
Image Analyst
on 16 Aug 2017
See if this works for you. It assumes vertical white blob stripe.
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 short g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = '1k_100kph_10x5mm.JPG';
baseFileName = '1k_100kph_5x20mm.JPG';
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
grayImage = imread(fullFileName);
% 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.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% binarize the image.
binaryImage = imbinarize(grayImage);
% Make sure there is only one blob
binaryImage = bwareafilt(binaryImage, 1);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
axis image;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Label the image
labeledImage = bwlabel(binaryImage);
% Make measurements of bounding box
props = regionprops(labeledImage, 'BoundingBox');
width = props.BoundingBox(3);
height = props.BoundingBox(4);
% Display it with the box overlaid on it.
subplot(2, 2, 3);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Gray Scale Image with box overlaid');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
hold on;
rectangle('Position', props.BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
message = sprintf('The width = %f.\nThe height = %f', width, height);
uiwait(helpdlg(message));

If you want the box to be taller, then we'll have to fiddle with imbinarize, or preprocess the image, or come up with another algorithm.
3 Comments
Jay Mehta
on 24 Jan 2018
Edited: Jay Mehta
on 24 Jan 2018
What changes should I make to the above code to make it usable for 'n' number of blobs?
And
would you please explain this part of the code once again?
the part
props = regionprops(labeledImage, 'BoundingBox'); width = props.BoundingBox(3); height = props.BoundingBox(4);
I did play with the numbers 3 and 4. When I increased the 3 to 5 the code gave an error ("Index exceeds matrix dimensions"), which I did not understand completely. I understood that the matrix of the image that we have max chosen in the code above is a 4x4 matrix hence anything above this values gives an error. But I did not find any declaration or initialization of any such matrix ... as we are just using [] which I assume to accommodate the max possible matrix that can be formed by the image that we choose.
Hoping to get my concepts clear.
Regards, Conceptuality. JM
K G V Kalyan Sreenivas Kumar
on 9 Nov 2019
In which units are the answers in??
I need units of the object to be in mm. how can i do that?
More Answers (2)
Khyati Savakia
on 10 Aug 2017
Edited: Khyati Savakia
on 10 Aug 2017
You can use "regionprops" function with "BoundingBox" property to find the rectangle containing the white object.
For instance: if you have an image BW, stats = regionprops(BW,'BoundingBox'); This would return the x and y coordinates along with width and height of the white object.
MATLAB Documentation: https://www.mathworks.com/help/images/ref/regionprops.html
3 Comments
Chukwuebuka Ubah
on 3 Aug 2019
Please, I need a code that will help me get the waist and Hip circumference from an image or from camera directly. I would appreciate it.
Please help me
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!