The algorithm is based on the Fog Image Imaging model:

where I (x) is the image to be fog, J (x) is the fog-free image we want to restore, a is ambient light, t (x) is the transmittance. Now known I (x), seek the target value J (x). Where ambient light A is calculated, refer to the Dark Channel de-fog algorithm. Defines the fog concentration of f (x, y) as an image, corresponding to a (1-T (x)) in the model.

Calculate f (x, Y)

The first step:

function SD = MyCloud (IM)%mycloud Summary of this function goes here% detailed EXPL Anation goes hererow=30;line=30;p=0.2; [H, W, c] = size (IM);% gets the original three channel r= IM (:,:, 1); G= IM (:,:, 2); B= IM (:,:, 3);% gets the original three channels end% each pixel takes three channel minimum for i=1:h for j=1:w if R (i,j) < g (I,J) if R (i,j) <b (i,j) M (i,j) =r (I,J); else M (i,j) =b (I,J); end else if G (i,j) <b (i,j) M (i,j) =g (I,J); else M (i,j) =b (I,J); end End EndEnd

Here im is the incoming parameter, that is, read the original image, where the h,w,c are the number of rows, columns and channels respectively.

Record the minimum value of three channels per point with M

Step Two:

% will M median filter am=medfilt2 (M,[row,line]);%am=m;

The M median filter, AM is the median filter after the result, smooth while preserving the m edge information.

Step Three:

For i=1:h for j=1:w if Am (i,j) >m (i,j) temp (i,j) =am (i,j)-M (i,j); Else temp (i,j) =m (i,j)-am (i,j); End %temp (i,j) =abs (Am (I,j)-M (i,j)) ; EndEnd

Calculation | Am (x, y)-M (x, y) |, the result exists in temp

MEDIAN=MEDFILT2 (Temp,[row,line]);

Filter the median temp and the result is median.

% calculation B (x, y) for i=1: H for j=1: w B (i,j)=am (i, j)-median (i,j); EndEnd

- Calculate b (x, y), B (x, y) =am (x, y)-median (| Am (x, y)-M (x, y) |). Considering that there may be no fog in the texture area with good contrast, this part of the area does not need to do fog processing, so I (x, y) minus m (x, y) local standard deviation; Am (x, y)-M (x, y) | Performs median filtering to estimate the local standard deviation of M (x, y), which guarantees robustness of the standard deviation estimate, such as when a small foreground target is suddenly present in an area of roughly the same depth, Using this method to estimate fog concentrations avoids keeping a certain amount of fog around small targets.

Fourth Step:

%f=max (min (p*b,m), 0); for i=1:h for j=1:w F (i,j) =max (min (p*b (i,j), M (i,j)), 0); EndEnd

- Calculates f (x, y) =max (min (p*b (x, y), M (x, y), 0), p is the parameter that controls the ability to remove fog, between 0~1, the greater the value of the fog the stronger the ability.

Here the minimum value between P*b and M is compared with 0 to prevent the case of negative numbers, if the result is 0 smaller then take 0.

Fifth Step:

Dark=mydark (IM, H, W, c);

The specific Mydark function how to run I will be in detail in another blog, here just need to know that he is back to the dark channel map can be

Get the dark channel diagram of IM dark in existence

%countdark is the number of points of 0.1% Countdark=floor (h*w/1000);%I Mtogray is the gray image Imtogray=rgb2gray (IM) of the original picture; The Copydark=dark;%ic matrix is the sequence number of the highlight, dcoordinate the position of the highlight Dcoordinate=zeros (Floor (COUNTDARK/1), 1); for Ic=1:countdark Temp=find (Copydark==max (Max (Copydark))); Dcoordinate (ic,1) =temp (n); Copydark (temp) =0;end

Records the position of the lightest 0.1% point in the Dark channel graph.

Luvim=colorspace (' Luv<-rgb', IM); Lightim=zeros (Floor (COUNTDARK/1), 1); For ICS =1:countdark x =ceil (Dcoordinate (ic,1)/w); y =rem (Dcoordinate (ic,1), W); if y<1 y=w; End Lightim (ic,1)=luvim (x,y,1); End

Convert the original image to a Luv chart, recording the luminance values of these 1 per thousand points,

J=luvim; A=max (Max (Lightim));

The maximum luminance value in the middle is a.

Sixth step:

According to the formula: Get the image after the fog.

Image de-fog based on dark channel