Borrar filtros
Borrar filtros

Changing all of one color in an image to another

23 visualizaciones (últimos 30 días)
saad khalid
saad khalid el 30 de Jul. de 2015
Comentada: Image Analyst el 30 de Jul. de 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!

Respuestas (1)

Image Analyst
Image Analyst el 30 de Jul. de 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 comentarios
saad khalid
saad khalid el 30 de Jul. de 2015
Editada: Image Analyst el 30 de Jul. de 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 el 30 de Jul. de 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);

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by