The code is as follows:
# include & lt; Iostream>
# include & lt; Stdlib. H>
# include & lt; Time. H>
# include & lt; Cmath>
# include & lt; Algorithm>
# include & lt; Opencv2 \ opencv. Hpp>
Using the namespace CV;
using namespace std;
Typedef struct Pixel//define a type alias, not just a simple macro substitution, can be used as a statement of pointer type multiple objects at the same time,
{
Int x, y;
Float data;
} Pixel;
Bool structCmp (const Pixel& A, const Pixel& B)
{
Return a. d. ata & gt; B.d ata.//item first-just descending
}
Mat getAdd (Mat& Parimg Mat& Perimg);
Mat getSubstract (Mat& Parimg Mat& Perimg);
Mat getA (Mat& Parimg Mat& Perimg, float * array);
Mat recover (Mat& Total Mat& A, float * array);
Int main ()
{
String loc1="? F: \ project \ \ 865 np project data tif ";
String loc2="? F: \ \ project \ project data 490 p. tif ";
String name="forest";
Clock_t start and finish;//
Double duration;
cout <"A defog program" & lt;
Mat src1=imread (loc1);
Mat src2=imread (loc2);
Mat parimage=Mat (src1. The size (), CV_16UC3);
Mat perimage=Mat (src2. The size (), CV_16UC3);
Src1. ConvertTo (parimage CV_16UC3, 255.0);
Src2. ConvertTo (perimage CV_16UC3, 255.0);
Imshow (" 865 np ", parimage);
Imshow (" 490 p ", perimage);
cout <"Input hazy image" & lt;
Int rows=parimage. Rows;
Int cols=parimage. Cols;
Mat newparimage; Mat newperimage;
Parimage. ConvertTo (newparimage CV_32FC3, 1.0/65535.0, 0).
Perimage. ConvertTo (newperimage CV_32FC3, 1.0/65535.0, 0).
Mat totalimage (rows, cols, CV_32FC3);
Mat diffimage (rows, cols, CV_32FC3);
Mat A (rows, cols, CV_32FC3);
Mat recoverimg (rows, cols, CV_32FC3);
Totalimage=getAdd (newparimage newperimage);
Diffimage=getSubstract (newparimage newperimage);
Int nr=rows; Int nl=cols.
Start=clock ();
//estimate Airlight
//open a struct array dark passage, and then sort, take maximum 0.1%, use structure stored in the body of the original coordinates in the original image take
cout <"Estimating airlight." & lt;
The split (diffimage, planes);
Mat rchannel=planes [2];
Int pixelTot=cols rows * * 0.001;
Float * Apar=new float [3]. Float * Aper=new float [3].
Toppixels Pixel * and * allpixels;
Toppixels=new Pixel [pixelTot];
Allpixels=new Pixel [rows * cols];
For (unsigned int r=0; R & lt; Rows; R++)
{
Const uchar * data=https://bbs.csdn.net/topics/rchannel.ptr
For (unsigned int c=0; C & lt; Cols. C + +)
{
Allpixels [r] * cols + c. Data=https://bbs.csdn.net/topics/* data;
Allpixels [r] * cols + c. X=r.
Allpixels [r] * cols + c. Y=c;
}
}
STD: : sort (allpixels allpixels + rows * cols, structCmp);
Memcpy (toppixels allpixels, pixelTot * sizeof (Pixel));
Float tmp_par_A [3]. Float tmp_per_A [3].
Float A_par_r A_par_g, A_par_b avg1, maximum1=0;
Int idx1 idy1, max_x1 max_y1;
for (int i=0; i
Float A_par_sum_r=0, A_par_sum_g=0, A_par_sum_b=0;
Idx1=allpixels [I]. X; Idy1=allpixels [I] y;
Const uchar * pardata=https://bbs.csdn.net/topics/newparimage.ptr
Pardata +=3 * idy1;
A_par_b=* pardata++;
A_par_g=* pardata++;
A_par_r=* pardata++;
A_par_sum_b +=A_par_b;
A_par_sum_g +=A_par_g;
A_par_sum_r +=A_par_r;
Tmp_par_A [0]=A_par_sum_b/pixelTot;
Tmp_par_A [1]=A_par_sum_g/pixelTot;
Tmp_par_A [2]=A_par_sum_r/pixelTot;
}
Float A_per_r A_per_g, A_per_b avg2, maximum2=0;
Int idx2 idy2, max_x2 max_y2;
for (int i=0; i
Float A_per_sum_r=0, A_per_sum_g=0, A_per_sum_b=0;
Idx2=allpixels [I]. X; Idy2=allpixels [I] y;
Const uchar * perdata=https://bbs.csdn.net/topics/newperimage.ptr
Perdata +=3 * idy2;
A_per_b=* perdata++;
A_per_g=* perdata++;
A_per_r=* perdata++;
A_per_sum_b +=A_per_b;
A_per_sum_g +=A_per_g;
A_per_sum_r +=A_per_r;
Tmp_per_A [0]=A_per_sum_b/pixelTot;
Tmp_per_A [1]=A_per_sum_g/pixelTot;
Tmp_per_A [2]=A_per_sum_r/pixelTot;
}
The delete [] toppixels;
The delete [] allpixels;
Float P [3]; Float Ainf [3].
for (int i=0; i <3; I++)
{
P=(tmp_per_A [I] [I] - tmp_par_A [I])/(tmp_per_A tmp_par_A [I] + [I]);
Ainf [I]=tmp_per_A tmp_par_A [I] + [I];
}
A=getA (newparimage newperimage, P);
cout <"Start recovering." & lt;
cout <"Recovering finished." & lt;
Finish=clock ();
Duration=(double) (finish - start);
cout