資源簡介
基于暗通道和導向濾波的圖像去霧算法C++實現

代碼片段和文件信息
#include“ImageDefogging.h“
int?main()
{
//效果參數
double?omega?=?0.95;
double?numt?=?0.3;
int?rectSize?=?15;
clock_t?startTime?endTime;
startTime?=?clock();//計時開始
Mat?src?=?imread(“1.png“);
Mat?dst;//(src.rows?src.cols?CV_32FC3)
Mat?dst1(src.rows?src.cols?CV_32FC3);
ImageDefogging(src?dst?rectSize?omega?numt);
endTime?=?clock(); //計時結束
cout?<“The?run?time?is:?“?<(double)(endTime?-?startTime)?/?CLOCKS_PER_SEC?<“s“?<
imwrite(“dst.jpg“?dst);
imwrite(“dst1.jpg“?dst1);
/*float?scale?=?0.15;
resize(src?src?Size(src.cols*scale?src.rows*scale));
resize(dst?dst?Size(dst.cols*scale?dst.rows*scale));
resize(dst1?dst1?Size(dst1.cols*scale?dst1.rows*scale));*/
dst?/=?255;
dst1?/=?255;
imshow(“src“?src);
imshow(“dst“?dst);
imshow(“dst1“?dst1);
waitKey();
return?0;
}
void?ImageDefogging(Mat?src?Mat&?dst?int?rectSize?double?omega?double?numt)
{
//對原圖進行歸一化
Mat?I;
src.convertTo(I?CV_32F);
I?/=?255;
float?A[3]?=?{?0?};
Mat?dark?=?DarkChannel(I?rectSize);
AtmLight(I?dark?A);
Mat?te?=?TransmissionEstimate(I?A?rectSize?omega);
Mat?t?=?TransmissionRefine(src?te);
dst?=?Defogging(I?t?A?numt);
}
Mat?DarkChannel(Mat?srcImg?int?size)
{
vector?chanels;
split(srcImg?chanels);
//求RGB三通道中的最小像像素值
Mat?minChannel?=?(cv::min)((cv::min)(chanels[0]?chanels[1])?chanels[2]);
Mat?kernel?=?getStructuringElement(MORPH_RECT?Size(size?size));
Mat?dark(minChannel.rows?minChannel.cols?CV_32FC1);
erode(minChannel?dark?kernel); //圖像腐蝕
return?dark;
}
template?vector?argsort(const?vector&?array)
{
const?int?array_len(array.size());
vector?array_index(array_len?0);
for?(int?i?=?0;?i? array_index[i]?=?i;
sort(array_index.begin()?array_index.end()
[&array](int?pos1?int?pos2)?{return?(array[pos1]?
return?array_index;
}
void?AtmLight(Mat?src?Mat?dark?float?outA[3])
{
int?row?=?src.rows;
int?col?=?src.cols;
int?imgSize?=?row*col;
//將暗圖像和原圖轉為列向量
vector?darkVector?=?dark.reshape(1?imgSize);
Mat?srcVector?=?src.reshape(3?imgSize);
//按照亮度的大小取前0.1%的像素(亮度高)
int?numpx?=?int(max(floor(imgSize?/?1000)?1.0));
vector?indices?=?argsort(darkVector);
vector?dstIndices(indices.begin()?+?(imgSize?-?numpx)?indices.end());
for?(int?i?=?0;?i? {
outA[0]?+=?srcVector.at(dstIndices[i]?0)[0];
outA[1]?+=?srcVector.at(dstIndices[i]?0)[1];
outA[2]?+=?srcVector.at(dstIndices[i]?0)[2];
}
outA[0]?/=?numpx;
outA[1]?/=?numpx;
outA[2]?/=?numpx;
}
Mat?TransmissionEstimate(Mat?src?float?outA[3]?int?size?float?omega)
{
Mat?imgA?=?Mat::zeros(src.rows?src.cols?CV_32FC3);
vector?chanels;
split(src?chanels);
for?(int?i?=?0;?i?3;?++i)
{
chanels[i]?=?chanels[i]?/?outA[i];
}
merge(chanels?imgA);
Mat?transmission?=?1?-?omega*DarkCha
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????287946??2019-09-26?13:15??1.png
?????文件????????5168??2019-12-07?10:52??ImageDefogging?-?副本.cpp
?????文件????????1326??2019-11-22?16:08??ImageDefogging.h
?????文件????????4394??2019-12-07?10:52??ImageDefogging.vcxproj
?????文件????????1091??2019-12-07?10:52??ImageDefogging.vcxproj.filters
?????文件?????????165??2019-11-28?10:24??ImageDefogging.vcxproj.user
- 上一篇:操作系統銀行家算法源碼
- 下一篇:華容道基本功能c++實現
評論
共有 條評論