/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
- % FILTER_HIGHCONTRAST(VIDEO, DX, DY) increases the contrast of the current
- % frame (video.frame(1).filtered) by moving the intensity values v of the image
- % towards 0 and 1 using a mapping functions f(v).
- %
- % VIDEO: a structure containing an array of frames where frame(1)
- % contains the most current frame.
- %
- % DX/DY: settings for constructing the mapping function f(v)
- %
- % VIDEO = FILTER_HIGHCONTRAST(VIDEO, DX, DY) 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:
- % Transform the frame into HSV space, apply the step function to each
- % value and transform it back to RGB space.
- %
- % PHYSICAL BACKGROUND:
- % The human eye has a much wider contrast range than a static
- % photography could ever have, because it adapts to the intensity
- % of the light.
- % We can simulate that effect by artificially enhancing the contrast
- % of the image. We dim intensities below a certain threshold and
- % lighten intensities above the threshold. By doing so, information
- % is lost due to quantisation after compression, but the global contrast
- % ratio of the image is increased.
- % In black/white filming and photography the same effect could be
- % achieved by using various color filters in front of the lense.
- % For example, a yellow or orange filter could increase the contrast
- % between clouds and sky by filtering out the blue, which made the
- % sky more black in contrast to the white clouds.
- %
- % RANGE VALUES FOR PARAMETERS:
- % dy and dx defines a step function for contrast. The range for dx
- % and dy can be between 0 and 1. Low dx and dy means nearly no
- % contrast change.
-
- function video = filter_highcontrast(video, dx, dy)
- cmap = rgb2hsv(video.frame(1).filtered);
-
- cmap(:,:,3) = arrayfun(@(x) step_function(x, dx, dy), cmap(:,:,3));
-
- video.frame(1).filtered = hsv2rgb(cmap);
- end
-
- function value = step_function(value, dx, dy)
- if value <= dx
- value = value * dy / dx;
- elseif value <= 1-dx
- value = dy + (value-dx) * (1-2*dy)/(1-2*dx);
- else
- value = 1-dy + (value-(1-dx)) * dy / dx;
- end
- end