Select input pixel of image by mouseclick in Matlab - 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

Related

Incorrect pixel value from data cursor on Matlab

When I open a Dicom image on Matlab (R2014b) and place the data cursor on a specific set of coordinates, I obtain an "index" (or pixel value) that does not match the same set of coordinates on the image's matrix (T).
>> figure, imshow(T);
See below on attached image.
Any idea why this is happening?
When using images, the data cursor tool with MATLAB's figure and its various ilk assume that X and Y are the column (horizontal) and row (vertical) coordinates respectively. In the variable inspector (the left part of your figure), these are reversed.
Be careful when using the data cursor on images. See this post by MathWorks on more details regarding interactive display of data: http://www.mathworks.com/help/matlab/creating_plots/data-cursor-displaying-data-values-interactively.html

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].

Making a 2D Matlab figure that contains multiple images which start at a given point (x,y)

Problem
I am trying to make a 2D figure in Matlab which consists of multiple images and a graph with plot data (which I could eventually convert into an image too). For these images and graph, I need to be able to specify where they are located in my cartesion coordinate system.
For my specific case, it is sufficient to be able to "tell" Matlab where the left-bottom corner of the image is.
So for the example above. I would need some "trick" to let "bird1.jpg" start at position (a,b), "bird2.jpg" at position (c,d) and my plot at position (e,f) in one Matlab figure.
Solution to problem
Thanks to chappjc I was able to find a solution for my problem. Here is the code I used such that other people can use it in the future too.
figure_color = [.741 .717 .42];
axe_color = [1 1 1];
Screen.fig = figure('units','pixels',...
'name','Parallel projection',...
'menubar','none',...
'numbertitle','off',...
'position',[100 100 650 720],...
'color',figure_color,...
'busyaction','cancel',...
'renderer','opengl');
Screen.axes = axes('units','pix',...
'position',[420 460 200 200],... % (420,460) is the position of the first image
'ycolor',axe_color,...
'xcolor',axe_color,...
'color',axe_color,...
'xtick',[],'ytick',[],...
'xlim',[-.1 7.1],...
'ylim',[-.1 7.1],...
'visible','On');
Screen.img = imshow(phantom);
Screen.axes2 = axes('units','pix',...
'position',[0 0 200 200],... % (0,0) is the position of the second image
'ycolor',axe_color,...
'xcolor',axe_color,...
'color',axe_color,...
'xtick',[],'ytick',[],...
'xlim',[-.1 7.1],...
'ylim',[-.1 7.1],...
'visible','On');
Screen.img2 = imshow(phantom);
Basically what I do is first creating a (big) figure, and then create a first axe at a certain position in this big picture, and make it the default axe. In this axe I display my first image (made with the phantom function). After that I make a new axe at a another position and make it again the default axe. After I have done that, I place an image there too (the same picture, but you can also use another one if you want). You can also use handles which is the more clean method, as chappjc describes.
Positioning axes in a figure
One approach would be to manipulate the Position property of multiple axes in a figure. To make multiple axes in a figure:
hf = figure;
ha0 = axes('parent',hf,'Position',[x0 y0 w0 h0]);
ha1 = axes('parent',hf,'Position',[x1 y1 w1 h1]);
Then display your images and plots into the axes by specifying the handle (i.e. ha0 or ha1). For example: image(img0,'Parent',ha0) or imshow(img1,'parent',ha1).
Single Large Image
Another approach is to make a single large image and simply display it with image/imshow/etc.
First for the plots, you can use getframe followed by frame2im to get an image in a matrix format.
Next, decide what goes into your combined image and compute the largest box required to circumscribe the images (using their origins and sizes find the largest x and y coordinate), which includes the origin presumably. Use this info to make a blank image (e.g. img = zeros(h,w,3) for and RGB image).

Plot points at 2d on matlab and show the associated image when click on a point

I have a collection of images associated to 2d points. I want tot plot those points on a figure and when I click on the points I expect to see the corresponding image on the screen. Suppose I have a matrix IMAGES that is nxd where n is number of images and d dimension of each image and I have Points matrix that is nx2 which shows the corresponding points as I said.
Can you please any method to do that?
Try to create a callback function for ButtonDownFcn. It will allow you to get which area has been clicked and execute your code.

Getting pixel coordinates efficiently in Matlab

I would like to create a function in Matlab that, given an image, will allow one to select a pixel by clicking on it in the image and return the coordinates of the pixel. Ideally, one would be able to click on several pixels in the image in succession, and the function would store all the respective coordinates in a matrix. Is there a way to do this in Matlab?
ginput
Graphical input from mouse or cursor
Syntax
[x,y] = ginput(n)
[x,y] = ginput
[x,y,button] = ginput(...)
Description
[x,y] = ginput(n) enables you to
identify n points from the current
axes and returns their x- and
y-coordinates in the x and y column
vectors. Press the Return key to
terminate the input before entering n
points.
I think this is what you want:
A=imread('filename.jpg');
image(A)
[x,y]=ginput()