# How to find which coordinate or pixel (x,y) contains which colour intensity?

7 views (last 30 days)
Nimit Jain on 30 Jun 2016
Commented: Guillaume on 4 Jul 2016
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.
Nimit Jain on 30 Jun 2016
Edited: Image Analyst on 30 Jun 2016
Hi Guillaume Apologise for the confusion. Below is the image details
FileSize 20945
Format jpg
FormatVersion ''
Width 200
Height 200
BitDepth 24
ColorType truecolor
Also I am applying Rrc = a(r,c,1); in my code it's giving me Index exceeds matrix dimensions error. Below is my code I am using
close all
clear all
figure, imshow(a);
[r,c] = size(a);
totalSum = 0;
fileID = fopen('exp.txt','w');
fileID1 = fopen('totalValue.txt', 'w');
binaryImage = a >= 200;
numberOfWhitePixels = sum(binaryImage(:));
R = a(:,:,1); % Red color plane
rSize = size(R);
G = a(:,:,2); % Green color plane
B = a(:,:,3); % Blue color plane
check = a(r,c,1);
% Iterating pixel by pixel of the image and saving value in txt file
%for i=1:r
% for j=1:c
% value = double( a(i,j));
% totalSum = totalSum + value;
% fprintf(fileID,'%d\n',i,j, value);
% fprintf(fileID1, '%d\n' , totalSum);
% disp(a(i,j));
% end
%end
fclose(fileID);
fclose(fileID1);

Muhammad Usman Saleem on 30 Jun 2016
Edited: Muhammad Usman Saleem on 30 Jun 2016

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);
##### 2 CommentsShow 1 older commentHide 1 older comment
Guillaume on 1 Jul 2016
There are a few things in your code that show a lack of understanding of how images are represented and of how matlab works. I would suggest grabbing a book on image processing in your favorite library:
[rR, cR, zR] = size(R);
[rG, cG, zG] = size(G);
[rB, cB, zB] = size(B);
R, G, and B are the three colour planes of your images. The z* is always going to be 1, there's no point asking for it. The size of the colour planes is going to be the same as the size of the image, so rR == rG == rB == r, same for c*. In other words, the above three queries are completely unnecessary. You've already got the information.
valueR = double(something uint);
%then simply print value
There's absolutely no point in converting to double. The exact same number will be printed than if you hadn't bothered.
value = double(rgbImage(i,j));
%...
fprintf(format, value)
Note that rgbImage is an r x c x 3 matrix. You haven't specified the 3rd dimension index in the above, so due to the way matlab indexing work, it's simply 1. Therefore that loop is only going over the red pixels, same as your first loop.
Note that to make it easier to spot bugs, I would move the initialisation of totalsum to 0 just before the loop starts.

Guillaume on 1 Jul 2016
A much simpler way to save your pixels to text files would be:
[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.
Guillaume on 4 Jul 2016
"I want to reduce the size of the image" Physical size (i.e. imresize the image)?, the memory footprint but not size, maybe by reducing the number colours and converting to indexed with rgb2ind?
"I am trying to get the high color density values" What does that mean? What is the density of a colour?
"either reduce it or remove it" What does it refer to?
" high color density values/bits" Again what does that mean? Why are you suddenly talking about bits?

### Categories

Find more on Explore and Edit Images with Image Viewer App 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!