How do I measure a distance in real world with a picture in matlab?

9 views (last 30 days)
first i have to convert pixels to cm , than i have the length between to points in cm . (in the picture) but in the real world , how many cm between the same point ? I do not know !!!

Accepted Answer

Image Analyst
Image Analyst on 9 Jan 2014
Edited: Image Analyst on 11 Jan 2014
Sara, you need to spatially calibrate. Run my attached demo and it will show you how.
[EDIT] Attached updated spatial_calibration_demo.m
  12 Comments
Joachim Huet
Joachim Huet on 20 Sep 2017
I would like to open this topic again, is there a way to do this calibration phase automatically ? Without needing the user to draw a line with known value !?
Image Analyst
Image Analyst on 20 Sep 2017
Of course. For example I automatically calibrate some systems by (1) finding a 4 inch square filter paper, and (2) by using the known dimensions of the X-Rite Color Checker chart. All you need is a known real world distance of some thing , and some way to find that thing in the image and measure the distance in pixels (like by thresholding or whatever...).

Sign in to comment.

More Answers (1)

Caroline David
Caroline David on 4 Feb 2018
Can someone show me the code for length measurements of an image in pixels. Most preferred in mm
  9 Comments
jue xi
jue xi on 26 Apr 2018
Edited: jue xi on 26 Apr 2018
i used this coding, now i want to convert the width in cm.. and i have read and try out the calibration demo and still got no clue on how to convert it to cm. </matlabcentral/answers/uploaded_files/114879/Capture.PNG>
if true
% code
folder=('C:\Users\user\MATLAB');
baseFileName=('img2copy.jpg');
fullFileName=fullfile(folder,baseFileName);
format long g;
format compact;
fontSize = 20;
%IMAGE SEGMENTATION
img=imread(fullFileName);
img=rgb2ycbcr(img);
for i=1:size(img,1)
for j= 1:size(img,2)
cb = img(i,j,2);
cr = img(i,j,3);
if(~(cr > 132 && cr < 173 && cb > 76 && cb < 126))
img(i,j,1)=235;
img(i,j,2)=128;
img(i,j,3)=128;
end
end
end
img=ycbcr2rgb(img);
subplot(2,2,1);
image1=imshow(img);
axis on;
title('Skin Segmentation', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%SEGMENTED IMAGE TO GRAYIMAGE
grayImage=rgb2gray(img);
subplot(2,2,2);
image2=imshow(grayImage);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%GRAY TO BINARY IMAGE
binaryImage = grayImage < 245;
subplot(2, 2, 3);
axis on;
image3=imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Label the image
labeledImage = bwlabel(binaryImage); % label the connected components in an image and assigning each one a unique label
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
% Let's extract the second biggest blob - that will be the hand.
allAreas = [measurements.Area];
[sortedAreas, sortingIndexes] = sort(allAreas, 'descend');
handIndex = sortingIndexes(2); % The hand is the second biggest, face is biggest.
% Use ismember() to extact the hand from the labeled image.
handImage = ismember(labeledImage, handIndex);
% Now binarize
handImage = handImage > 0;
% Display the image.
subplot(2, 2, 4);
image4=imshow(handImage, []);
axis on;
axis image;
title('Hand Image', 'FontSize', fontSize);
% Label the image
labeledImage = bwlabel(handImage); % label the connected components in an image and assigning each one a unique label
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
% Make measurements of bounding box
props = regionprops(labeledImage, 'BoundingBox');
width = props.BoundingBox(3);
height = props.BoundingBox(4);
hold on;
rectangle('Position', props.BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
message = sprintf('The width = %f.\nThe height = %f', width, height);
uiwait(helpdlg(message));

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!