getting 2D points from mouse in simulink? - matlab

I want to apply perspective transform in Computer Vision toolbox in simulink of Matlab. I need to get 4 points from mouse on a given image to compute perspective matrix according these points.
I want to use the first frame of movie to get points on it.

One possible way is to create a level-2 MATLAB S-Function block which takes input from "From Multimedia File" and shows a figure window with the first frame to the user. From the figure window you can use ginput to get mouse clicks and then send that as output Pts connected to "Estimate Geometric Transformation". After the first frame you need to continue to send the same Pts as output. See documentation for creating level-2 MATLAB s-function at
http://www.mathworks.com/help/simulink/slref/level2matlabsfunction.html
It is probably easier if you do this outside simulink, by reading the first frame and find the points using ginput on a figure window. You can then use some Constant blocks as Pts1 and Pts2 inputs for which you can set the values using set_param.

Related

How do I determine the displacement between two matrices using a two-dimensional cross-correlation function (xcorr2) in matlab

Im very new to matlab and medical imaging in general. For a project I need to finish a script that uses two matrices the "kernel" and "searchArea" to determine the displacement of the interventricular septum of the heart using the blockchaining method with multiple frames of the cardiac cycle. The "kernel" is a fraction of the orginal frame image and the "searchArea" is an area thats surrounds the kernel in the next frame. I have to use cross correlation to do this and already tried the following (this process will be repeated for all the frames):
crossCor = xcorr2(searchArea,kernel);
[max_cc,imax] = max(abs(crossCor(:)));
[ypeak, xpeak] = ind2sub(size(crossCor),imax(1));
dispLat = ypeak-size(kernel,1)
dispAx = xpeak-size(kernel,2)
This code was writen based on the xcorr2 documentation: https://nl.mathworks.com/help/signal/ref/xcorr2.html
The code does work but the results do not meet my expectations, the script that I got provided with uses the lateral displacement (x displacement) and axial displacement (y displacement) to create tracking frames:
The results should show the displacement of the septum just like in frame 1, but you can see that in frame 2 and 3 the displacement is way off.
I do not know if I'm on the right track here of giving the provided script the right displacements, but any advice or nudges in the right direction are welcome!
REMARK:
I am doing this for an introductory medical imaging course, the blue colored mesh which is used to plot the movement is created using speckle tracking, however I did not get any details on how this works which is probably intended. The Matlab script relies on the right input of axial (y) and lateral (x) displacement, the results are wrong, so my written code is probably (partly) wrong. My main intension to post this on stackoverflow is to receive feedback on my written code, because I don't know what I have done wrong.

Plotting position data from Simulink in MatLab

I have constructed a simulator in Simulink that simulates the position of an object. I want to visualize the X-Y position of this object in a matlab figure.
I exported the X-Y data from Simulink to matlab using the To Workspace block. From this I get an x and y time series data out.x_pos and out.y_pos. I can plot them against time with
plot(out.x_pos)
But the following does not work to get an X-Y plot
plot(out.x_pos, out.y_pos)
What is the best way to produce this X-Y plot?
Since no one helped me, I will present what I came up with after eventually reaching the right pages on the MATLAB documentation.
The To Workspace block exports the data as a timeseries object. This object has the values of the signal as a property called Data. To access the property one writes <object>.Data, so to obtain the desired X-Y plot one writes
plot(out.x_pos.Data, out.y_pos.Data)

how to easy select the pixel's position and pixel value in matlab?

In order to classify remote sensing image by using k-means method in matlab, I want to select some samples in an image with an mouse clicking on it, but I don't know how to code that, could you tell me some method to accomplish it?
you can use impoint, for example:
figure, imshow('pout.tif');
h = impoint(gca,[]);
position = wait(h);
this interactively place a point. Use wait to block the MATLAB command line. Double-click on the point to resume execution of the MATLAB command line.
position is specifying the coordinates of the point [X Y].

3D mouse input in matlab/simulink

I want to take input coordinates through mouse in Matlab oR Simulink, there is no built-in facility in Matlab of input of 3D coordinate through mouse device, however the buit-in function ginput can only store 2D coordinates of mouse, is there is any possibility in MATLAB/SIMULINK to input 3D coordinates through mouse device?
If I understand you correctly, you want to get the coordinates (in data space) of the mouse click, when the plot is 3D. That is, you click somewhere in the plot and it returns your current position. I have actually tackled this exact problem before.
The main difficulty with this task--and the other posters have alluded to it--is that you are clicking on a 2D screen. Thus, you cannot specify 3 independent positions on a 2D screen uniquely. Rather, clicking on the screen defines a line segment, normal to the plane of the screen, and any of the 3D points along this line are equally valid. Do you understand why this is the case?
To demonstrate, try this short example in Matlab:
surf(peaks); %draw a sample plot
keydown = 2;
while keydown ~= 0,
disp('Click some where on the figure');
keydown = waitforbuttonpress;
end
currPt = get(gca,'CurrentPoint');
disp(currPt);
You'll observe that currPt is a 2x3 matrix. This defines the start and end points of this line. Let's plot this line now:
hold on;
plot3( currPt(:,1), currPt(:,2), currPt(:,3), 'k-', 'LineWidth', 2);
view(-19,46); %rotate to view this line
So the question is: how to define which point along this line you want to select? Well the answer depends on what type of data you have in the first place. If you have point data, choosing one of your vertices exactly can be tricky, and you might need to do some post-processing of your data (for example, to calculate the closest point in your dataset to the currPt line). If you have patch or surface data (such as this example), this is just the intersection of a line and a plane.
There are some tools on the File Exchange to get 3D points for various datasets. One that I just found is: http://www.mathworks.com/matlabcentral/fileexchange/7594-click3dpoint

How can I select a pixel by its spatial coordinates in Matlab?

I want to select a pixel in an image using floating-point numbers as indexes. The Matlab documentation says that this is possible using "spatial coordinates". However, it doesn't provide any clues on how to do it. How can I select a pixel from an image using floating-point indexes ("spatial coordinates")?
Suppose that I have the following code:
i = imread('pout.tif')
get_pixel_by_spatial_coords(i, 1.5, 3.63)
What's the real name of the function get_pixel_by_spatial_coords?
I think the linked article on spatial coordinates was only describing the coordinate systems used by various image plotting routines.
Your your purpose, simply round the number. Depending on the context, use one of:
i(round(1.5), round(3.63))
i(floor(1.5), floor(3.63))
i(ceil(1.5), ceil(3.63) )
I believe you're looking for ginput:
ginput raises crosshairs in the current axes to for you to identify points in the figure, positioning the cursor with the mouse. The figure must have focus before ginput can receive input. If it has no axes, one is created upon the first click or keypress.