Help with MATLAB code for swapping red, blue, and green colors on RBG image. Cre
ID: 3774579 • Letter: H
Question
Help with MATLAB code for swapping red, blue, and green colors on RBG image.
Create a function called swap that has a single RGB image output and has 3 inputs: one for an RGB image input, and two others for character inputs such as ‘R’, ‘G’, or ‘B’. Your function must swap the colors specified in the call of the function, for example the code: out = swap(pic,‘R’,’B’) must swap the red and blue channels of the image pic and return the swapped image in out. Note that the green channel remains unaltered in the swapping process in this example. Your function must work exactly as shown above with character inputs specifying which color channels to swap.
Explanation / Answer
Here is the code for you:
function newImage = swap(oldImage, color1, color2)
figure();
imshow(oldImage); % show orignal
newImage = oldImage;
if color1 == 'R'
color1 = 1;
else if color1 == 'G'
color1 = 2;
else if color1 == 'B'
color1 = 3;
end
end
end
if color2 == 'R'
color2 = 1;
else if color2 == 'G'
color2 = 2;
else if color2 == 'B'
color2 = 3;
end
end
end
newImage(:,:,color2)=oldImage(:,:,color1);
newImage(:,:,color1)=oldImage(:,:,color2);
newImage(:,:,3)=oldImage(:,:,3);
figure();
imshow(newImage); % show new
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.