how to get the location and the color of a pixel in workspace by clicking with the mouse - Matlab
Show older comments
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
More Answers (2)
Image Analyst
on 17 Sep 2013
0 votes
Use impixelinfo(), available in the Image Processing Toolbox. Instructions are in the help.
5 Comments
HecLondon
on 18 Sep 2013
Image Analyst
on 18 Sep 2013
You can just get the value from the lab array directly:
labColor = lab(row, column, :);
I don't know what you're talking about with respect to angle - why there's an offset. Red is at zero degrees, yellow at 90, green at 180 and blue at 270 (like my avatar). Why would they not be? What is the purpose of an angular offset? If you want you can go to hsv colorspace with rgb2hsv() and h is the normalized hue angle.
Image Analyst
on 18 Sep 2013
See this demo and you'll see the RGB and LAB are not the same.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
promptMessage = sprintf('Please click a point in the image,\nor Quit to exit?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(button, 'Quit')
return;
end
[x,y] = ginput(1);
% Put a cross where they clicked.
hold on;
plot(x, y, 'w+', 'MarkerSize', 50);
% Get the location they click on.
row = int32(y);
column = int32(x);
% Extract the RGB color from that location.
rgbColor = rgbImage(row, column, :)
cform = makecform('srgb2lab');
% Get lab in range 0-255 for L.
labImage1 = applycform(rgbImage,cform);
% Get lab in range 0-100 for L
labImage2 = applycform(im2double(rgbImage),cform);
% Extract the RGB color from that location.
labColor1 = labImage1(row, column, :)
labColor2 = labImage2(row, column, :)
message = sprintf('Done!\n(x,y) = (%.3f, %.3f)\n(row, column) = (%d, %d)\n(R,G,B) = (%d, %d, %d)\n(L,A,B) = (%d, %d, %d)\n(L,A,B) = (%.3f, %.3f, %.3f)\n',...
x,y,row,column, ...
rgbImage(row, column, 1), rgbImage(row, column, 2), rgbImage(row, column, 3), ...
labImage1(row, column, 1), labImage1(row, column, 2), labImage1(row, column, 3), ...
labImage2(row, column, 1), labImage2(row, column, 2), labImage2(row, column, 3))
uiwait(helpdlg(message));
Image Analyst
on 18 Sep 2013
Yes - that was a copy/paste issue that I forgot to change.
HecLondon
on 18 Sep 2013
0 votes
5 Comments
Image Analyst
on 18 Sep 2013
Yes, see my color segmentation tutorial: http://www.mathworks.com/matlabcentral/fileexchange/31118-color-segmentation-by-delta-e-color-difference You manually pick a color (draw some regions) and then it finds all colors like that in the picture.
Variables in MATLAB are always "saved" in a workspace - the workspace that is in scope, which usually means the function workspace if you're in a function or the base workspace if you're in a script. There is no need to save anything to the base workspace from a function, though there are functions to do that. This is almost always recommended against. Why do you think you have a special need to do that? In many years of writing hundreds of complex MATLAB programs of thousands of lines I've never needed to do that.
HecLondon
on 18 Sep 2013
Image Analyst
on 18 Sep 2013
I'd use ginput(1) to have the user select a single point. impixel() is kind of a pain for that. Then get the row and column from that, something like this untested code off the top of my head:
[x,y] = ginput(1);
row = int32(y);
column = int32(x);
labColor = labImage(row, column, :);
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.
Categories
Find more on Basic Display in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!