How does getPosition work in impoly?

1 次查看(过去 30 天)
Hello,
i've been using impoly for drawing polygon in a blank figure, and then use the pos=getPosition(h) function to get the position of the drawn polygon. but the problem is, when i changed the corner of the drawn polygon, the "h=getPosition" function won't retrieve (update) the new position, is there a way to update the position while we change the corner?.
also, suppose that i draw a few polygon in a figure using only "h=impoly" then i want to delete one of the previous polygons (by choosing one of the drawn polygons to be deleted). i have used the "delete(h)" command but only the previous body that can be deleted.
thanks in advance :)
  2 个评论
Mallory
Mallory 2012-1-16
thanks, for the answers, really helped me alot
i wish i could make 2 accepted answers for you guys, :p
Matt Tearle
Matt Tearle 2012-1-16
Glad to help. You could upvote Alex's answer (I have, because it was a good explanation of the code I posted.)

请先登录,再进行评论。

采纳的回答

Matt Tearle
Matt Tearle 2012-1-12
I'm not seeing the behavior you observe. You wrote the "h=getPosition" function, so I'm wondering if you're overwriting your h variable (so it no longer refers to the impoly object)? Or maybe that you have multiple ones (given your second question) and you're changing one, but that's not the one your h variable refers to.
Anyway, to address both questions:
imshow('street1.jpg')
h(1) = impoly(gca,[20,20;80,20;80,80;20,80])
h(2) = impoly(gca,[200,200;280,200;280,280;200,280]);
x1 = getPosition(h(1))
% Move one of the corners of the first polygon
x2 = getPosition(h(1))
x1 - x2
Note that h is a 2-element impoly object (1 element per shape). x1 and x2 are numeric arrays of the coordinates.
EDIT TO ADD: OK, didn't understand the (first) question at all, sorry! The answer to the second part stands: just make h an array of impoly objects (or, of course, just have multiple objects with different variable names, but that's harder to maintain).
For the first part, about updating xy whenever you move a vertex, how about this:
addNewPositionCallback(h(1),@(p) assignin('base','xy',p));
This is dangerous, in that you are clobbering anything in the base workspace with the name xy. And if you want this in a function, then don't use the base workspace.
Also, I just realized I didn't fully answer the second part, about deleting. The point I was making was that if you keep all the handles in an array, you can delete any of them:
delete(h(1))
  3 个评论
Mallory
Mallory 2012-1-12
i don't really understand, the script that you mentioned
(sorry, i'm still a newbie in matlab)
addNewPositionCallback(h(1),@(p) assignin('base','xy',p));
can you give any advice in using the script to update the polygon position?
Matt Tearle
Matt Tearle 2012-1-13
Just run that command. From then on, whenever you move the vertices of the polygon, the base workspace variable "xy" will be updated with the coordinates. Try it out: run the command, then move the polygon points while you can see the workspace browser in the background; you should see the values in "xy" change. (If necessary, expand the "value" field in the workspace browser so you can see them.)

请先登录,再进行评论。

更多回答(2 个)

Alex Taylor
Alex Taylor 2012-1-13
Regarding "updating the polygon position". I'm still not fully understanding the question, but Matt has already provided good answers. I'd like to try to add a bit.
1) The getPosition method of impoly will always return the current XY positions of the vertices of the polygon. If you adjust the polygon position via an interactive gesture, or via the setPosition method, calling getPosition will always provide updated information. So, I don't know what you mean when you say "the "h=getPosition" function won't retrieve (update) the new position". Each time you want to know the current vertex positions, you can call getPosition and you will get the updated vertex positions.
2) If what you want is a function to fire whenever the position of the polygon is adjusted, then you should use the addNewPositionCallback method described by Matt. To provide a slightly more simple example:
h = impoly();
fun = @(p) disp(p);
addNewPositionCallback(h,fun);
Will add a new position callback function to the impoly object h which will cause the function defined in the anonymous function "fun" to fire each time the position of the polygon changes. The function argument requires that you pass in a function handle, which can either be an anonymous function or can be a handle to any function on the path. In this case, the anonymous function fun takes one input argument "p", which is the array of new XY vertex positions. The function uses the DISP function to display these vertex positions.
- Alex.

Image Analyst
Image Analyst 2012-1-16
You might consider using roipolyold(). It doesn't let you reposition the location of vertices that have already been placed, but you avoid having to right click and "copy mask" or whatever it says. It finishes simply by you right clicking the last point.

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by