How do i obtain a single pixel value in a grayscale image by clicking on it?

I have a GUI snd i need to obtain a pixel value of one of my images so i can set a threshold for segmentation. I only want one pixel value and want the user to click on the picture to find the specific value. Does ginput do this or is there something to ginput that i can use?

 Accepted Answer

See my interactive thresholding app : http://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image. It should work beautifully for all type of grayscale images - let me know if it doesn't. Other than that, use ginput() to get the column,row coordinates
[column, row] = ginput(1);
grayLevel = image(row, column);
or use impixelinfo() to get the gray level "live" as you move the mouse around over the image.

7 Comments

Your app is fantastic! I am working on my project for a class, and mine is no where near that nice! I used the ginput and it gives me a column and row however when i grayLevel = image(row, column) i get an error that says "Incorrect number of arguments specified" and the image i have in the axis goes away. Any ideas on why that is?
Under my push button callback all i have is:
[column, row] = ginput(1); grayLevel = image(row, column)
and i dont get a pixel value.
Sorry - replace image with the actual name of your image variable, like img, grayImage, or whatever it's called. image is the name of a built in display function. ANd be sure to round row and column, or cast them to int32, because ginput() can give floating point (fractional) values. Maybe like this:
[x, y] = ginput(1);
row = int32(y);
column = int32(y);
grayLevel = grayImage(row, column);
% Or if it's an RGB image
rgbTriplet = rgbImage(row, column, :);
You can still do impixelinfo() - that's what I'd do instead of ginput(), but you can do both if you want.
But there are four images that can be clicked on so in the coding i wouldnt know which on the user picks. I am trying to make a GUI that finds fish. so i have four colored fish pictures, the user picks which one they want to find in the main image. when the user clicks the fish i want that to be the threshold which the segmentation is done around. Im just stuck on where on the part where i get pixel information from the users click. impixel works sometimes and sometimes i get NaN, is there any way i can get impixel to terminate after a single pixel has been chosen?
I think this is where you'd want to put the code in the mousedown event for each image, so you know which image they clicked on.
Okay so i just added a push button under each photo, when you pick one it pulls up ginput and i select a pixel, however when i use the code below i get a row and column but then get an error that says
Attempted to access BlueFish(91,212); index out of bounds because size(BlueFish)=[0,0]
mySize = size(BlueFish);
[column, row] = ginput(1); newRow = round(row); newColumn = round(column);
colorValue = BlueFish(newRow, newColumn);
Well, then you'd better track down why the BlueFish image is basically null.
I didnt have BlueFish set to global earlier in my program! That basically null comment clicked in my brain! Thank you so much for your help! I now have a set of RGB Values!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!