MATLAB: How to figure a colour based on a matrice of 1-by-3

imageimage processingMATLAB

This is my code:
%%In this part we just find the coordinates of the required colours
im = imread('Lighthouse.jpg');
n = 4;
figure, imshow(im)
[x,y] = ginput(n);
x = floor(x);
y = floor(y);
%colour(length(x),3);
for i=1:length(x)
for j=1:3
colour(i,j) = im(y(i),x(i),j);
end
end
image(colour)
when I run it, and slect a point on the image it doesn't show me the colour I selected!! it figures a red colour!! why is that? the variable colour is [195,213,223]
how can I visualize that RGB colour?

Best Answer

  • Well you messed up in a couple of places. Try this:
    im = imread('peppers.png');
    n = 4;
    figure, imshow(im)
    [x,y] = ginput(n);
    x = floor(x);
    y = floor(y);
    %colour(length(x),3);
    colour = zeros(length(x), 1, 3, 'uint8');
    for i=1:length(x)
    for j=1:3
    colour(i,1, j) = im(y(i),x(i),j);
    end
    end
    image(colour)