how to calculate the distance between different pixels

7 views (last 30 days)
mn.JPG

Accepted Answer

Image Analyst
Image Analyst on 9 Dec 2019
Try imdistline() or improfile() to get the distance either in pixels or real world units (if you've used XDAta when you called imshow()).
Also see my attached spatial calibration demo.
  1 Comment
meizaki fatihin
meizaki fatihin on 15 Jan 2020
Thanks a lot sir May Allah bless you. In shaa allah
Sir. can I determine the points between the 2 binary images. but I want the determination of this point automatically. so that later I can measure the distance between these points. thanks again for your help sir. warm greetings from Indonesia

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 15 Jan 2020
Try this to get the mean separation:
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 = 15;
%===============================================================================
% Read in demo image.
folder = pwd;
baseFileName = '20200115_200524.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
binaryImage = imread(fullFileName);
% Display the image.
hFig = figure;
subplot(2, 3, 1);
imshow(binaryImage, []);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% 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(binaryImage)
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(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
binaryImage = binaryImage(:, :, 1); % Take red channel.
else
binaryImage = binaryImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Need to convert it into a logical (binary) image.
binaryImage = binaryImage > 128;
% Take the two largest blobs only
binaryImage = bwareafilt(binaryImage, 2);
% Fill any holes.
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 3, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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;
% Get separate images for the upper and lower blob.
labeledImage = bwlabel(binaryImage);
props = regionprops(labeledImage, 'Centroid');
if props(1).Centroid(2) < props(2).Centroid(2)
% Blob 1 is higher
upperMask = ismember(labeledImage, 1);
lowerMask = ismember(labeledImage, 2);
else
% Blob 2 is higher
upperMask = ismember(labeledImage, 2);
lowerMask = ismember(labeledImage, 1);
end
% Display the images.
subplot(2, 3, 3);
imshow(upperMask, []);
title('Upper Mask', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
subplot(2, 3, 4);
imshow(lowerMask, []);
title('Lower Mask', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Find the top and bottom lines.
x = 1 : columns;
topCurve = zeros(1, columns);
bottomCurve = zeros(1, columns);
separation = zeros(1, columns);
for col = 1 : columns
thisRow = find(upperMask(:, col), 1, 'last');
if ~isempty(thisRow)
topCurve(col) = thisRow;
end
end
for col = 1 : columns
thisRow = find(lowerMask(:, col), 1, 'first');
if ~isempty(thisRow)
bottomCurve(col) = thisRow;
end
separation(col) = abs(bottomCurve(col) - topCurve(col));
end
subplot(2, 3, 2);
hold on
plot(x, topCurve, 'r-', 'LineWidth', 3);
plot(x, bottomCurve, 'r-', 'LineWidth', 3);
subplot(2, 3, 5:6);
plot(x, separation, 'r-', 'LineWidth', 3);
title('Separations', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Column', 'FontSize', fontSize);
ylabel('Separation [pixels]', 'FontSize', fontSize);
grid on;
% Compute the mean separations where the separation is less than 100 but more than 0
goodColumns = separation > 0 & separation < 100;
meanSeparation = mean(separation(goodColumns))
% Put a line over the plot at the mean
line(xlim, [meanSeparation, meanSeparation], 'Color', 'b', 'LineWidth', 2);
caption = sprintf('Separations. Mean Separation = %.1f pixels', meanSeparation);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
0000 Screenshot.png
  2 Comments
meizaki fatihin
meizaki fatihin on 20 Jan 2020
sir. im sorry. i can't run your program
can you help me, please Capture11.JPG
Image Analyst
Image Analyst on 20 Jan 2020
Evidently you've downloaded it more than once and your operating system made the second downloaded copy have (1) after the file name. When you try to run that second copy of the m-file, MATLAB is interpreting the 1 as being an imput argument when it's running the first copy. But the program is a script and it does not take any input arguments, so it throws that error.
To fix, delete the second copy of the m-file - the one with the (1) after it. You should have only one copy and it should not have any number in parentheses in the filename.

Sign in to comment.


meizaki fatihin
meizaki fatihin on 16 Jan 2020
Alhamdulillah. Thank a lot sir . Thank you very much ?

Categories

Find more on Read, Write, and Modify Image 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!