├── LICENSE ├── README.md ├── display_pyramid.m ├── downsample.m ├── example.m ├── exposure_fusion.m ├── gaussian_pyramid.m ├── house ├── A.jpg ├── B.jpg ├── C.jpg ├── D.jpg └── readme.txt ├── laplacian_pyramid.m ├── load_images.m ├── pyramid_filter.m ├── reconstruct_laplacian_pyramid.m └── upsample.m /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Tom Mertens, tom.mertens@gmail.com 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # exposure-fusion 2 | 3 | Implementation of Exposure Fusion in Matlab, as described in: 4 | 5 | "Exposure Fusion", 6 | Tom Mertens, Jan Kautz and Frank Van Reeth 7 | In proceedings of Pacific Graphics 2007 8 | 9 | "Exposure Fusion: A Simple and Practical Alternative to High Dynamic Range Photography. " 10 | Tom Mertens, Jan Kautz and Frank Van Reeth 11 | In Computer Graphics Forum, 28 (1) 161 - 171, 2009 12 | 13 | This code was originally written by Tom Mertens at Hasselt University, August 2007. 14 | Uploaded to GitHub in February 2015 15 | 16 | Author and main contact: Tom Mertens, tom.mertens@gmail.com 17 | 18 | 19 | To get started, go to 'example.m'. 20 | 21 | Subdirectory 'house' contains a sample dataset (photos by Min H. Kim, as used in the paper) 22 | -------------------------------------------------------------------------------- /display_pyramid.m: -------------------------------------------------------------------------------- 1 | % Display the contents of a pyramid, as returned by functions 2 | % 'laplacian_pyramid' or 'gaussian pyramid' 3 | % 4 | % tom.mertens@gmail.com, August 2007 5 | % 6 | 7 | function display_pyramid(pyr) 8 | 9 | L = length(pyr); 10 | r = size(pyr{1},1); 11 | c = size(pyr{1},2); 12 | k = size(pyr{1},3); 13 | R = zeros(r,2*c,k); 14 | 15 | offset = 1; 16 | for l = 1:L 17 | I = pyr{l}; 18 | r = size(I,1); 19 | c = size(I,2); 20 | R(1:r, offset:offset-1+c, :) = I; 21 | offset = offset + c; 22 | end 23 | 24 | if (min(R(:)) < 1e-5) 25 | %make negative values displayable 26 | a = min(R(:)); 27 | b = max(R(:)); 28 | R = (R - a) / (b - a); 29 | end 30 | 31 | figure; imshow(R); 32 | -------------------------------------------------------------------------------- /downsample.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % Downsampling procedure. 28 | % 29 | % Arguments: 30 | % grayscale I image 31 | % downsampling filter 'filter', should be a 1D separable filter. 32 | % 'border_mode' should be 'circular', 'symmetric', or 'replicate'. See 'imfilter'. 33 | % 34 | % If image width W is odd, then the resulting image will have width (W-1)/2+1, 35 | % Same for height. 36 | % 37 | % tom.mertens@gmail.com, August 2007 38 | % 39 | 40 | function R = downsample(I, filter) 41 | 42 | border_mode = 'symmetric'; 43 | 44 | % low pass, convolve with separable filter 45 | R = imfilter(I,filter,border_mode); %horizontal 46 | R = imfilter(R,filter',border_mode); %vertical 47 | 48 | % decimate 49 | r = size(I,1); 50 | c = size(I,2); 51 | R = R(1:2:r, 1:2:c, :); -------------------------------------------------------------------------------- /example.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | function example 28 | 29 | I = load_images('house'); 30 | 31 | figure('Name','Input sequence'); 32 | subplot(2,2,1); imshow(I(:,:,:,1)); 33 | subplot(2,2,2); imshow(I(:,:,:,2)); 34 | subplot(2,2,3); imshow(I(:,:,:,3)); 35 | subplot(2,2,4); imshow(I(:,:,:,4)); 36 | 37 | R = exposure_fusion(I,[1 1 1]); 38 | figure('Name','Result'); 39 | imshow(R); 40 | -------------------------------------------------------------------------------- /exposure_fusion.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % 28 | % Implementation of Exposure Fusion 29 | % 30 | % written by Tom Mertens, Hasselt University, August 2007 31 | % e-mail: tom.mertens@gmail.com 32 | % 33 | % This work is described in 34 | % "Exposure Fusion" 35 | % Tom Mertens, Jan Kautz and Frank Van Reeth 36 | % In Proceedings of Pacific Graphics 2007 37 | % 38 | % 39 | % Usage: 40 | % result = exposure_fusion(I,m); 41 | % Arguments: 42 | % 'I': represents a stack of N color images (at double 43 | % precision). Dimensions are (height x width x 3 x N). 44 | % 'm': 3-tuple that controls the per-pixel measures. The elements 45 | % control contrast, saturation and well-exposedness, respectively. 46 | % 47 | % Example: 48 | % 'figure; imshow(exposure_fusion(I, [0 0 1]);' 49 | % This displays the fusion of the images in 'I' using only the well-exposedness 50 | % measure 51 | % 52 | 53 | function R = exposure_fusion(I,m) 54 | 55 | r = size(I,1); 56 | c = size(I,2); 57 | N = size(I,4); 58 | 59 | W = ones(r,c,N); 60 | 61 | %compute the measures and combines them into a weight map 62 | contrast_parm = m(1); 63 | sat_parm = m(2); 64 | wexp_parm = m(3); 65 | 66 | if (contrast_parm > 0) 67 | W = W.*contrast(I).^contrast_parm; 68 | end 69 | if (sat_parm > 0) 70 | W = W.*saturation(I).^sat_parm; 71 | end 72 | if (wexp_parm > 0) 73 | W = W.*well_exposedness(I).^wexp_parm; 74 | end 75 | 76 | %normalize weights: make sure that weights sum to one for each pixel 77 | W = W + 1e-12; %avoids division by zero 78 | W = W./repmat(sum(W,3),[1 1 N]); 79 | 80 | % create empty pyramid 81 | pyr = gaussian_pyramid(zeros(r,c,3)); 82 | nlev = length(pyr); 83 | 84 | % multiresolution blending 85 | for i = 1:N 86 | % construct pyramid from each input image 87 | pyrW = gaussian_pyramid(W(:,:,i)); 88 | pyrI = laplacian_pyramid(I(:,:,:,i)); 89 | 90 | % blend 91 | for l = 1:nlev 92 | w = repmat(pyrW{l},[1 1 3]); 93 | pyr{l} = pyr{l} + w.*pyrI{l}; 94 | end 95 | end 96 | 97 | % reconstruct 98 | R = reconstruct_laplacian_pyramid(pyr); 99 | 100 | 101 | 102 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103 | 104 | % contrast measure 105 | function C = contrast(I) 106 | h = [0 1 0; 1 -4 1; 0 1 0]; % laplacian filter 107 | N = size(I,4); 108 | C = zeros(size(I,1),size(I,2),N); 109 | for i = 1:N 110 | mono = rgb2gray(I(:,:,:,i)); 111 | C(:,:,i) = abs(imfilter(mono,h,'replicate')); 112 | end 113 | 114 | % saturation measure 115 | function C = saturation(I) 116 | N = size(I,4); 117 | C = zeros(size(I,1),size(I,2),N); 118 | for i = 1:N 119 | % saturation is computed as the standard deviation of the color channels 120 | R = I(:,:,1,i); 121 | G = I(:,:,2,i); 122 | B = I(:,:,3,i); 123 | mu = (R + G + B)/3; 124 | C(:,:,i) = sqrt(((R - mu).^2 + (G - mu).^2 + (B - mu).^2)/3); 125 | end 126 | 127 | % well-exposedness measure 128 | function C = well_exposedness(I) 129 | sig = .2; 130 | N = size(I,4); 131 | C = zeros(size(I,1),size(I,2),N); 132 | for i = 1:N 133 | R = exp(-.5*(I(:,:,1,i) - .5).^2/sig.^2); 134 | G = exp(-.5*(I(:,:,2,i) - .5).^2/sig.^2); 135 | B = exp(-.5*(I(:,:,3,i) - .5).^2/sig.^2); 136 | C(:,:,i) = R.*G.*B; 137 | end 138 | 139 | 140 | -------------------------------------------------------------------------------- /gaussian_pyramid.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % Contruction of Gaussian pyramid 28 | % 29 | % Arguments: 30 | % image 'I' 31 | % 'nlev', number of levels in the pyramid (optional) 32 | % 33 | % tom.mertens@gmail.com, August 2007 34 | % 35 | 36 | function pyr = gaussian_pyramid(I,nlev) 37 | 38 | r = size(I,1); 39 | c = size(I,2); 40 | 41 | if ~exist('nlev') 42 | %compute the highest possible pyramid 43 | nlev = floor(log(min(r,c)) / log(2)); 44 | end 45 | 46 | % start by copying the image to the finest level 47 | pyr = cell(nlev,1); 48 | pyr{1} = I; 49 | 50 | % recursively downsample the image 51 | filter = pyramid_filter; 52 | for l = 2:nlev 53 | I = downsample(I,filter); 54 | pyr{l} = I; 55 | end -------------------------------------------------------------------------------- /house/A.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mericam/exposure-fusion/03e24695607550dce146ed58e2354a12c843eb99/house/A.jpg -------------------------------------------------------------------------------- /house/B.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mericam/exposure-fusion/03e24695607550dce146ed58e2354a12c843eb99/house/B.jpg -------------------------------------------------------------------------------- /house/C.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mericam/exposure-fusion/03e24695607550dce146ed58e2354a12c843eb99/house/C.jpg -------------------------------------------------------------------------------- /house/D.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mericam/exposure-fusion/03e24695607550dce146ed58e2354a12c843eb99/house/D.jpg -------------------------------------------------------------------------------- /house/readme.txt: -------------------------------------------------------------------------------- 1 | Images courtesy of Min H. Kim -------------------------------------------------------------------------------- /laplacian_pyramid.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % Contruction of Laplacian pyramid 28 | % 29 | % Arguments: 30 | % image 'I' 31 | % 'nlev', number of levels in the pyramid (optional) 32 | % 33 | % tom.mertens@gmail.com, August 2007 34 | % 35 | % 36 | % More information: 37 | % 'The Laplacian Pyramid as a Compact Image Code' 38 | % Burt, P., and Adelson, E. H., 39 | % IEEE Transactions on Communication, COM-31:532-540 (1983). 40 | % 41 | 42 | function pyr = laplacian_pyramid(I,nlev) 43 | 44 | r = size(I,1); 45 | c = size(I,2); 46 | 47 | if ~exist('nlev') 48 | % compute the highest possible pyramid 49 | nlev = floor(log(min(r,c)) / log(2)); 50 | end 51 | 52 | % recursively build pyramid 53 | pyr = cell(nlev,1); 54 | filter = pyramid_filter; 55 | J = I; 56 | for l = 1:nlev - 1 57 | % apply low pass filter, and downsample 58 | I = downsample(J,filter); 59 | odd = 2*size(I) - size(J); % for each dimension, check if the upsampled version has to be odd 60 | % in each level, store difference between image and upsampled low pass version 61 | pyr{l} = J - upsample(I,odd,filter); 62 | J = I; % continue with low pass image 63 | end 64 | pyr{nlev} = J; % the coarest level contains the residual low pass image 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /load_images.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % This procedure loads a sequence of images 28 | % 29 | % Arguments: 30 | % 'path', refers to a directory which contains a sequence of JPEG or PPM 31 | % images 32 | % 'reduce' is an optional parameter that controls downsampling, e.g., reduce = .5 33 | % downsamples all images by a factor of 2. 34 | % 35 | % tom.mertens@gmail.com, August 2007 36 | % 37 | 38 | function I = load_images(path, reduce) 39 | 40 | if ~exist('reduce') 41 | reduce = 1; 42 | end 43 | 44 | if (reduce > 1 || reduce <= 0) 45 | error('reduce must fulfill: 0 < reduce <= 1'); 46 | end 47 | 48 | % find all JPEG or PPM files in directory 49 | files = dir([path '/*.jpg']); 50 | N = length(files); 51 | if (N == 0) 52 | files = dir([path '/*.ppm']); 53 | N = length(files); 54 | if (N == 0) 55 | error('no files found'); 56 | end 57 | end 58 | 59 | % allocate memory 60 | sz = size(imread([path '/' files(1).name])); 61 | r = floor(sz(1)*reduce); 62 | c = floor(sz(2)*reduce); 63 | I = zeros(r,c,3,N); 64 | 65 | % read all files 66 | for i = 1:N 67 | 68 | % load image 69 | filename = [path '/' files(i).name]; 70 | im = double(imread(filename)) / 255; 71 | if (size(im,1) ~= sz(1) || size(im,2) ~= sz(2)) 72 | error('images must all have the same size'); 73 | end 74 | 75 | % optional downsampling step 76 | if (reduce < 1) 77 | im = imresize(im,[r c],'bicubic'); 78 | end 79 | 80 | I(:,:,:,i) = im; 81 | end 82 | -------------------------------------------------------------------------------- /pyramid_filter.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % This is a 1-dimensional 5-tap low pass filter. It is used as a 2D separable low 28 | % pass filter for constructing Gaussian and Laplacian pyramids. 29 | % 30 | % tom.mertens@gmail.com, August 2007 31 | % 32 | 33 | function f = pyramid_filter; 34 | f = [.0625, .25, .375, .25, .0625]; -------------------------------------------------------------------------------- /reconstruct_laplacian_pyramid.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % Reconstruction of image from Laplacian pyramid 28 | % 29 | % Arguments: 30 | % pyramid 'pyr', as generated by function 'laplacian_pyramid' 31 | % 32 | % tom.mertens@gmail.com, August 2007 33 | % 34 | % 35 | % More information: 36 | % 'The Laplacian Pyramid as a Compact Image Code' 37 | % Burt, P., and Adelson, E. H., 38 | % IEEE Transactions on Communication, COM-31:532-540 (1983). 39 | % 40 | 41 | function R = reconstruct_laplacian_pyramid(pyr) 42 | 43 | r = size(pyr{1},1); 44 | c = size(pyr{1},2); 45 | nlev = length(pyr); 46 | 47 | % start with low pass residual 48 | R = pyr{nlev}; 49 | filter = pyramid_filter; 50 | for l = nlev - 1 : -1 : 1 51 | % upsample, and add to current level 52 | odd = 2*size(R) - size(pyr{l}); 53 | R = pyr{l} + upsample(R,odd,filter); 54 | end 55 | -------------------------------------------------------------------------------- /upsample.m: -------------------------------------------------------------------------------- 1 | %{ 2 | Copyright (c) 2015, Tom Mertens 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | %} 26 | 27 | % Upsampling procedure. 28 | % 29 | % Argments: 30 | % 'I': greyscale image 31 | % 'odd': 2-vector of binary values, indicates whether the upsampled image 32 | % should have odd size for the respective dimensions 33 | % 'filter': upsampling filter 34 | % 35 | % If image width W is odd, then the resulting image will have width (W-1)/2+1, 36 | % Same for height. 37 | % 38 | % tom.mertens@gmail.com, August 2007 39 | % 40 | 41 | function R = upsample(I,odd,filter) 42 | 43 | % increase resolution 44 | I = padarray(I,[1 1 0],'replicate'); % pad the image with a 1-pixel border 45 | r = 2*size(I,1); 46 | c = 2*size(I,2); 47 | k = size(I,3); 48 | R = zeros(r,c,k); 49 | R(1:2:r, 1:2:c, :) = 4*I; % increase size 2 times; the padding is now 2 pixels wide 50 | 51 | % interpolate, convolve with separable filter 52 | R = imfilter(R,filter); %horizontal 53 | R = imfilter(R,filter'); %vertical 54 | 55 | % remove the border 56 | R = R(3:r - 2 - odd(1), 3:c - 2 - odd(2), :); 57 | 58 | --------------------------------------------------------------------------------