how to get the location and the color of a pixel in workspace by clicking with the mouse - Matlab

11 views (last 30 days)
Hello,
I have some fiducial points in an image and I need the user to select the first point and get the coordinates of that point and the colour vector in workspace.
At the moment I have only found:
Data cursor, but it only gives the location, not the colour Impixel, apparently should give both, but it is a bit confusing and it's not working very well imroi only gives location too from what I've read
Can you please help me with this? Also, can the colour vector be in the Lab colorspace? (transform the image first, and then click in the point?)
Many thanks! Hector

Accepted Answer

Chad Greene
Chad Greene on 23 Oct 2015
I wrote a colorpicker function to make this a bit easier.

More Answers (2)

Image Analyst
Image Analyst on 17 Sep 2013
Use impixelinfo(), available in the Image Processing Toolbox. Instructions are in the help.
  5 Comments
HecLondon
HecLondon on 18 Sep 2013
Edited: HecLondon on 18 Sep 2013
Many thanks and sorry for the delay.
When it says towards the end "Extract the RGB color from that location" did you mean the LAB?
I tried the demo. Previously, my color segmentation code was based in choosing an ROI within the fiducial point and getting the mean a and b values. I put the values obtained from your demo (range 0-255 as I didn't use im2double in my code) but the color segmentation is not working. Most of the code is from a demo I found, so I copy it here so that you can check:
%load the photo and resize it to reduce the time of the code
fabric2 = imread('skin2.JPG');
fabric = imresize(fabric2, 0.2);
%Define the number of colors in the photo (we just need the skin and
%fiducial points, but it is possible to add more) and a matrix for the ROI
nColors = 2;
%This is to convert the RGB image into an L*a*b* image
cform = makecform('srgb2lab');
lab_fabric = applycform(fabric,cform);
%This is to calculate the mean 'a*' and 'b*' value for each area extracted with roipoly. These values serve as color markers in 'a*b*' space
a = lab_fabric(:,:,2);
b = lab_fabric(:,:,3);
color_markers = repmat(0, [nColors, 2]);
for count = 1:nColors% this is the part I changed, just putting the %values obtained from your demo
color_markers(count,1) = 14 ;
color_markers(count,2) = -22 ;
end
%Create an array that contains your color labels
color_labels = 0:nColors-1;
%Initialize matrices to be used in the nearest neighbor classification
a = double(a);
b = double(b);
distance = repmat(0,[size(a), nColors]);
%Perform classification
for count = 1:nColors
distance(:,:,count) = ( (a - color_markers(count,1)).^2 + ...
(b - color_markers(count,2)).^2 ).^0.5;
end
[value, label] = min(distance,[],3);
label = color_labels(label);
clear value distance;
%Display Results of Nearest Neighbor Classification
rgb_label = repmat(label,[1 1 3]);
segmented_images = repmat(uint8(0),[size(fabric), nColors]);
for count = 1:nColors
color = fabric;
color(rgb_label ~= color_labels(count)) = 0;
segmented_images(:,:,:,count) = color;
%Choose 1 to show the color of the fiducial points
imshow(segmented_images(:,:,:,1));
end
By the way, should I create another question about the angle problem, separated from this one? (As I see you are only replying to the problem about the color).

Sign in to comment.


HecLondon
HecLondon on 18 Sep 2013
Hi Image Analyst,
Many thanks for your answer. I think I didn't explain myself properly: I would like to get the coordinates and the colour value of a pixel by clicking on the mouse directly into the workspace, without having to copy. In other words: I want to do automatic colorsegmentation, so I want the user to just click in a fiducial point, save this colour automatically in workspace and then use it somewhere else in the script. Is this possible?
  5 Comments
HecLondon
HecLondon on 18 Sep 2013
Edited: HecLondon on 18 Sep 2013
Thanks, but it is possible to get the L,a,b values instead of the rgb ones with labColor? I keep getting the rgb even I change my image to Lab (see below):
I = imread('skin2.JPG'); %read the image in I
RGB = imresize(I, 0.2);
cform = makecform('srgb2lab');
lab_fabric = applycform(RGB,cform);
[x,y] = ginput(1);
row = int32(y);
column = int32(x);
labColor = lab_fabric(row, column, :);
Also, following your comment above about the angles. Sorry for the misunderstanding: the angle problem is not related with the colors. Once I have the color of the pixel and I do color segmentation, I end up with a black and white image of the fiducial points. Now, I want to put labels in these points and get the centroids in a clockwise direction, starting with the point clicked at the beginning (starting angle).
The only idea I had to order the points in a clockwise direction was using the angles of the points, and order them in a increasing order. The problem is that I don't know how to change the atan2 function (or use another one), to put the zero angle where the first fiducail point is (it seems the axis are always horizontal). Do you have any idea to go around this problem?
Image Analyst
Image Analyst on 18 Sep 2013
Let's continue the conversation under my answer, not yours. But it looks like that should give you the lab values, not the rgb values because lab_fabric is a different array than RGB.

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!