Detect points in predefinied distnace in array
1 回表示 (過去 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 件のコメント
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
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.
1 件のコメント
Image Analyst
2013 年 2 月 18 日
その他の回答 (3 件)
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 件のコメント
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
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);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で 3-D Volumetric Image Processing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!