%Function Name: colorScreen % Inputs (3): - (char) a filename of a foreground im
ID: 3539995 • Letter: #
Question
%Function Name: colorScreen
% Inputs (3): - (char) a filename of a foreground image
% - (char) a filename of a background image
% - (double) a vector of length three of RGB values to replace
% Outputs (1): - (uint8) a uint8 array with the colorScreen effect applied
%
% Function Description:
% Given two images, write a function called "colorScreen" that will
% replace all pixels in the first image that match a given RGB value with
% the corresponding pixels in the second image. For example, if the
% following is called:
%
% colorScreen('img1.bmp','img2.bmp', [0 255 0]);
%
% A uint8 array should be returned that should look like 'img1.bmp', but
% with all of the pure green pixels replaced with the corresponding pixel
% in 'img2.bmp'.
%
% Notes:
% - You do not have to use a for loop in this problem. For loops are a
% very slow and inefficent due to the size of the images that must be
% looped though. Consider logical indexing instead.
% - The two input images will always be the same size.
%
% Test Cases:
% im1 = colorScreen('trex.bmp','ttower.bmp',[0 255 0]);
% imshow(im1)
% => should look like soln1.bmp
% im2 = colorScreen('lookit.bmp','pic.bmp',[237 28 36]);
% imshow(im2)
% => should look like soln2.bmp
Explanation / Answer
Save the following as colorScreen.m
function y = colorScreen(img1,img2,inp)
B(:,:,1)=(img2(:,:,1)==inp(1));
B(:,:,2)=(img2(:,:,2)==inp(2));
B(:,:,3)=(img2(:,:,3)==inp(3));
img1(~B)=0;
img2(B)=0;
y=img1+img2;
end
Also save the two images in the present working directory as im1.png and im2.png
Then run the following in the comand window
img1=imread('im1.png');
img2=imread('im2.png');
inp=[0,255,0];
imgo = colorScreen(img1,img2,inp);
imshow(imgo)
Be careful while loading images, load correct images in correct sequence
You can change the input value as you wish to.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.