标签:
上个月用基于中值滤波的暗通道图像去雾算法在matlab中实现了,现在想把它改造成C++的代码,结果效果明显不对,特此发出来,希望能找到问题。
1 #include <opencv2/core/core.hpp> 2 #include <opencv2/highgui/highgui.hpp> 3 #include <opencv2/highgui/highgui_c.h> 4 #include <opencv2/imgproc/types_c.h> 5 #include <opencv2/imgproc/imgproc_c.h> 6 #include <opencv2/imgproc/imgproc.hpp> 7 #include <opencv2/imgcodecs/imgcodecs_c.h> 8 #include <iostream> 9 10 using namespace cv; 11 using namespace std; 12 Mat Iimage; 13 Mat Oimage; 14 int MAX_KERNEL_LENGTH = 31; 15 double p=0.2; 16 int c1; 17 int c2; 18 int c3; 19 int cloud() 20 { 21 int a; 22 Iimage = imread("D:\\img\\train.bmp", CV_LOAD_IMAGE_COLOR); // Read the file 23 24 25 ///*测试代码开始*/ 26 //for(int i=0;i<1;i++) 27 //{ 28 // for(int j=0;j<200;j++) 29 // { 30 // cout<<j<<":"<<(float)Iimage.at<Vec3b>(i,j)[0]<<endl; 31 // } 32 //} 33 ///*测试代码结束*/ 34 Mat M; 35 cvtColor(Iimage,M,CV_BGR2GRAY);//得到灰度图 36 /*测试代码*/ 37 imshow( "灰度", M ); 38 39 /*测试代码*/ 40 //cvtColor( image, gray_image, CV_BGR2GRAY ); //将RGB图像转化为灰度格式 41 for(int i=0;i<Iimage.rows;i++) 42 { 43 uchar *cp=M.ptr<uchar>(i); 44 for(int j=0;j<Iimage.cols;j++) 45 { 46 if(Iimage.at<Vec3b>(i,j)[0]<Iimage.at<Vec3b>(i,j)[1]) 47 { 48 if(Iimage.at<Vec3b>(i,j)[0]<Iimage.at<Vec3b>(i,j)[2]) 49 { 50 c1=Iimage.at<Vec3b>(i,j)[0]; 51 c2=Iimage.at<Vec3b>(i,j)[2]; 52 cp[j]=(int)Iimage.at<Vec3b>(i,j)[0]; 53 } 54 else 55 { 56 57 cp[j]=(int)Iimage.at<Vec3b>(i,j)[2]; 58 } 59 } 60 else 61 { 62 if((float)Iimage.at<Vec3b>(i,j)[1]<(float)Iimage.at<Vec3b>(i,j)[2]) 63 { 64 65 cp[j]=(int)Iimage.at<Vec3b>(i,j)[1]; 66 67 } 68 else 69 { 70 71 72 cp[j]=(int)Iimage.at<Vec3b>(i,j)[2]; 73 74 } 75 } 76 } 77 } 78 /*测试代码*/ 79 imshow( "测试", M ); 80 waitKey(0); 81 /*测试代码*/ 82 83 ///*测试代码开始*/ 84 //uchar *cp=M.ptr<uchar>(0); 85 //for(int j=0;j<200;j++) 86 // { 87 // cout<<"j="<<j+1<<":"<<(float)cp[j]<<endl; 88 // } 89 /*测试代码结束*/ 90 /*中值滤波*/ 91 Mat Am=Iimage; 92 93 for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) 94 { medianBlur ( M,Am, i ); 95 // if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } 96 } 97 /*中值滤波结束*/ 98 99 Mat temp=Am; 100 101 /*求Am-m*/ 102 for(int i=0;i<Iimage.rows;i++) 103 { 104 uchar *cp=temp.ptr<uchar>(i); 105 uchar *cp1=Am.ptr<uchar>(i); 106 uchar *cp2=M.ptr<uchar>(i); 107 for(int j=0;j<Iimage.cols;j++) 108 { 109 110 if((float)cp1[j]>(float)cp2[j]) 111 { 112 113 /*uchar *cp1=Am.ptr<uchar>(i); 114 uchar *cp2=M.ptr<uchar>(i);*/ 115 cp[j]=(int)cp1[j]-(int)cp2[j]; 116 117 } 118 else 119 { 120 /*uchar *cp=temp.ptr<uchar>(i); 121 uchar *cp1=Am.ptr<uchar>(i); 122 uchar *cp2=M.ptr<uchar>(i);*/ 123 cp[j]=(int)cp2[j]-(int)cp1[j]; 124 } 125 } 126 } 127 128 129 130 /*求Am-m结束*/ 131 132 133 /*中值滤波*/ 134 Mat Median=Am; 135 136 for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) 137 { medianBlur ( temp,Median, i ); 138 //if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } 139 } 140 /*中值滤波结束*/ 141 142 Mat B=Median; 143 144 for(int i=0;i<Iimage.rows;i++) 145 { 146 uchar *cp=B.ptr<uchar>(i); 147 uchar *cp1=Am.ptr<uchar>(i); 148 uchar *cp2=Median.ptr<uchar>(i); 149 for(int j=0;j<Iimage.cols;j++) 150 { 151 152 cp[j]=(int)cp1[j]-(int)cp2[j]; 153 154 } 155 } 156 Mat F=Am; 157 double tmin=0 ,tmax=0; 158 for(int i=0;i<Iimage.rows;i++) 159 { 160 uchar *cp=B.ptr<uchar>(i); 161 uchar *cp1=M.ptr<uchar>(i); 162 uchar *cp3=F.ptr<uchar>(i); 163 for(int j=0;j<Iimage.cols;j++) 164 { 165 166 if(p*(float)cp[j]<(float)cp1[j]) 167 { 168 if(p*(float)cp[j]<0) 169 { 170 tmin=0; 171 } 172 else 173 { 174 tmin=p*(float)cp[j]; 175 } 176 } 177 else 178 { 179 if(p*(float)cp1[j]<0) 180 { 181 tmin=0; 182 } 183 else 184 { 185 tmin=p*(float)cp1[j]; 186 } 187 } 188 /*if(tmin<0) 189 { 190 tmax=tmin; 191 } 192 else 193 { 194 tmax=0; 195 }*/ 196 197 cp3[j]=tmin; 198 } 199 } 200 201 int moodX=3; 202 int moodY=3; 203 204 Mat M1=Am; 205 Mat Mn=Am; 206 /*暗通道*/ 207 //每个像素取三通道最小值 208 for (int i=4;i<Iimage.rows-4;i++) 209 { 210 for(int j=4;j<Iimage.cols-4;j++) 211 { 212 //每个像素四周三排的最小值存在M1中 213 for(int i1=i-3;i1<i+3;i1++) 214 { 215 uchar *cp=M1.ptr<uchar>(i1); 216 for(int j1=j-3;j1<j+3;j1++) 217 { 218 if((float)Iimage.at<Vec3b>(i1,j1)[0]<(float)Iimage.at<Vec3b>(i1,j1)[1]) 219 { 220 if ((float)Iimage.at<Vec3b>(i1,j1)[0]<(float)Iimage.at<Vec3b>(i1,j1)[2]) 221 { 222 223 cp[j1]=(float)Iimage.at<Vec3b>(i1,j1)[0]; 224 225 } 226 else 227 { 228 229 cp[j1]=(float)Iimage.at<Vec3b>(i1,j1)[2]; 230 } 231 } 232 else 233 { 234 if((float)Iimage.at<Vec3b>(i1,j1)[1]<(float)Iimage.at<Vec3b>(i1,j1)[2]) 235 { 236 237 cp[j1]=(float)Iimage.at<Vec3b>(i1,j1)[1]; 238 } 239 else 240 { 241 242 cp[j1]=(float)Iimage.at<Vec3b>(i1,j1)[2]; 243 } 244 } 245 } 246 } 247 uchar *cpMn=Mn.ptr<uchar>(i); 248 uchar *cpM1=M1.ptr<uchar>(i); 249 cpMn[j]=(float)cpM1[j]; 250 for (int i1=i-3;i1<i+3;i1++) 251 { 252 uchar *cpMn=Mn.ptr<uchar>(i); 253 uchar *cpM1=M1.ptr<uchar>(i1); 254 for (int j1=j-3;j1<j+3;j1++) 255 { 256 257 if ((float)cpMn[j]>(float)cpM1[j1]) 258 { 259 cpMn[j]=(float)cpM1[j1]; 260 } 261 //获取M1中最小值存在M中 262 263 } 264 } 265 } 266 } 267 /*暗通道结束*/ 268 269 Mat Dark=Mn; 270 int countDark=Iimage.rows*Iimage.cols/1000; 271 Mat IMtoGray=Am; 272 cvtColor(Iimage,IMtoGray,CV_BGR2GRAY);//得到灰度图 273 Mat CopyDark=Dark; 274 int *Dcoordinate; 275 Dcoordinate=new int[countDark]; 276 int MMax=0; 277 int MMaxI=0; 278 int MMaxJ=0; 279 double Mtemp=0; 280 for (int ic=0;ic<countDark;ic++) 281 { 282 /*求最大值*/ 283 for (int i=0;i<Iimage.rows;i++) 284 { 285 uchar *cpCopyDark=CopyDark.ptr<uchar>(i); 286 for(int j=0;j<Iimage.cols;j++) 287 { 288 289 290 if((float)cpCopyDark[j]>MMax) 291 { 292 MMax=(float)cpCopyDark[j]; 293 MMaxI=i; 294 MMaxJ=j; 295 } 296 } 297 } 298 299 /*求最大值结束*/ 300 301 Mtemp=(MMaxI)*Iimage.rows+MMaxJ+1; 302 Dcoordinate[ic]=Mtemp; 303 uchar *cpCopyDark=CopyDark.ptr<uchar>(MMaxI); 304 cpCopyDark[MMaxJ]=0; 305 } 306 Mat LuvIM=Iimage; 307 cvtColor( Iimage, LuvIM, CV_BGR2Luv ); 308 //M1.at<float>(i1,j1)=LuvIM.at<Vec3b>(i1,j1)[0]; 309 int *LightIM; 310 LightIM=new int[countDark]; 311 for (int ic=0;ic<countDark;ic++) 312 { 313 LightIM[ic]=LuvIM.at<Vec3b>((float)(Dcoordinate[ic]/Iimage.rows),Dcoordinate[ic]%Iimage.rows-1)[0]; 314 } 315 int LightMax=0; 316 for (int ic=0;ic<countDark;ic++) 317 { 318 /*求最大值*/ 319 320 if(LightIM[ic]>LightMax) 321 { 322 LightMax=LightIM[ic]; 323 } 324 325 /*求最大值结束*/ 326 } 327 double A=LightMax; 328 Mat J=LuvIM; 329 float x1,x2,x3,x4,x5; 330 for (int i=0;i<Iimage.rows;i++) 331 { 332 uchar *cpF=F.ptr<uchar>(i); 333 A=74.2852; 334 for (int j=0;j<Iimage.cols;j++) 335 { 336 x1=(float)LuvIM.at<Vec3b>(i,j)[0]; 337 x2=(float)cpF[j]; 338 x3=(float)cpF[j]; 339 x4=(1-(float)cpF[j])/A; 340 x5=(x1-x2)/x4; 341 J.at<Vec3b>(i,j)[0]=((float)LuvIM.at<Vec3b>(i,j)[0]-(float)cpF[j])/((1-(float)cpF[j])/A); 342 } 343 } 344 345 Mat resultImage=Iimage; 346 cvtColor( J, resultImage, CV_Luv2BGR ); 347 imshow( "图像去雾", resultImage ); 348 waitKey(0); 349 return 0; 350 }
标签:
原文地址:http://www.cnblogs.com/PHPer-Wu/p/4471619.html