/exercise1/src/filter_remove_color.m
http://twenties-video-filter.googlecode.com/ · MATLAB · 87 lines · 23 code · 10 blank · 54 comment · 2 complexity · e03ff33619db667ab712ee6ac8a06832 MD5 · raw file
- % FILTER_REMOVE_COLOR(VIDEO, MODE) removes the color information of the current
- % frame (video.frame(1).filtered). MODE can be either BW (function returns an RGB
- % image where only intensity values are considered) or SEPIA ( function returns
- % an RGB image transformed into a yellow-brownish image).
- %
- % VIDEO: a structure containing an array of frames where frame(1)
- % contains the most current frame.
- %
- % VIDEO = FILTER_REMOVE_COLOR(VIDEO, MODE) returns the original video structure
- % with the updated current video.frame(1).filtered. Each filter takes
- % video.frame(1).filtered as input and writes the result back to this array.
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % IMPLEMENTATION:
- % bw: We switch to the HSV color space and set the saturation to 0.
- % So no color has enough saturation to differ from gray.
- % sepia: We multiply the RGB values of each pixel with a sepia
- % matrix.
- %
- % PHYSICAL BACKGROUND:
- % In the twenties there were no colors in films.
- % A color with no saturation is black, gray or white.
- % When a photograph is 60 years and older, the black color turns into
- % brown and white into yellow because of the UV radiation. This color
- % space is called sepia.
- % The sepia matrix multiplication transforms the color of the frame.
- function [video] = filter_remove_color(video, mode)
-
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Black / White Mode
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- if (strcmp(mode,'bw'))
- video.frame(1).filtered = black_white(video.frame(1).filtered);
- return;
- end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Sepia Mode
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- if (strcmp(mode,'sepia'))
- video.frame(1).filtered = sepia(video.frame(1).filtered);
- return;
- end
-
- disp_error('Only ''bw'' and ''sepia'' mode supported!');
-
-
-
-
- %%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % REMOVE COLOUR INFORMATION FROM AN RGB IMAGE
- %
- % 1) CONVERT img FROM RGB TO YCbCr COLORSPACE
- % 2) SET CHROMA CHANNELS APPROPRIATELY (NEUTRAL CHROMA CHANNELS)
- % 3) CONVERT IMAGE BACK TO RGB
- % 4) RETURN
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- function [bw] = black_white(img)
- img = rgb2hsv(img);
- img(:,:,2) = 0;
- bw = hsv2rgb(img);
- end
- %%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % CONVERT RGB IMAGE TO SEPIA
- %
- % 1) USE SEPIA CONVERSION MATRIX FOR CONVERTING RGB IMAGE img TO SEPIA
- % 2) RETURN CONVERTED IMAGE
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- function [sepia] = sepia(img)
- sepia_matrix = [0.4 0.769 0.189; 0.349 0.686 0.168; 0.272 0.534 0.131];
- sepia = img;
- % for x=1:size(img,1)
- % for y =1:size(img,2)
- % sepia(x,y,:)= sepia_matrix * reshape(img(x,y,:), 3,1);
- % end
- % end
- sepia(:,:,1) = img(:,:,1)*sepia_matrix(1,1)+ img(:,:,2)*sepia_matrix(1,2)+ img(:,:,3)*sepia_matrix(1,3);
- sepia(:,:,2) = img(:,:,1)*sepia_matrix(2,1)+ img(:,:,2)*sepia_matrix(2,2)+ img(:,:,3)*sepia_matrix(2,3);
- sepia(:,:,3) = img(:,:,1)*sepia_matrix(3,1)+ img(:,:,2)*sepia_matrix(3,2)+ img(:,:,3)*sepia_matrix(3,3);
- end
- end