How do I change the RGB values of individual pixels?
19 views (last 30 days)
Show older comments
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
on 14 Aug 2012
Your question would be easier to read if you broke it up into paragraphs.
Susanna Moore
on 10 Mar 2014
Have you check this post? http://www.mathworks.nl/matlabcentral/answers/76651-how-to-change-the-pixel-value-of-rgb-unit8-image-to-nan-or-black
It is related with how to change the pixel value of rgb. And idea about how to convert an RGB image into a grayscale image is also talked. Guess it help.
Accepted Answer
Walter Roberson
on 14 Aug 2012
RGBImage = reshape(Combinations, [size(Grayscale, 1), size(Grayscale, 2), 3]);
3 Comments
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().
More Answers (3)
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
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.
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
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
on 14 Aug 2012
1 Comment
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.
See Also
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!