/exercise1/src/filter_highcontrast.m

http://twenties-video-filter.googlecode.com/ · MATLAB · 57 lines · 14 code · 4 blank · 39 comment · 2 complexity · 13d85f5e0d4749a3d6848342c27d5365 MD5 · raw file

  1. % FILTER_HIGHCONTRAST(VIDEO, DX, DY) increases the contrast of the current
  2. % frame (video.frame(1).filtered) by moving the intensity values v of the image
  3. % towards 0 and 1 using a mapping functions f(v).
  4. %
  5. % VIDEO: a structure containing an array of frames where frame(1)
  6. % contains the most current frame.
  7. %
  8. % DX/DY: settings for constructing the mapping function f(v)
  9. %
  10. % VIDEO = FILTER_HIGHCONTRAST(VIDEO, DX, DY) returns the original video
  11. % structure with the updated current video.frame(1).filtered. Each filter
  12. % takes video.frame(1).filtered as input and writes the result back to this
  13. % array.
  14. %
  15. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  16. %
  17. % IMPLEMENTATION:
  18. % Transform the frame into HSV space, apply the step function to each
  19. % value and transform it back to RGB space.
  20. %
  21. % PHYSICAL BACKGROUND:
  22. % The human eye has a much wider contrast range than a static
  23. % photography could ever have, because it adapts to the intensity
  24. % of the light.
  25. % We can simulate that effect by artificially enhancing the contrast
  26. % of the image. We dim intensities below a certain threshold and
  27. % lighten intensities above the threshold. By doing so, information
  28. % is lost due to quantisation after compression, but the global contrast
  29. % ratio of the image is increased.
  30. % In black/white filming and photography the same effect could be
  31. % achieved by using various color filters in front of the lense.
  32. % For example, a yellow or orange filter could increase the contrast
  33. % between clouds and sky by filtering out the blue, which made the
  34. % sky more black in contrast to the white clouds.
  35. %
  36. % RANGE VALUES FOR PARAMETERS:
  37. % dy and dx defines a step function for contrast. The range for dx
  38. % and dy can be between 0 and 1. Low dx and dy means nearly no
  39. % contrast change.
  40. function video = filter_highcontrast(video, dx, dy)
  41. cmap = rgb2hsv(video.frame(1).filtered);
  42. cmap(:,:,3) = arrayfun(@(x) step_function(x, dx, dy), cmap(:,:,3));
  43. video.frame(1).filtered = hsv2rgb(cmap);
  44. end
  45. function value = step_function(value, dx, dy)
  46. if value <= dx
  47. value = value * dy / dx;
  48. elseif value <= 1-dx
  49. value = dy + (value-dx) * (1-2*dy)/(1-2*dx);
  50. else
  51. value = 1-dy + (value-(1-dx)) * dy / dx;
  52. end
  53. end