Moe
Moe

Reputation: 51

Pixel location through MATLAB

I am working on a project where I have to find a certain object on a platform using an attached camera through MATLAB. I thought about using the platform as a grid, but I've been told that using the pixels of the camera, I might be able to get that position precisely by clicking on the camera window/screen and choosing a certain pixel (where the objects are going to show on the camera window/screen).

Is there a way to calculate the location of the object (clicked pixel) or is there any possible way I could do that?

Upvotes: 4

Views: 9080

Answers (1)

Steve
Steve

Reputation: 4097

Try using the ginput(...) function in MATLAB, like this:

% Load some image:
data = imread('fishy 01.jpg');

% display the image:
figure(88);
clf;
h = imagesc(data);
axis image

% Get a value from the screen:
[x, y] = ginput(1);

msgbox(['You want pixel: ' num2str(round([x,y]))]);

This will give you the location of the pixel in the current axis. Alternately you, could use the figure callback WindowButtonUpFcn to get the current mouse position in the figure then translate that over to the axis you want it relative to, then scale to the current axis xlim and ylim. But ginput(1) will be much easier.

Example Run

Upvotes: 6

Related Questions