Reputation: 59
I want to convert just green color to red in an image. I've written the code below but it doesn't work correctly
rgbImage = imread('image.jpg');
[rows columns numberOfColorBands] = size(rgbImage);
imshow(rgbImage, []);
title('Original Color Image');
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
rgbImage2 = cat(3, greenChannel, redChannel, blueChannel);
imshow(rgbImage2, []);
title('Green is now red');
Upvotes: 2
Views: 571
Reputation: 62
As Rotem said, you are just swapping red and green channels. Swapping affects to whole image instead of just green colors.
One has to segment out green color first to change green color to any other color. You can find couple of examples in Matlab documentation itself.
I have tried to segment and change the green color, below code might not work on your image, but it's possible to get decent results I guess.
rgbImage = imread('peppers.png');
figure, imshow(rgbImage);
title('Original Image');
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
%% Now lets take the difference of each the channels.
% these subtracted images will be used to mask the segmented area.
% If you are curious, plot them and see how they look!!
red_subtract_grn = redChannel-greenChannel;
red_subtract_blue = redChannel-blueChannel;
grn_subtract_blue = greenChannel - blueChannel;
red_add_grn = double(redChannel)+double(greenChannel)+double(blueChannel);
%% Lets segment the green color by filtering/thresholding technique,
% we need to choose the index number according to rgbImage, one should tweak a bit to get better results. (These
% numbers worked well for 'peppers.jpg' image.I have used indexing since its
% very neat and faster, alternatively you can use find() also).
try_mask = ones(size(rgbImage(:,:,1))); %Initialize mask image.
try_mask(red_subtract_blue < 7) = 0; %remove background
try_mask = medfilt2(try_mask,[4,4]); %Filter unwanted scattered pixels.
try_mask(red_subtract_grn > 40) = 0;
try_mask(red_add_grn > 500) = 0;
try_mask(grn_subtract_blue < 20) = 0;
try_mask(blueChannel > 80) = 0;
try_mask = medfilt2(try_mask,[8,8]);
%% Lets apply mask to remove green and blue pixels such that only red color will appear on the masked region.
greenChannel(try_mask > 0) = 0;
blueChannel(try_mask > 0) = 0;
rgbImage2 = cat(3, redChannel, greenChannel, blueChannel);
figure, imshow(rgbImage2);
title('After changing green to red')
green color to red color:
Upvotes: 1
Reputation: 32144
Your code swap Red color channel and Green color channel.
rgbImage2 = cat(3, greenChannel, redChannel, blueChannel);
Put green in place of red, and red in place of green (swap channels).
In order to keep green color channel unchanged, and replace (original) red with green, use the following:
rgbImage2 = cat(3, greenChannel, greenChannel, blueChannel);
Result for peppers.png
image:
rgbImage = imread('peppers.png');
[rows columns numberOfColorBands] = size(rgbImage);
imshow(rgbImage, []);
title('Original Color Image');
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
rgbImage2 = cat(3, greenChannel, greenChannel, blueChannel);
imshow(rgbImage2, []);
title('Green is now red');
Upvotes: 2