how to easy select the pixel's position and pixel value in matlab? - 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].

Related

Limit impoly clicks and convert it to a rectangle - Matlab

I am using impoly as in the script below and I have two questions:
Can I limit the to points clicked (e.g., 5) and close it automatically?
Is there a way convert the impoly to imrect like in the attached image (red box)?
Script:
clc;
clear;
figure, imshow('pout.tif');
hpoly = impoly(gca);
From the documentation of impoly, I don't think it is directly possible. For such custom behaviour, you should probably write your own point picking function.
Several matlab functio ncan help you in this direction.
[x,y] = ginput(n) to pick points
impoint(hparent,x, y) to draw draggable points,
line to draw a the line between points, and the rectangle bounding box.
impoint has a 'PositionConstraintFcn' parameter, that will call a function of yours when the point is moved. You can use it to update the lines draw when the points are moved.
I suggest you to have a main function that handles the point picking (constraining the number of points, etc...), and a "display" function, that calculate the bounding box, draw the lines between points, that you can call when a point is added (in the main function), or when a point is moved (with the 'PositionConstraintFcn'parameter).

getting 2D points from mouse in simulink?

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.

Select input pixel of image by mouseclick in Matlab

I am trying to implement a tracking program using the Mean-Shift algorithm in Matlab. The idea is that, given the first frame of one video, the user can click on top of any object he wants and the program will track it all through the video sequence. I have already implemented and working the tracking part, but I am having problems giving the user the possibility to click on top of the image to select the initial pixel for the tracking algorithm.
I have thought about input function, but I don't know how to make it work. How can I display an image and click on top of a pixel and get its coordinates [x,y] to initialize the program?
You could use ginput (Graphical input from mouse or cursor)
Read any image and show them using imread and imshow
h = imread('hestain.png'); % any input image
imshow(h);
Get the coordinates using ginput, where the input argument corresponds to the number of user clicks recorded.
[x,y] = ginput(1); % getting coordinates from user
To obtain the pixel value, we need to pass the coordinates as indices of the image. To do this, the output arguments from the ginput which are double by default, must be converted to unsigned integers.
Also, x and y represents horizontal and vertical by default. But matlab syntax takes first dimension as rows (number of horizontal lines calculated vertically). Hence y value is passed as first dimension. Similarly x value as second dimension.
pixelValue = h(uint8(y),uint8(x)); % using coordinates as indices

Control Point Registration using matlab -'fitgeotrans' function

I'm new to Matlab and I'm trying to do a Control Point Registration using their guide: http://www.mathworks.com/help/images/point-mapping.html
It works fine until the 'fitgeotrans' function where I get an error that saying:
'Undefined function or variable 'input_points'.
I read the Matlab help and the previous "cpselect" function gives me two nX2 arrays with the coordinates which are saved (by the 'cpselect' function) in two array variables 'input_points' and 'base_points'. So, I really don't understand why the next function can't "See" them and considers them 'Undefined'.
My code is attached bellow. Thank you for your help.
function [Y] =EBL
ReferenceImg=imread('GFI.jpg'); %This is the fixed image
CroppedImg=imcrop(ReferenceImg); %Crop the image
close %close the imcrop window
ResizedIReferenceImg= imresize(CroppedImg,[1000 1000]); %re-size the fixed image
t=imagesc(ResizedIReferenceImg); %Set transparency of fixed image
set(t,'AlphaData',0.5);
hold on
I = imread('GF.bmp'); %This is the moving picture
MovingImg = imrotate(I,-5,'nearest','crop'); % Rotate the moving picture cw
ResizedMovingImg= imresize(MovingImg,[1000 1000]); %re-size the moving image
h=imagesc(ResizedMovingImg); %Set transparency of moving image
set(h,'AlphaData',0.6);
close
cpselect(ResizedMovingImg,ResizedIReferenceImg); %Alignment
tform = fitgeotrans(input_points,base_points,'NonreflectiveSimilarity');
The problem is that MATLAB doesn't, by default, wait for you to be done before moving on from cpselect. So it simply moves on from cpselect to tform before you have a chance to actually select any points, at which point input_points doesn't yet exist. You have to set the Wait parameter, and doing so also affects the outputs. With Wait on, call cpselect something like this:
[input_points,base_points] = cpselect(MovingImg,ReferenceImg,'Wait', true);
When calling cpselect in this way, you will not have the "Export Points to Workspace" option. Instead, the selected points will be output into the variables input_points,base_points when the cpselect window is closed.

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