Changing color in matlab

I've attached an image herewith. I want to change the color from green to red. Thanks.

1 Comment

Thank you all. Is there a way I can change it to other color than RGB? like yellow, for example...

Sign in to comment.

 Accepted Answer

im = imread('xyz_converted.png');
im(:,:,[1 2]) = im(:,:,[2 1]); % swap the Red and Green channels
imshow(im)

7 Comments

Thank you. Is there a way I can change it to other color than RGB? like yellow, for example...
im = imread('xyz_converted.png');
im(:,:,1) = im(:,:,2);
imshow(im)
In general:
% desired color:
color = [0.9, 0.5, 0.1]; % orange
im = imread('xyz_converted.png');
data = im(:,:,2); % only the green channel has non-zero elements
for ii = 1:3
im(:,:,ii) = data*color(ii); % apply those values to each channel, weighted by the
% corresponding value in the desired color
end
% show it
imshow(im)
Thank you, but what would be the best way to change any color of choice?
Specify the color you want as the variable color:
color = [0.9, 0.5, 0.1];
% ^^^ red channel
% ^^^ green channel
% ^^^ blue channel
Each element is a number between 0 and 1, so color = [1 0 0] gives you red, color = [0 1 0] is green, color = [0 0 1] is blue, [1 0 1] is magenta (red+blue), etc.
Here's cyan (color = [0 1 1]):
% desired color:
color = [0, 1, 1]; % cyan
im = imread('xyz_converted.png');
data = im(:,:,2); % only the green channel has non-zero elements
for ii = 1:3
im(:,:,ii) = data*color(ii); % apply those values to each channel, weighted by the
% corresponding value in the desired color
end
% show it
imshow(im)
Why does some of my images have fourth channel. How to handle those? My images are in .tif and can't be converted to png or jpeg. So could not attach that.
Thanks
You can zip the tiff and attach the zip.
TIFF files can have 4 channels under several circumstances:
  1. They might have an alpha channel. Tiff with alpha can generally be converted to png, if you are careful about the imwrite()
  2. They might be CMYK TIFF -- CMYK support is an important reason why TIFF was designed (even if I rarely see CMYK TIFF in practice, as those are mostly used in publishing)
  3. The 4th (and possibly additional) channels might be addtional spectra, most commonly NIR (Near Infrared). If you have included infrared you should typically be working with the Hyperspectral Imaging Library, https://www.mathworks.com/help/images/hyperspectral-image-processing.html
  4. TIFF are one of the most common file formats for use with multiple "layers" of data, such as having views from the air together with information about roads and maybe about sewers and maybe about power lines and maybe about income... In particular, geotiff extension is often used for those kind of tiff images.

Sign in to comment.

More Answers (2)

I=imread("xyz_converted.png");
I(:,:,1)=I(:,:,2);
[m,n,~]=size(I);
I(:,:,2)=zeros(m,n);
imshow(I)

1 Comment

Thank you. Is there a way I can change it to other color than RGB? like yellow, for example...

Sign in to comment.

A = imread('xyz_converted.png');
[R,G,B] = imsplit(A);
B=cat(3,G, 0*R,0*B);
imshow(B)

1 Comment

Thank you. Is there a way I can change it to other color than RGB? like yellow, for example...

Sign in to comment.

Tags

Asked:

on 28 Sep 2023

Commented:

on 29 Sep 2023

Community Treasure Hunt

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

Start Hunting!