How do I not include toolbar button coordinates when using getpts?

3 次查看(过去 30 天)
I am writing a simple script that takes in user mouse-clicks, and using getpts, overlays a scatterplot on top of the image and saves the coordinates of the mouse clicks.
I am running into a problem whenever I zoom into the image. I have already added code so that, when the "peppers.png" image first loads, the toolbar icon coordinates are not included if they're pressed because in the beginning, they are outside of the image's coordinates.
However, if I zoom into an image, MATLAB believes that the "Restore View" button is now within the image, and so whenever I am done zooming and deselect the "zoom-in" button, and then select the "Restore View" button, it includes the "Restore View" button's coordinates in getpts. Why these tools are even included within the figure itself is beyond me. How do I prevent this from happening?
My code:
clear; close all; clc;
I = imread('peppers.png');
[y_px, x_px] = size(I);
figure
addToolbarExplorationButtons(gcf)
imshow(I);
hold on
[X_pts, Y_pts] = getpts;
X_idx = X_pts > 0 & X_pts <= x_px; %ensure we're only keeping points found within the actual image (i.e. not counting zoom-in/out tool clicks)
Y_idx = Y_pts > 0 & Y_pts <= y_px;
idx = X_idx & Y_idx;
X_pts = X_pts(idx); Y_pts = Y_pts(idx)
scatter(X_pts, Y_pts,'yellow', 'filled')

回答(1 个)

Sachin Lodhi
Sachin Lodhi 2024-5-2
Hello Mitch,
I understand that you are trying to overlay a scatter plot on top of your image using 'getpts' function.
According to Matlab documentation, 'getpts' function is not recommended for this purpose. You can use 'drawpoint' function instead. The 'drawpoint' function creates a 'Point' object that specifies the position of a point region of interest (ROI). You can create the ROI interactively by drawing the ROI over an image using the mouse, or programmatically by using name-value arguments. You can also specify the initial appearance and behavior of the ROI.
Here is a sample code snippet, that helps you plot 10 points interactively using mouse.
clear; close all; clc;
c = 0;
imshow(imread('peppers.png'))
while (c < 10)
h = drawpoint;
c = c + 1;
end
Note that if you want to plot more than 10 points, you will have to change the condition of the while loop.
I recommend you to go through the following documentation for more information on 'drawpoint' function :
I hope this helps!

类别

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

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by