Effacer les filtres
Effacer les filtres

Changing all of one color in an image to another

23 vues (au cours des 30 derniers jours)
saad khalid
saad khalid le 30 Juil 2015
Commenté : Image Analyst le 30 Juil 2015
So, I have this image: http://i62.tinypic.com/2lux9w1.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!

Réponses (1)

Image Analyst
Image Analyst le 30 Juil 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 commentaires
saad khalid
saad khalid le 30 Juil 2015
Modifié(e) : Image Analyst le 30 Juil 2015
I'm having a bit of a hard time implementing what you're talking about with my code. Currently, I'm using imread to import the images. Using imread also gives me access to the alpha data.
However, when i do something like:
>>pic1 = imread('pic1.png')
>>pic1
then it returns a matrix, mostly filled with 0's, and then some numbers that range around 200. is this data in the rgb format? Or is it in greyscale? Would your method still work with what I'm doing?
Image Analyst
Image Analyst le 30 Juil 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);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by