本文主要是介绍图像去雾完结(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很久没有更新去雾相关的博客了,第一篇的何凯明博士的暗通道去雾的论文,之前也有实现过,前面的博客有一定的说明,在第一篇去雾系列博客中有相关的算法讲解,都是个人的想法,https://mp.csdn.net/postedit/81915054。但是也没有相关源码的说明,在这里我把去雾的源码发布出来,不过也是其他博主的成果,我只是转载过来的,多谢大家提点。
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include<opencv2/cv.h>
#include<time.h>
#include<omp.h>
#include <vector>using namespace std;
using namespace cv;Mat autolevel(Mat matface,double dlowcut,double dhighcut)
{uchar allmap[256*3] = {0};//double dlowcut = 0.5;//double dhighcut = 0.5;long T_1 = clock();vector<Mat> rgb_planes;split(matface,rgb_planes);Mat HistBlue,HistGreen,HistRed;int histSize = 256;float range[] = { 0, 255 } ;const float* histRange = { range };bool uniform = true; bool accumulate = false;calcHist( &rgb_planes[0], 1, 0, Mat(), HistRed, 1,&histSize, &histRange, uniform, accumulate );calcHist( &rgb_planes[1], 1, 0, Mat(), HistGreen, 1, &histSize, &histRange, uniform, accumulate );calcHist( &rgb_planes[2], 1, 0, Mat(), HistBlue, 1, &histSize, &histRange, uniform, accumulate );printf("\n hist time %f ms.\n",(double)(clock() - T_1));int PixelAmount = matface.rows*matface.cols;//printf("%d\n",PixelAmount);float isum = 0;// bluelong T_2 = clock();int iminblue=0;int imaxblue=0;for (int y = 0;y<256;y++){isum= isum + HistBlue.at<const float>(y);//printf("%d\n",(int)HistBlue.at<int>(y));//isum= isum+HistBlue[y];if (isum>=PixelAmount*dlowcut*0.01){iminblue = y;break;}}//printf("%d\n",iminblue);isum = 0;for (int y=255;y>=0;y--){isum=isum + HistBlue.at<const float>(y);//isum=isum+HistBlue[y];if (isum>=PixelAmount*dhighcut*0.01){imaxblue=y;break;}}isum=0;int iminred=0;int imaxred=0;for (int y = 0;y<256;y++){isum= isum+HistRed.at<const float>(y);//isum= isum+HistRed[y];if (isum>=PixelAmount*dlowcut*0.01){iminred = y;break;}}//printf("%d\n",iminred );isum = 0;for (int y=255;y>=0;y--){isum=isum+HistRed.at<const float>(y);//isum=isum+HistRed[y];if (isum>=PixelAmount*dhighcut*0.01){imaxred=y;break;}}//printf("%d\n",imaxred);//greenisum=0;int imingreen=0;int imaxgreen=0;for (int y = 0;y<256;y++){isum= isum+HistGreen.at<const float>(y);//isum= isum+HistGreen[y];if (isum>=PixelAmount*dlowcut*0.01){imingreen = y;break;}}//printf("%d\n",imingreen);isum = 0;for (int y=255;y>=0;y--){isum=isum+HistGreen.at<const float>(y);//isum=isum+HistGreen[y];if (isum>=PixelAmount*dhighcut*0.01){imaxgreen=y;break;}}printf("\n cut time %f ms.\n",(double)(clock() - T_2));long T_3 = clock();
#pragma omp parallel for for (int y=0;y<256;y++){if (y<=iminblue){allmap[y*3+2]=0;}else{if (y>imaxblue){allmap[y*3+2]=255;}else{float ftmp = (float)(y-iminblue)/(imaxblue-iminblue);allmap[y*3+2]=(uchar)(ftmp*255);}}}//red
#pragma omp parallel for for (int y=0;y<256;y++){if (y<=iminred){allmap[y*3]=0;}else {if (y>imaxred){allmap[y*3]=255;}else{float ftmp = (float)(y-iminred)/(imaxred-iminred);allmap[y*3]=(uchar)(ftmp*255);}}}//green
#pragma omp parallel for for (int y=0;y<256;y++){if (y<=imingreen){allmap[y*3+1]=0;}else {if (y>imaxgreen){allmap[y*3+1]=255;}else{float ftmp = (float)(y-imingreen)/(imaxgreen-imingreen);allmap[y*3+1]=(uchar)(ftmp*255);}}}printf("\n map time %f ms.\n",(double)(clock() - T_3));long T_4 = clock();Mat lut(1,256,CV_8UC3,allmap);LUT(matface,lut,matface);printf("\n values time %f ms.\n",(double)(clock() - T_4));return matface;
}
int main()
{bool flag;VideoCapture cap("test.avi"); //打开摄像头// 如果要打开本地视频采用 VideoCapture cap("***.avi");// if(!cap.isOpened()) return -1; //检测一下摄像头是否打开Mat frame;Mat result;
// Mat result2;flag=true;while(flag){cap>>frame;cv::resize(frame, result, cv::Size(512, 512), (0, 0), (0, 0), cv::INTER_LINEAR);cv::resize(frame, frame, cv::Size(512, 512), (0, 0), (0, 0), cv::INTER_LINEAR);
// cv::resize(frame, result2, cv::Size(512, 512), (0, 0), (0, 0), cv::INTER_LINEAR);imshow("yuanshi",frame);result=autolevel(result,0.5,0.5) ;
// result2=autolevel(result,1,0.5) ;//namedWindow("xiaorun Opencv CAM",CV_WINDOW_AUTOSIZE) ; //读取当前帧// 此处可添加图像处理算法,对图像进行处理,当然了,我们可以不做任何操作,只打开一下摄像头imshow("CAM", result); //显示一下
// imshow("CAM2", result2); //显示一下if(waitKey(20) >=0) break; // 等待按键,跳出循环}
}/*int main()
{Mat I = imread("14.jpg");long T_1 = clock();Mat dehaze = autolevel(I,0.5,0.5);printf("\nTotal Time: %f ms.\n", (double)(clock() - T_1)); namedWindow("dehaze",WINDOW_NORMAL);imshow("dehaze",dehaze);imwrite("dehaze.jpg",dehaze);waitKey(0);destroyAllWindows();return 0;
}*/
这个代码是可以实现去雾的,也是何博士的暗通道去雾的论文实现。运用到的语言是C++语言。如果有地方借鉴,是可以借鉴的。注:本文中有include<opencv2/cv.h>这段代码,大家都知道,在opencv2文件夹中是没有cv.h的,所以需要将opencv文件夹的cv.h拷贝到opencv2文件夹下即可使用这些源码。
转载自:https://blog.csdn.net/xiao__run/article/details/77188089
这篇关于图像去雾完结(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!