how to change pixel with the same RGB values
2 views (last 30 days)
Show older comments
I'm writing a white balance algorithm in matlab. I have to change the RGB values of a pixel in (1,1,1) if it has the same RGB values as my RGB vector. I copied the RGB values of my vector in a matrix with the same size like my picture( x * y * 3(RGB)), because i think it's easier to compare. Now i compare the values with this statement:
input = picture;
tmp = ... % RGB value matrix
input( input(1:end,1:end,1) == tmp(1:end,1:end,1) & input(1:end,1:end,2) == tmp(1:end,1:end,2) & input(1:end,1:end,3) == tmp(1:end,1:end,3) ) = 1;
I think my comparison is wrong, because it's just comparing the Red value with the Red value, the green with the green.... and doesn't test R = R and G = G and B = B then --> R = 1, G = 1, B = 1
Sry for my bad english but some help would be awesome!
0 Comments
Accepted Answer
Image Analyst
on 25 Mar 2013
I'm not sure what that code attempts to do. Whatever it does, it's not doing white balancing. Why don't you just identify the region of the image that you want to be white, for example by imfreehand or some other method. Then find the actual mean color for that region. Then use imadjust() to linearly scale the actual color values to the desired color values. Do it for each color channel and then recombine. There are more sophisticated methods but I'm sure that will be fine for you.
7 Comments
Image Analyst
on 25 Mar 2013
John, try this demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 4, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Ask user to click a point
message = sprintf('Click on a point that you would like to make white');
button = questdlg(message, 'Continue?', 'OK', 'Cancel', 'OK');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'Cancel')
close all;
return;
end
[column row] = ginput(1);
hold on;
plot(column, row, 'r+', 'MarkerSize', 50, 'LineWidth', 2);
% Convert to integer
row = int32(floor(row));
column = int32(floor(column));
for colorChannel = 1 : 3
% Extract just this color channel.
thisColorChannel = rgbImage(:, :, colorChannel);
% Display the color channel image.
subplot(2, 4, 1+colorChannel);
imshow(thisColorChannel);
caption = sprintf('Original Color Channel #%d', colorChannel);
title(caption, 'FontSize', fontSize);
originalMinValue = 0.0;
originalMaxValue = double(thisColorChannel(row, column)) % Whatever pixel value you clicked on.
originalRange = originalMaxValue - originalMinValue;
% Get a double image in the range 0 to +1
desiredMin = 0;
desiredMax = 255.0;
desiredRange = desiredMax - desiredMin;
dblImage = desiredRange * (double(thisColorChannel) - originalMinValue) / originalRange + desiredMin;
% Display the corrected color channel image.
subplot(2, 4, 5+colorChannel);
imshow(dblImage, []);
caption = sprintf('Corrected Color Channel #%d', colorChannel);
title(caption, 'FontSize', fontSize);
% Build up RGB image
if colorChannel == 1
whiteBalancedImage = dblImage;
else
whiteBalancedImage = cat(3, whiteBalancedImage, dblImage);
end
end
% Convert to uint8
whiteBalancedImage = uint8(whiteBalancedImage);
% Display the White Balanced color image.
subplot(2, 4, 5);
imshow(whiteBalancedImage);
title('White Balanced Image', 'FontSize', fontSize);
More Answers (1)
Jan
on 25 Mar 2013
Currently your code is equivalent to:
in = picture; % avoid to use builtin function name "input" as variable
in(in == tmp) = 1;
When tmp is a [1x3] RGB vector, this would be the code:
in(in(:, :, 1) = tmp(1) & in(:, :, 2) = tmp(2) & in(:, :, 3) = tmp(3)) = 1;
Now all pixels of in, whose color equals the value of tmp, gets white. Why do yout think, that this is not the wanted result?
5 Comments
Image Analyst
on 25 Mar 2013
You can use code like this:
originalMinValue = 0
originalMaxValue = % Whatever pixel value you clicked on.
originalRange = originalMaxValue - originalMinValue;
% Get a double image in the range 0 to +1
desiredMin = 0;
desiredMax = 1.0;
desiredRange = desiredMax - desiredMin;
dblImage = desiredRange * (double(grayImage) - originalMinValue) / originalRange + desiredMin;
Do that for every color channel image. So grayImage would be redChannel, greenChannel, and blueChannel, each in turn.
See Also
Categories
Find more on MATLAB Mobile 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!