Find euclidean distance between 2 regions

7 ビュー (過去 30 日間)
Elysi Cochin
Elysi Cochin 2018 年 1 月 6 日
コメント済み: Image Analyst 2019 年 12 月 11 日
I have two regions R1 and R2
How to find "d(pi; pj)", which denote the Euclidean distance between any two pixels pi and pj.
Then define the distance Di from the ith pixel in R1 to R2 as:
please can someone help me with the explanation to compute distance from 2 regions. I find it difficult in understanding. Please help.
  2 件のコメント
Rik
Rik 2018 年 1 月 6 日
The Euclidean distance is simply the root of the squared difference. How you can calculate it for many pixels at once depends a bit on how your data is structured, but the meshgrid function will likely help out.
This doesn't really look like a Matlab question to me, and it looks like a homework question.
(Have a read here and here for tips on how to find solutions yourself, and you can find guidelines for posting homework on this forum here.)
Jan
Jan 2018 年 1 月 6 日
@Elysi: You forgot to mention, what "pi" and "pj" is and how they represent a "pixel". In addition it is not clear what "region" means: Do you want the minimal distance between any points belonging to two sets of points? Or is it the distance between the centers of the "regions"?
I understand, that Rik recommends to read "How to ask a good question". But this is your 255th question in the forum. You should be aware that the readers do not have the faintest idea about what you are doing, except if you explain it exactly.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2018 年 1 月 6 日
See full demo below:
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;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in gray scale demo image.
folder = fileparts(which('eight.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'eight.tif';
% 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
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% 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(rgbImage);
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.
grayImage = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
%------------------------------------------------------------------------------
% 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')
subplot(2, 2, 2);
histogram(grayImage);
grid on;
title('Histogram', 'FontSize', fontSize, 'Interpreter', 'None');
% Create a binary image
binaryImage = grayImage < 200;
% Fill holes and take 2 largest blobs.
binaryImage = bwareafilt(imfill(binaryImage, 'holes'), 2);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image with 2 Regions', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
%=============================================================================================
% Now we have our two regions and we can begin!
%=============================================================================================
% Get the boundaries.
boundaries = bwboundaries(binaryImage);
b1 = boundaries{1};
b2 = boundaries{2};
% Get the x and y
x1 = b1(:, 2);
y1 = b1(:, 1);
x2 = b2(:, 2);
y2 = b2(:, 1);
% Get distances between each boundary pixel and each one in the other blob.
% We just check the outer boundaries to reduce the number of distances we need to compute.
distances = pdist2([x1,y1], [x2,y2]);
% Find the min distance
minDistance = min(distances(:))
[index1, index2] = find(distances == minDistance)
% Get the locations
x1Min = x1(index1);
y1Min = y1(index1);
x2Min = x2(index2);
y2Min = y2(index2);
% Draw a line between them
hold on;
plot([x1Min, x2Min], [y1Min, y2Min], 'r-', 'LineWidth', 2);
title('Binary Image with 2 Regions with Closest Points Indicated', 'FontSize', fontSize, 'Interpreter', 'None');
  3 件のコメント
Conner Anderson
Conner Anderson 2019 年 12 月 11 日
Index exceeds the number of array elements (1).
b2 = boundaries{2};
Image Analyst
Image Analyst 2019 年 12 月 11 日
Conner, you need to have at least two blobs. I didn't put in a check for that (I just assumed it). You have only one blob, and so you have only one boundary, or maybe even no blobs at all. Post your binary image if you need more help.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by