How do you use inpolygon?

I am trying to learn how to use this function. I have a photo of two buttons, and have acquired the bounding boxes for those buttons with their coordinates. The coordinates are saved in two matrices, one for x (Xcoordinates) and one for y (Ycoordinates).
Now I want the image to open and have the user click on one of the buttons using the inpolygon function. If the user clicks outside the bounding boxes, nothing is displayed, but if they click inside one of the buttons, information pertaining to that button is displayed.
I've defined xv to be Xcoordinates and yv to be Ycoordinates, for the inpolygon function, but I am not sure how the function works and how to implement for this thing I want to do.

 Respuesta aceptada

Image Analyst
Image Analyst el 12 de Mayo de 2013

0 votos

Wouldn't it just be
xVertices = [Xcoordinates(1), Xcoordinates(2), Xcoordinates(2), Xcoordinates(1)];
yVertices = [Ycoordinates(1), Ycoordinates(1), Ycoordinates(2), Ycoordinates(2)];
itsInside = inpolygon(x, y, xVertices, yVertices);
where x and y are what you got from ginput(1)? But it sounds like you already tried that, so am I missing something? Do you need a full blown demo?

6 comentarios

Jake
Jake el 12 de Mayo de 2013
The way I've done, using your variables is:
xv = xVertices;
yv = yVertices;
a = inpolygon(x,y,xv,yv)
if a == 0
disp ('Nothing')
else
disp('Information')
end
I am a beginner with Matlab, so I probably have made some errors here, including inside the loop. Thank you for your help!
Jake
Jake el 12 de Mayo de 2013
So when I run this, Matlab says "Polygon must be defined by vectors (XV,YV)"
Image Analyst
Image Analyst el 12 de Mayo de 2013
Jake, you did something wrong, but you didn't supply your full code so I can't tell what it was. Here is the full-blown demo, guaranteed to work.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.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);
imshow(grayImage, []);
axis on;
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
promptMessage = sprintf('Draw a box.\nLeft click and hold to begin drawing.\nSimply lift the mouse button to finish');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
% Have user draw the box. (Code from the help for rbbox).
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
% Get vertices of the box.
xVertices = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)]
yVertices = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)]
hold on
axis manual
% Plot the box.
plot(xVertices, yVertices, 'b-');
% Have user click inside or outside the box.
while true
promptMessage = sprintf('Click a single point.');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
[x, y] = ginput(1)
% Determine if the point is inside or outside.
itsInside = inpolygon(x, y, xVertices, yVertices)
% Tell user if it was inside or outside.
if itsInside
caption = sprintf('The point (%.2f, %.2f) is inside the box.', x, y);
else
caption = sprintf('The point (%.2f, %.2f) is outside the box.', x, y);
end
title(caption, 'FontSize', fontSize);
uiwait(helpdlg(caption));
end
Jake
Jake el 13 de Mayo de 2013
Thank you this helped. But itsInside could apply to three polygons. How do I tell the program to differentiate between them? "elseif" does not seem to work. So:
itsInside = inpolygon(x,y,xVertices, yVertices)
%Tell the program "If user clicks inside this polygon, display this %information. Elseif user clicks inside the next polygon, display this %information
%If user clicks outside polygon, display "error"
Thanks again!
Image Analyst
Image Analyst el 13 de Mayo de 2013
Just call it 3 times, once with each set of vertices.
Eddy Sanoli
Eddy Sanoli el 13 de Mayo de 2020
You can also separate the vertices of the different polygons with NaNs and the function will treat each sequence of vertices not separated with a NaN as an individual polygon

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Surfaces, Volumes, and Polygons en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Mayo de 2013

Comentada:

el 13 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by