How do I change the RGB values of individual pixels?

28 views (last 30 days)
Hello. I'm given two different grayscale images that I convert to RGB images. I change the colormap for each image so that one image goes from Black to Blue, and the other image goes from Black to Red. There is no Green value for either picture. Next I determine the RGB values for each image, and I create two matrices one with variations in the Red and the other with variations in Blue (Picture1 = [0.54,0.36,0.5,0.58];(Variations for Red) and Picture2 = [0.23,0.15,0.31,0.23]; (Variations for Blue)). Then I will combine these matrices into one matrix with three columns where the first column is Red and the second is Green and the third column is Blue (Combination = [Picture1(:)';(0.*Picture1(:)');Picture2(:)']; NOTE: the second column is zero because there are no Green values). FINALLY my question is how do I take these values for Red Green and Blue, and make a third picture that uses these values as the Red Green and Blue values at each pixel? For instance if my matrix is Combination = [0.54,0.00,0.23;...;...] I want the first pixel of my image to have an RGB value of RGB: 0.54,0,.23. If it helps the index of each image is equal. I just want to specify the color of each pixel by assigning the RGB values one by one.
Thank you very much,
Eric
  2 Comments
Walter Roberson
Walter Roberson on 14 Aug 2012
Your question would be easier to read if you broke it up into paragraphs.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Aug 2012
RGBImage = reshape(Combinations, [size(Grayscale, 1), size(Grayscale, 2), 3]);
  3 Comments
Eric
Eric on 14 Aug 2012
However I am unable to adjust the contrast of the new image, or change the image to a grayscale image. How could I go about doing this?
Thak you again.
Image Analyst
Image Analyst on 14 Aug 2012
If you want to adjust the contrast, do it before you make the gray scale images into an RGB image. If you want to do it on the RGB images, then you'll need to convert to hsv colorspace and adjust the v channel. To convert an RGB image into a gray scale image, use rgb2gray().

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 14 Aug 2012
Eric: See if this demo does what you want. It takes 2 standard MATLAB demo images (moon and cameraman) and makes then into 64x300 images (which, by the way, is NOT large at all) and each with a certain gray level range (not overlapping, though it wouldn't matter). Then it scales one and put it into the red channel and scaled the other and puts it into the blue channel and displays the result. Just copy, paste, and run.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
format compact;
format long;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage1 = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage1);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage1, []);
title('Original Grayscale Image #1', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount1 grayLevels1] = imhist(grayImage1);
subplot(2, 2, 2);
bar(pixelCount1);
grid on;
title('Histogram of original image #1', 'FontSize', fontSize);
xlim([0 grayLevels1(end)]); % Scale x axis manually.
% Resize to 64x300
grayImage1 = imresize(grayImage1, [64 300]);
% Remap intensities so that 0-255 is now in the range 0-127.
grayImage1 = imadjust(grayImage1, [0 1], [0 0.5]);
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(grayImage1, []);
title('Scaled Image #1', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount1 grayLevels1] = imhist(grayImage1);
subplot(2, 2, 4);
bar(pixelCount1);
grid on;
title('Histogram of scaled image #1', 'FontSize', fontSize);
xlim([0 grayLevels1(end)]); % Scale x axis manually.
%---------------------------------------------------------------
% Now the same for the second image.
figure;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'moon.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage2 = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage2);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage2, []);
title('Original Grayscale Image #2', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount2 grayLevels2] = imhist(grayImage2);
subplot(2, 2, 2);
bar(pixelCount2);
grid on;
title('Histogram of original image #2', 'FontSize', fontSize);
xlim([0 grayLevels2(end)]); % Scale x axis manually.
% Resize to 64x300
grayImage2 = imresize(grayImage2, [64 300]);
% Remap intensities so that 0-255 is now in the range 128-255.
grayImage2 = imadjust(grayImage2, [0 1], [.5 1]);
subplot(2, 2, 3);
imshow(grayImage2, []);
title('Scaled Image #2', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount2 grayLevels2] = imhist(grayImage2);
subplot(2, 2, 4);
bar(pixelCount2);
grid on;
title('Histogram of scaled image #2', 'FontSize', fontSize);
xlim([0 grayLevels2(end)]); % Scale x axis manually.
%---------------------------------------------------------
% Now, finally, we have our two starting images
% and we can begin.
% Recall grayImage 1 had gray levels in the range 0-127.
% Let's scale to 0-255.
redChannel = imadjust(grayImage1, single([min(grayImage1(:)), max(grayImage1(:))])/255, [0 1]);
% Recall grayImage 2 had gray levels in the range 128-255.
% Let's scale to 0-255.
blueChannel = imadjust(grayImage2, single([min(grayImage2(:)), max(grayImage2(:))])/255, [0 1]);
% Let's make a green channel of zero.
greenChannel = zeros(size(grayImage1), 'uint8')
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
figure;
% subplot(2, 2, 3);
imshow(rgbImage, []);
title('RGB Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
  3 Comments
Image Analyst
Image Analyst on 16 Aug 2012
Well that's all you'd use. The vast majority of the code was just to create two images that were like yours. You already have the images so you don't need that - you would only need the last 20 lines or so. You just need to calls to imadjust and cat. Just 3 or 4 lines really. If you want, you can upload your images somewhere and I could try it.

Sign in to comment.


Image Analyst
Image Analyst on 14 Aug 2012
this doesn't really make sense. Let's go over it one step at a time:
(1)"I'm given two different grayscale images that I convert to RGB images. " OK fine, you're doing something like rgbImage = cat3(grayImage, grayImage, grayImage) or using rgbImage=ind2rgb(grayImage, gray(256)).
(2) "I change the colormap for each image so that one image goes from Black to Blue, and the other image goes from Black to Red. There is no Green value for either picture." OK, that means you must have either had three different figures, or you followed these instructions http://www.mathworks.com/support/solutions/en/data/1-GNRWEH/index.html But changing the color map only changes how the gray image is displayed. It doesn't change any of the actual pixel values and the colormap does not even apply to color images because they're already in color.
(3) "Next I determine the RGB values for each image" OK, the RGB values are just redValue = rgbImage(row, column, 1), greenValue = rgbImage(row, column, 2), and blueValue = rgbImage(row, column, 3).
(4). "and I create two matrices one with variations in the Red and the other with variations in Blue (Picture1 = [0.54,0.36,0.5,0.58];(Variations for Red) and Picture2 = [0.23,0.15,0.31,0.23]; (Variations for Blue))." I have no idea what you're doing here. You make two 4 element vectors, which are nor really considered an image. And you didn't even use the RGB values you determined in step 3.
(5) "Then I will combine these matrices into one matrix with three columns where the first column is Red and the second is Green and the third column is Blue (Combination = [Picture1(:)';(0.*Picture1(:)');Picture2(:)']; " I have no idea what this is doing. You now have a 3 row by 4 column image, but I'm not sure what it's for.
(6) "NOTE: the second column is zero because there are no Green values). " No - the second columns is [0.36, 0, 0.15]' and the second ROW is all zeros. Just execute this code to prove it:
Picture1 = [0.54,0.36,0.5,0.58]
Picture2 = [0.23,0.15,0.31,0.23]
Combination = [Picture1(:)';(0.*Picture1(:)');Picture2(:)']
secondColumn = Combination(:, 2)
(7) "FINALLY my question is how do I take these values for Red Green and Blue, and make a third picture that uses these values as the Red Green and Blue values at each pixel?" Huh? What values for red green and blue?
(8) "For instance if my matrix is Combination = [0.54,0.00,0.23;...;...]" That's not what Combination looks like - run my code above to see.
(9) "I want the first pixel of my image to have an RGB value of RGB: 0.54,0,.23." Okay, just do rgbImage(1, 1, :) = [0.54,0,.23]
(10) "If it helps the index of each image is equal." That doesn't help - I don't even know what you mean. What are the images? The red color channel, the green color channel, and the blue color channel? Or combination? Or Picture1 and Picture2? Or your original two gray scale images? And how could the indexes be different? You need to specify the same row and column in each color channel if you want to get the rgb values at that row and column.
(11) "I just want to specify the color of each pixel by assigning the RGB values one by one." Like I said, just do rgbImage(row, column, :) = [redValue, greenValue, blueValue] with whatever values you want.
Please just give an overall high level concept for what you want to do with your gray level image and I will determine the best way to do it. Like "I want to make a purple image out of my grayscale image" or whatever. What you developed makes little sense (to me) as it is.
  2 Comments
Walter Roberson
Walter Roberson on 14 Aug 2012
I think "the index of each image is equal" means that the sizes of the two images are the same.
Eric
Eric on 14 Aug 2012
Hello I posted my question in the answer box below by mistake. I do appreciate your help on this.

Sign in to comment.


Eric
Eric on 14 Aug 2012
Ok, so for starters, here is the situation. I am given two large matrices both of which have a size of 64 by 300. Each of the values within the matrix represents some intensity of light. I want to display these intensities as an image where each value is a different shade of one particular color.
For instance if we were looking at MatrixA the values inside this (64*300) matrix would go from 15 : 65 where these are the values of intensity ( Also these values are always whole numbers), and I want the colorbar to go from black (lowest intensity of 15) to red (highest intensity of 65).
Ok so before this makes any sense I probably want to say that the first step I took was using the function imagesc(MatrixA) to produce an image that used the RGB colormap of "Jet" as the default image. The option of using Jet produces a colorbar or colormap where blue is the lowest intensity and Red is the highest intensity. However I only want one color to be displayed for each matrix. (MatrixA would be black to red and MatrixB would be black to blue)
In order to do this I went to the figure colormap editor and changed the markers on the colormap so that the first marker was black and the last marker was red. I deleted all the markers in-between. This produced a colorbar that used black as the lowest intensity (15) and red as the highest intensity (65). I know that there are 255 different possible values for Red, so the colorbar/colormap would have 255 different shades of red.
However here is where I start to get confused. I only have intensity values that range from 15:65 in my original matrix. So an intensity value of 15 would have an RGB value of 0,0,0 and an intensity value of 65 would have an RGB value of 255,0,0. This is not the case, because (I think) Matlab scales these RGB values so that they range from 0 to 1 so when displaying an intensity value of 65 the actual RGB value would be 1,0,0 (One good thing is that when displaying an intensity value of 15 the RGB value is still 0,0,0). I also think that there are still 255 different possibilities that Matlab uses on this 0 to 1 scale. However don’t worry about this part for a second.
Ok so let’s put this aside and just say that I produced a way of determining what RGB values Matlab assigns the different intensities.
In other words I took my original 64*300 matrix with values that ranged from 15:65 and determined what RGB values Matlab assigns the different values inside the matrix. An example being that if I have an intensity value of (let’s say) 23 then Matlab would assign this an RGB value of 0.159,0,0.
Now here might be one of my mistakes. I took the original 64*300 matrix and produced a matrix that has a size of 1*19200 (64*300 = 19200), but instead of using the values for intensity I used the values that matlab uses for the color red. Again what I mean is if the original matrix had a value of 23 at position 1000 then in the new 1*19200 matrix I would assign position 1000 a value of 0.159.
So now I have a ( 1*19200) matrix with the values Matlab uses for red instead of the intensity values.
Finally remember that at the very beginning I mentioned that I was given two matrices. Well I did the exact same thing for the second matrix. Except that instead of using red I used blue as the only color.
I now have two matrices with a size of 1*19200 and matrixA has values for Red and matrixB has values for Blue. What I want to do with these values now is to produce a third 1*19200 matrix that has a value for red, a value for green, and a value for blue at each location. I thought about just making a matrix that was 2*19200 and assigning column 1 the values for red and column 2 the values for blue. Better yet I wanted to produce a 3*19200 matrix where col 1 would be red, col 2 is green (set this column = to 0), and col 3 is blue. However I do not know if this will help at all.
The very last (and most important) part of this is to produce a new image that is 64*300 pixels in size, and each pixel is assigned the value for Red, the 0 value for green, and the value for Blue. What I mean by this is that I want to change the RGB values for each pixel by assigning each pixel the color I want them to display.
I know this is very long, but I really do appreciate your help.
-Eric
  1 Comment
Image Analyst
Image Analyst on 14 Aug 2012
64 by 300 is actually very tiny. See my code demo. I believe it does what you want to achieve in a more straightforward way than doing all that stuff you did with linear arrays, combination, etc.

Sign in to comment.

Categories

Find more on Colormaps 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!