Change specific color in an image to another one
Show older comments
Hello,
I am trying to change colors in an image into another color. I have generated some code myself but something seems to be wrong. What I am trying to achieve on the attached file is to change every purple data point into a green data point, or whatever color I would prefer. Currently, the code that I have written is able to change purple to green but the edges of the marked points are still in purple, I believe this has something to do with the intensity at the edges being different or something...
[I,m] = imread('iteration31.png');
%image = imshow(I,'Colormap',m);
rgbImage = ind2rgb(I,m);
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
purplePixels = redChannel == 1 & greenChannel == 0 & blueChannel == 1;
% MAKE THEM GREEN
redChannel(purplePixels) = 0;
greenChannel(purplePixels) = 1;
blueChannel(purplePixels) = 0;
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
imshow(rgbImage);
Accepted Answer
More Answers (1)
Consider the following example using the OP's original image and task.
% Load image
[idxpict,ct] = imread('iteration31.png');
rgbpict = im2uint8(ind2rgb(idxpict,ct));
% Play around with the thresholds until you get what you want.
thresholds = [0.82 0.85]; % [Hmin Hmax]
hsvpict = rgb2hsv(rgbpict);
isPurple = hsvpict(:,:,1)>0.82 & hsvpict(:,:,1)<0.85;
% clean up the mask
isPurple = imerode(isPurple,ones(2));
% dilate it to hopefully include the extremes of the region extent
isPurple = imdilate(isPurple,ones(3));
imshow(isPurple(250:450,1200:1350,:)) % show a closeup of the mask
% expand to simplify addressing later
isPurpleArray = repmat(isPurple,1,1,3);
% Once you're happy with your thresholds above, adjust the image content.
hsvpict(:,:,1) = mod(hsvpict(:,:,1)-0.5,1); % rotate hue by 180d
adjpict = hsv2rgb(hsvpict);
% combine the adjusted image and the original using logical composition
% images must be the same class and scale
outpict = rgbpict;
outpict(isPurpleArray) = im2uint8(adjpict(isPurpleArray));
% Compare the original and new image.
figure(3)
subplot(1,2,1)
imshow(rgbpict(250:450,1200:1350,:))
title('Original')
subplot(1,2,2)
imshow(outpict(250:450,1200:1350,:))
title('Modified')
This is a relatively simplified case since the colored dots are well separated by neutral colors. In this scenario, there's little harm in making the mask overselect the dots. If the background were not neutral, things would be different.
I've posted a number of answers to "change one color to another" problems. The above example uses no special tools, but some of the other examples use freely available third party tools.
Change the colors in a smoothly-graduated rainbow image (uses MIMT color2alpha())
Naive channel swap, hard masked filling/tweaking, lin masked colorization (color chips against table)
Color squares example (hue rotate, hue replace, solid fill; (mostly MIMT)
Pink strawberries (HSV segmentation & adjustment; no MIMT)
Change hair color (colorization of black objects and the challenges of realism)
Categories
Find more on Purple in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

