Interactive plot using impoint

4 views (last 30 days)
Eddie Boyd
Eddie Boyd on 10 Apr 2017
Answered: Nirav Sharda on 17 Apr 2017
I'm working on developing an application that places a sinusoid over an image due to points selected on the image. I have that part down, but I would like for the user to have the ability of changing( thus updating) the period and amplitude of the sinusoid by moving the points on the image. After the sine function is drawn over the image, I attempt to move a point to update but I receive the following error: "Undefined function 'getPosition' for input arguments of type 'double'." My code is below
function imageDemo2
clf, clc;
hFigure = figure(1);
hAx = axes;
readPic = imread('SomeImage.jpg');
[numRows numCols RGB] = size(readPic);
imagesc(readPic);
hold on;
[x,y] = ginput(2);
hLine = line(x,y,'LineWidth',2,'Color',[0 0 0]);
P1 = impoint(gca,[]);
setColor(P1,'r');
P2 = impoint(gca,[]);
setColor(P2,'r');
P3 = impoint(gca,[]);
setColor(P3,'r');
P4 = impoint(gca,[]);
setColor(P4,'r');
[L1,L2,D1,D2] = dragLine2(P1,P2,P3,P4);
x = L1:L2;
D = floor(D1(:,2) - D2(:,2));
L = 2*floor(L2(:,1)-L1(:,1));
y = (D).*sin((4*pi).*(x-L1(:,1))/L) + L1(:,2);
hplot = plot(x,y,'r','LineWidth',1);
addNewPositionCallback(P1,@dragLine2);
addNewPositionCallback(P2,@dragLine2);
addNewPositionCallback(P3,@dragLine2);
addNewPositionCallback(P4,@dragLine2);
function [L1,L2,D1,D2] = dragLine2(P1,P2,P3,P4)
L1 = getPosition(P1);
L2 = getPosition(P2);
D1 = getPosition(P3);
D2 = getPosition(P4);
I did try to make P1-P4 global variables but I encountered the same problems. If there is any guidance you can provide towards a mistake I made or something I neglected to add, your help would be greatly appreciated.

Accepted Answer

Nirav Sharda
Nirav Sharda on 17 Apr 2017
The documentation page on addNewPositionCallback mentions that when the ROI object changes its position, the callback function is called with the syntax 'fcn(pos)', where pos is returned from the object's 'getPosition' method. Once you have the new position either you can assign it to a variable in the base workspace using 'assignin' or can do some calculations there and modify the sinusoid. Here is a sample script for your reference.
hFigure = figure(1);
hAx = axes;
readPic = imread('SomeImage.jpg');
imagesc(readPic);
hold on;
P1 = impoint(gca,[]);
setColor(P1,'r');
addNewPositionCallback(P1,@dragLine2);
function dragLine2(pos)
disp('position')
disp(pos)
% to assign the variable to the base workspace
assignin('base','a',pos)
% or you can modify the sin wave here using some code.
% some code here
end
I hope this helps.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!