How can I edit the coordinate of a point ploted on an image?
11 次查看(过去 30 天)
显示 更早的评论
Hi all,
I am developing a GUI and I need to eventually move a point to another place using the mouse. For example:
imshow(someImage, []), hold on;
plot(x, y, '*r')
I want to select one of the points of x and y vectors and move it using the mouse and register its new coordinate, i.e., substitute the old value of the point (x, y) coordinate by the new one. How can I implement this?
4 个评论
Geoff Hayes
2016-6-4
Gabriel - please clarify step 4 While dragging the point, the software should plot the point following the mouse and erases the last position; Are you suggesting that as the pixel is dragged across the image, the underlying pixel should be replaced by that one and then replaced with the original pixel when the mouse cursor has passed over this pixel? Suppose I have an image that has all blue pixels except for one red one. I click on the red pixel and start to drag it to a new position. As the cursor passes over the other blue pixels, is the blue pixel momentarily replaced by the red one or do we see a red streak showing my path from the red pixel source to its destination?
Also, step 7 The software must get this new coordinate and update the list of coordinates (x, y vectors); What list of coordinates are you referring to?
Is this a homework assignment?
采纳的回答
Geoff Hayes
2016-6-13
Gabriel - you can use the WindowButtonDownFcn and WindowButtonUpFcn callbacks for the figure to determine where the user has pressed the mouse button within the figure (and so within the axes) and where he or she has released the mouse button. This will allow you to "drag" and "drop" a pixel from one location to another. Suppose we create a 10x10 image of random colours as
img = uint8(randi(255,10,10,3));
hImg = image(img);
hAxes = get(hImg,'Parent');
hFig = get(hAxes,'Parent');
Once the image has been drawn, we can get the handle to the axes and the handle to the figure. (An alternative to the above is to use the gca and gcf to get the equivalent handles.)
We now assign the button down and up callbacks to our figure as
set(hFig, 'WindowButtonDownFcn', @FigButtonDownFunc);
set(hFig, 'WindowButtonUpFcn', @FigButtonUpFunc);
where the button down callback is defined as
function FigButtonDownFunc(hObject, event)
[r,c] = IsInAxes;
if ~isempty(r)
mouseInAxes = true;
pixel = img(r,c,:);
end
end
We call the IsInAxes function to determine the row and column of the image where the user has pressed the mouse button. We need to handle the case where the user has pressed outside the axes, so we check to see if r is non-empty before proceeding. Once we have done that, we save the pixel that the user has selected (to a local variable that is visible to FigButtonDownFunc which has been nested within the parent function pixelMover).
The button up callback is defined as
function FigButtonUpFunc(hObject, event)
[r,c] = IsInAxes;
if ~isempty(r) && mouseInAxes
img(r,c,:) = pixel;
set(hImg,'CData',img);
end
mouseInAxes = false;
pixel = [];
end
Again, we call IsInAxes to ensure that the user has released the mouse button within the axes. If so, then we replace the current pixel with the one saved when the user pressed the mouse button. We update the image object (via the set(hImg,'CData',img)) and we observe the result of the drag and drop.
See the attached which performs the above and can be adapted for your needs.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!