Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote