/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

  1. % FILTER_REMOVE_COLOR(VIDEO, MODE) removes the color information of the current
  2. % frame (video.frame(1).filtered). MODE can be either BW (function returns an RGB
  3. % image where only intensity values are considered) or SEPIA ( function returns
  4. % an RGB image transformed into a yellow-brownish image).
  5. %
  6. % VIDEO: a structure containing an array of frames where frame(1)
  7. % contains the most current frame.
  8. %
  9. % VIDEO = FILTER_REMOVE_COLOR(VIDEO, MODE) returns the original video structure
  10. % with the updated current video.frame(1).filtered. Each filter takes
  11. % video.frame(1).filtered as input and writes the result back to this array.
  12. %
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. %
  15. % IMPLEMENTATION:
  16. % bw: We switch to the HSV color space and set the saturation to 0.
  17. % So no color has enough saturation to differ from gray.
  18. % sepia: We multiply the RGB values of each pixel with a sepia
  19. % matrix.
  20. %
  21. % PHYSICAL BACKGROUND:
  22. % In the twenties there were no colors in films.
  23. % A color with no saturation is black, gray or white.
  24. % When a photograph is 60 years and older, the black color turns into
  25. % brown and white into yellow because of the UV radiation. This color
  26. % space is called sepia.
  27. % The sepia matrix multiplication transforms the color of the frame.
  28. function [video] = filter_remove_color(video, mode)
  29. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  30. % Black / White Mode
  31. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  32. if (strcmp(mode,'bw'))
  33. video.frame(1).filtered = black_white(video.frame(1).filtered);
  34. return;
  35. end
  36. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  37. % Sepia Mode
  38. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  39. if (strcmp(mode,'sepia'))
  40. video.frame(1).filtered = sepia(video.frame(1).filtered);
  41. return;
  42. end
  43. disp_error('Only ''bw'' and ''sepia'' mode supported!');
  44. %%
  45. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  46. % REMOVE COLOUR INFORMATION FROM AN RGB IMAGE
  47. %
  48. % 1) CONVERT img FROM RGB TO YCbCr COLORSPACE
  49. % 2) SET CHROMA CHANNELS APPROPRIATELY (NEUTRAL CHROMA CHANNELS)
  50. % 3) CONVERT IMAGE BACK TO RGB
  51. % 4) RETURN
  52. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  53. function [bw] = black_white(img)
  54. img = rgb2hsv(img);
  55. img(:,:,2) = 0;
  56. bw = hsv2rgb(img);
  57. end
  58. %%
  59. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  60. % CONVERT RGB IMAGE TO SEPIA
  61. %
  62. % 1) USE SEPIA CONVERSION MATRIX FOR CONVERTING RGB IMAGE img TO SEPIA
  63. % 2) RETURN CONVERTED IMAGE
  64. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  65. function [sepia] = sepia(img)
  66. sepia_matrix = [0.4 0.769 0.189; 0.349 0.686 0.168; 0.272 0.534 0.131];
  67. sepia = img;
  68. % for x=1:size(img,1)
  69. % for y =1:size(img,2)
  70. % sepia(x,y,:)= sepia_matrix * reshape(img(x,y,:), 3,1);
  71. % end
  72. % end
  73. sepia(:,:,1) = img(:,:,1)*sepia_matrix(1,1)+ img(:,:,2)*sepia_matrix(1,2)+ img(:,:,3)*sepia_matrix(1,3);
  74. sepia(:,:,2) = img(:,:,1)*sepia_matrix(2,1)+ img(:,:,2)*sepia_matrix(2,2)+ img(:,:,3)*sepia_matrix(2,3);
  75. sepia(:,:,3) = img(:,:,1)*sepia_matrix(3,1)+ img(:,:,2)*sepia_matrix(3,2)+ img(:,:,3)*sepia_matrix(3,3);
  76. end
  77. end