MATLAB Answers

Nancy
0

Changing colors in an image

Asked by Nancy
on 10 Jul 2011
Latest activity Commented on by Image Analyst
on 20 May 2019
Hi Guys,
I have a 1024*1024*3 size .tif picture. The main colors in it are white, black and green. I want to reverse the colors black and white and change green to red. Can anyone tell me how i can do that?
Thanks Nancy

  2 Comments

Hi! I had a similar doubt, I have an image of a shoe on a white background( the shoe is of the same color). Now i want to show the image of shoes in various different colors. Is it possible to do such kind of a thing on MATLAB
Image Analyst
on 20 May 2019
Yes it's possible. Just convert the RGB image to HSV, multiply the HSV image by some scale factor in side rem, then call hsv2rgb
hsvImage = rgb2hsv(rgbImage);
hsvImage(:,:,1) = rem(hsvImage(:,:,1) * scale factor, 1);
rgbImage = hsv2rgb(hsvImage);
See attached demos also.

Sign in to comment.

5 Answers

Image Analyst
Answer by Image Analyst
on 11 Jul 2011
 Accepted Answer

Try this:
% Construct the rgb image with swapped red and green channels.
thresholdValue = 5; % How much greener does it need to be?
greenishPixels = (single(greenChannel) - 0.5*(single(redChannel) + single(blueChannel))) > thresholdValue;
newRed = 255-greenChannel;
newRed(greenishPixels) = 255;
newGreen = 255-greenChannel;
newGreen(greenishPixels) = 0;
newBlue = 255-blueChannel;
newBlue(greenishPixels) = 0;
rgbImage2 = cat(3, newRed, newGreen, newBlue);

  5 Comments

Image Analyst
on 14 Feb 2018
All I have time for now is to give you my demo that does a similar thing. See if you can adapt my attached demo.
HI Image Analyst. Can you please help!!
I have an image of black and grey. The image is containing a series of black spots (Darker to lighter) on grey background. I need to change the black intensity into RED to blue color format with colorbar. RED will be for dark dots and blue will for lighter dots. So that it will depict the intensity change in original image.
Image Analyst
on 28 Sep 2018
By keeping it as it is, gray scale, and applying a color mpa to the display? By segmenting it and converting it to RGB? By segmenting and overlaying a graphics channel? Please answer in a new question (not here), AFTER you read this link

Sign in to comment.


Answer by Prabhakar on 10 Jul 2011

The tiff image seems to be in a RGB true color format. If this is so you can change the image from from true color to indexed format using the RGB2IND function.. Then once the data has been converted to an indexed colormap, you can use the inbuilt colormaps to change the indexing.
To switch particular colors, use the FIND function to find the pixels which have a particular color say white and then set the true color to black or something that you want.
A good resource on playing with colormaps is: http://www.mathworks.com/support/tech-notes/1200/1215.html

  1 Comment

Nancy
on 10 Jul 2011
Thanks prabhakar. When I tried using rgb2ind, it is giving me a warning which I have copypasted below.
Warning: RGB2IND(RGB) is an obsolete syntax.
Specify number of colors, tolerance, or colormap.

Sign in to comment.


Image Analyst
Answer by Image Analyst
on 10 Jul 2011

All right, it was so easy that I decided to just do it and post it. Here is the code:
clc;
close all;
workspace;
clear;
fontSize = 16;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% 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(1, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
cform = makecform('srgb2lab');
lab = applycform(rgbImage, cform);
L_channel = lab(:,:,1);
A_channel = lab(:,:,2);
B_channel = lab(:,:,3);
L_channelNew = 100 - L_channel;
A_channelNew = 255-A_channel;
labNew = cat(3, L_channelNew, A_channelNew, B_channel);
cform2 = makecform('lab2srgb');
rgbNew = applycform(labNew,cform2);
% Display the new color image.
subplot(1, 2, 2);
imshow(rgbNew, []);
title('Transformed Color Image', 'FontSize', fontSize);

  9 Comments

Nancy
on 11 Jul 2011
https://picasaweb.google.com/112605247026977347833/MATLAB?authkey=Gv1sRgCOKrtZnZweObcQ
I converted the image to a jpg image. For some reason MATLAB wasnt allowing me to upload tif or png files
bachir
on 28 Nov 2015
wow this is pretty good, it worked for me with the attached picture
Image Analyst
on 28 Nov 2015
There is a new function you can use: rgb2lab() that does the color space conversion, using book formulas, all in one line of code.

Sign in to comment.


Image Analyst
Answer by Image Analyst
on 10 Jul 2011

Another way to do it is to convert RGB to LAB color space with makecform(), if you have the image processing toolbox. Then make a new L channel as 100 - the old L channel (to change light to dark), and negate the A channel (to convert green to red), and then convert back to RGB colorspace with makecform again. If you can't figure it out and need code, write back.

  1 Comment

Image Analyst
on 10 Jul 2011
By the way, my logo is a representation of the LAB color space.

Sign in to comment.


Image Analyst
Answer by Image Analyst
on 11 Jul 2011

It would have been helpful to post the image in the first place. It looks like you're dealing with graphics. So if you can't just draw it in another color, then just swap the red and green channels with cat():
rgbImage2 = cat(3, greenChannel, redChannel, blueChannel);
The Full Demo:
% Read in a color demo image.
folder = 'C:\Documents and Settings\user\My Documents\Temporary stuff';
baseFileName = 'outline.jpg';
fullFileName = fullfile(folder, baseFileName);
% 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(1, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Here's where the real meat of the code begins:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Construct the rgb image with swapped red and green channels.
rgbImage2 = cat(3, greenChannel, redChannel, blueChannel);
% Display the new color image.
subplot(1, 2, 2);
imshow(rgbImage2, []);
title('Green is now red', 'FontSize', fontSize);

  2 Comments

Nancy
on 11 Jul 2011
Thanks imageanalyst. How does one know if you are dealing with graphics or RGB image. And what about reversing black and white in the image?
Image Analyst
on 11 Jul 2011
Use this code:
% Construct the rgb image with swapped red and green channels.
thresholdValue = 5; % How much greener does it need to be?
greenishPixels = (single(greenChannel) - 0.5*(single(redChannel) + single(blueChannel))) > thresholdValue;
newRed = 255-greenChannel;
newRed(greenishPixels) = 255;
newGreen = 255-greenChannel;
newGreen(greenishPixels) = 0;
newBlue = 255-blueChannel;
newBlue(greenishPixels) = 0;
rgbImage2 = cat(3, newRed, newGreen, newBlue);

Sign in to comment.