19 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.

Muhammad Usman Saleem
on 30 Jun 2016

Edited: Muhammad Usman Saleem
on 30 Jun 2016

find your solution to problem

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

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:

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.

Image Analyst
on 2 Jul 2016

We're still confused and your most recent explanation did not answer any of Guillaume's questions. Perhaps you want the color frequency image: http://www.mathworks.com/matlabcentral/fileexchange/28164-color-frequency-image

or perhaps you just want imresize(). I have no since your wording doesn't correlate with your code where all you do is copy and image and take it's mean and sum. Sure, the mean or sum is a reduced set of data compared to the original image, but is that what you want or need? Still, I have no idea.

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?

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376145

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376145

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376147

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376147

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376154

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376154

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376155

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376155

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376169

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/293086-how-to-find-which-coordinate-or-pixel-x-y-contains-which-colour-intensity#comment_376169

Sign in to comment.