필터 지우기
필터 지우기

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

조회 수: 15 (최근 30일)
Nimit Jain
Nimit Jain 2016년 6월 30일
댓글: Guillaume 2016년 7월 4일
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.
  댓글 수: 5
Guillaume
Guillaume 2016년 6월 30일
편집: Guillaume 2016년 6월 30일
By convention, RGB values go from 0 to 1 in matlab if the image is of type double.
The range will be [0 255] if the image is uint8, [0 65535] if the image is uint16, etc.
I too do not understand the question. The red value of pixel at row r and column c is
Rrc = a(r, c, 1);
But surely that is not the question.
Nimit Jain
Nimit Jain 2016년 6월 30일
편집: Image Analyst 2016년 6월 30일
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
a = imread('C:\Users\nimitja\Documents\MATLAB\example.jpg');
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);

댓글을 달려면 로그인하십시오.

답변 (3개)

Muhammad Usman Saleem
Muhammad Usman Saleem 2016년 6월 30일
편집: Muhammad Usman Saleem 2016년 6월 30일
On this link , read question and other answers..
find your solution to problem

Image Analyst
Image Analyst 2016년 6월 30일
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
Nimit Jain
Nimit Jain 2016년 7월 1일
편집: Walter Roberson 2016년 7월 1일
Thanks for the guidance. I have written the below code to iterate pixel by pixel in the image and also get the R, G, B value according to (x,y) position in a txt file. Below is the code. Can you review and tell me if there is anything wrong in it.
close all
clear all
rgbImage = imread('C:\Users\Desktop\Documents\MATLAB\example.jpg');
reducedA = zeros(size(rgbImage));
figure, imshow(rgbImage);
[r,c,numberOfColorChannels] = size(rgbImage);
totalSum = 0;
fileID = fopen('pixels.txt','w');
fileIDR = fopen('rColor.txt','w');
fileIDG = fopen('gColor.txt','w');
fileIDB = fopen('bColor.txt','w');
fileID1 = fopen('totalValue.txt', 'w');
fileIDCompressed = fopen('CompressedPixels.txt', 'w');
binaryImage = rgbImage >= 200;
numberOfWhitePixels = sum(binaryImage(:));
% Here we are calculating the color (RGB) planes
R = rgbImage(:,:,1); % Red color plane
G = rgbImage(:,:,2); % Green color plane
B = rgbImage(:,:,3); % Blue color plane
[rR, cR, zR] = size(R);
[rG, cG, zG] = size(G);
[rB, cB, zB] = size(B);
% try to print all Red value present in the image
for iR = 1:rR
for jR = 1:cR
valueR = double(R(iR,jR));
fprintf(fileIDR,'%d %d %d\n',iR,jR, valueR);
end
end
% try to print all Green value present in the image
for iG = 1:rG
for jG = 1:cG
valueG = double(G(iG,jG));
fprintf(fileIDG,'%d %d %d\n',iG,jG, valueG);
end
end
% try to print all Blue value present in the image
for iB = 1:rB
for jB = 1:cB
valueB = double(B(iB,jB));
fprintf(fileIDB,'%d %d %d\n',iB,jB, valueB);
end
end
% Iterating pixel by pixel of the image and saving value in txt file
for i=1:r
for j=1:c
value = double(rgbImage(i,j));
compressedValue = value - 1;
totalSum = totalSum + value;
fprintf(fileIDCompressed, '%d %d %d \n',i,j, compressedValue);
fprintf(fileID,'%d %d %d \n',i,j, value);
fprintf(fileID1, '%d\n' , totalSum);
% disp(a(i,j));
end
end
fclose(fileID);
fclose(fileID1);
fclose(fileIDR);
fclose(fileIDG);
fclose(fileIDB);
fclose(fileIDCompressed);
Guillaume
Guillaume 2016년 7월 1일
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
Guillaume 2016년 7월 1일
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.
  댓글 수: 5
Image Analyst
Image Analyst 2016년 7월 2일
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
Guillaume 2016년 7월 4일
"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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by