% CThruFog_alg2.m % % This m-file is the implementation of the second see-through-fog % algorithm. % % Author: Philip Edwards and Bill Parsons % Created: September 15, 2006 % Modified: September 18, 2006 % September 22, 2006 % Read the image into a matrix and convert it to black and white. %[y, map] = imread('mountains.jpg'); %z = rgb2gray(y); z = double(fog) * 255.0; [M, N] = size(z); %figure; imshow(y); figure; imshow(uint8(z)); % variable declaration C0 = 0.001; loop = 1; iter = 0; z = double(z)/255.0; defog = double(z); % distance matrix - R(k) - creation % For now, use the distance matrix produced by Induce fog for row = 1:M for col = 1:N R(row, col)= double(row/10.0); %R(row, col) = 1.001 - (double(row)/double(M))^2; end end % First, set C0 to the maximum pixel value of the image. for row = 1:M for col = 1:N if(defog(row, col) > C0) C0 = defog(row, col); end end end % Next, set B to the initial guess. % Q: should we guess, or apply an equation? B = 0.0 *10^-1; % Third,compute F(k) accroding to: F(k) = [I(k)/C0 - 1] * exp(B * R(k)) + 1 % Note: this step is the beginning of a loop. while (loop > 0 && iter < 100) % debug variable iter = iter + 1; for row = 1:M for col = 1:N defog(row, col) = double((z(row, col)/C0 - 1.0) * exp(B * double(R(row, col))) + 1.0); end end % Fourth, find the minimum value of F(k). % First, set it to a high value. % Note: need to record the coordinates k_min = 1.0; for row = 1:M for col = 1:N if(defog(row, col) < k_min) k_min = defog(row, col); x_min = row; y_min = col; end end end % Fifth, calculate the new B value according to: % B = -1/R(k_min)[ln(1 - I(k_min)/C0)] B = (-1.0/R(x_min, y_min)) * log(1.0 - z(x_min, y_min)/C0); % After the fifth step, evaluate the loop condition if (k_min == 0.000) loop = 0; end end %Recalculate the defogged image using the newest variables. for row = 1:M for col = 1:N defog(row, col) = double((z(row, col)/C0 - 1.0) * exp(B * double(R(row, col))) + 1.0); end end % The image is defogged. Display it to the screen. figure; imshow(uint8(defog * 255.0)) % This is an attempt at color definition. %for row = 1:M % for col = 1:N % col_im(row, col, 1) = 10.0/3.0 * defog(row, col); % col_im(row, col, 2) = 1.0/.59 * defog(row, col); % col_im(row, col, 3) = 1.0/.11 * defog(row, col); % end %end %Display the color image %figure; %imshow(uint8(col_im * 255.0))