Changing all of one color in an image to another
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/147584/image.png)
I also have 6 others like it, they all come together to form a picture of a brain. They all have a transparency layer and have the same dimensions, so I can show them all at the same time. However, what I would like to do is to change the black parts of the image into a color of my choosing(the color will represent the intensity of brain activity). Each of the 7 pictures will have a different color. I already know how to show all of the pictures on the same figure, i just don't know how to change the black to a different color.
If it helps at all, I am trying to change the black to a color along a gradient ranging from black to bright red.
Thank you!
0 Kommentare
Antworten (1)
Image Analyst
am 30 Jul. 2015
There might be a more compact way but one way is to just assign the color you want to each color channel and then recombine:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Find pixels that are pure black - black in all 3 channels.
blackPixels = redChannel == 0 & greenChannel == 0 & blueChannel == 0;
% Make red (255,0,0)
redChannel(blackPixels) = 255;
greenChannel(blackPixels) = 0;
blueChannel(blackPixels) = 0;
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
2 Kommentare
Image Analyst
am 30 Jul. 2015
There's no reason your code shouldn't work. Do this:
rgbImage = imread('pic1.png');
[rows, columns, numberOfColorChannels] = size(rgbImage);
If numberOfColorChannels = 1 instead of 3 then you have a gray scale image and you need to convert it into a color image.
% All color channels need to be set equal to the gray scale image.
redChannel = rgbImage;
greenChannel = rgbImage;
% Find pixels that are pure black - black in all 3 channels.
blackPixels = redChannel == 0;
% Make red (255,0,0)
redChannel(blackPixels) = 255;
greenChannel(blackPixels) = 0; % Same as blue channel
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, greenChannel);
Siehe auch
Kategorien
Mehr zu Convert Image Type finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!