How to find which coordinate or pixel (x,y) contains which colour intensity?
14 views (last 30 days)
I am writing a program where I am able to find the RGB values in the image using the below code
R = a(:,:,1); % Red color plane
G = a(:,:,2); % Green color plane
B = a(:,:,3); % Blue color plane
Now how I can find which coordinate or pixel (x,y) contains which type of colour intensity.
Image Analyst on 30 Jun 2016
To interactively see the RGB values, use impixelinfo():
hp = impixelinfo();
Also, you're using size incorrectly:
[r,c] = size(a);
c is the product of the number of columns times the number of color channels, so is basically three times the number of columns and that's why you get an index out of bounds error. See Steve's blog for more info:
Don't call your image "a" - that is a not very descriptive name, and it seems like it might be susceptible for you to use "a" again later in the code, blowing away your image, because you forgot you used a. Call it rgbImage instead. It's much more descriptive. To use it correctly, do
[rows, columns, numberOfColorChannels] = size(rgbImage);
Guillaume on 1 Jul 2016
A much simpler way to save your pixels to text files would be:
rgbImage = imread('C:\Users\Desktop\Documents\MATLAB\example.jpg');
[height, width, ~] = size(rgbImage); %height and width are more meaningful than r and c.
RedChannel = rgbImage(:, :, 1);
GreenChannel = rgbImage(:, :, 2);
BlueChannel = rgbImage(:, :, 3);
[rows, columns] = ndgrid(1:height, 1:column);
%because you save by rows and matlab is column based, we need to transpose all the arrays before reshaping them into one column
%it can then be written as one matrix
rows = reshape(rows.', , 1);
columns = reshape(columns.', , 1);
dlmwrite('rColor.txt', [rows, columns, reshape(RedChannel.', , 1)], ' ');
dlmwrite('gColor.txt', [rows, columns, reshape(GreenChannel.', , 1)], ' ');
dlmwrite('bColor.txt', [rows, columns, reshape(BlueChannel.', , 1)], ' ');
I have no idea what you're trying to do with your last loop, but I'm certain you don't need a loop.