Detect points in predefinied distnace in array

2 次查看(过去 30 天)
Hello
I have a simple question but I can not find its solution.
I have a predefined point in an array (Xo,Yo) and I need to detect the points that lie within a distance D from (Xo,Yo). Is there any simple way to do this ?
By looking into availabe funcitons of Matlab I step upon ExhaustiveSearcher() but I think is quite complicated to use for such a simple process !
Is there any other function I am missing ?
Thank you in advance !
  4 个评论
Jan
Jan 2013-2-18
编辑:Jan 2013-2-18
Please post a small example of the inputs, than we do not have to spend time for guessing how the data a represented. What is "predefined point in an array (Xo,Yo)" exactly?
Image Analyst
Image Analyst 2013-2-18
编辑:Image Analyst 2013-2-18
You said later you are dealing with pixels (an image). You can post your image if you want: http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers What is ExhaustiveSearcher?

请先登录,再进行评论。

采纳的回答

Jan
Jan 2013-2-18
allPixels = rand(1000, 2);
point = [0.5, 0.6];
radius = 0.1;
% Squared Euclidean distance:
dist_2 = sum(bsxfun(@minus, allPixels, point) .^ 2, 2);
near = dist_2 <= (radius .^ 2);
Now near is TRUE for all pixels inside the radius around the point.

更多回答(3 个)

José-Luis
José-Luis 2013-2-18
编辑:José-Luis 2013-2-18
If you are only interested in the closest points and not the actual value of the distance itself, you could get rid of the expensive square root operation and compare the squared distances.
D = rand; %some_value
squared_dist = D.^2;
test_x = rand; %some_value, could be a vector as well
test_y = rand;
isClose = ( (test_x - x0).^2 + (text_y - y0).^2 ) <= squared_dist;
  2 个评论
Dimitris M
Dimitris M 2013-2-18
Hello
Thank you for you immediate answer but I am not sure what your algorithm is computing !
I am interested in finding all pixel lying at a certain standardize distance from Xo,Yo point. Is this also what your algorithm is doing ?
Regards
José-Luis
José-Luis 2013-2-18
There was no mention of pixel points in your original question. This just tests whether a point is at a certain distance from another, using Euclidean distance. When you talk about pixels, should the centers be at a certain distance? Should any part of the pixel be at a certain distance?

请先登录,再进行评论。


Image Analyst
Image Analyst 2013-2-18
Dimitris, see my demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
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;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'moon.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
uiwait(helpdlg('Click on a point'));
[centerX, centerY] = ginput(1)
hold on;
plot(centerX, centerY, 'r+', 'MarkerSize', 30);
% Find a circle 50 pixels from that point.
radius = 50;
% Reference the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
[columnsInImage rowsInImage] = meshgrid(1:columns, 1:rows);
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
subplot(2, 2, 2);
imshow(circlePixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle with radius 50', 'FontSize', fontSize);
% Assign values outside the circle to zero
newGrayLevelOutside = 0; % Can be anything from 0-255.
grayImage(~circlePixels) = newGrayLevelOutside ;
subplot(2, 2, 3);
imshow(grayImage);
title('Image within 50 pixels', 'FontSize', fontSize);

Dimitris M
Dimitris M 2013-2-19
Thanks for all the feedback ! Your support is really good !
Regards

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by