Changing all of one color in an image to another
조회 수: 23 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Image Analyst
2015년 7월 30일
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
Image Analyst
2015년 7월 30일
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!