資源簡介
圖像運動模糊與去運動模糊的opencv實現(xiàn),linux版本實現(xiàn),含函數(shù)實現(xiàn),測試代碼,與測試demo,結(jié)果

代碼片段和文件信息
/*---------------------------?motion?blur?and?demotion?blur?--------------------------------*/
//?此代碼用于實現(xiàn)模糊運動的添加與消除。
//?原理:在已知模糊運動核的前提下,可通過核線性卷積的形式對圖像添加運動模糊,
//???????反之也可利用該核精確的去除該運動模糊。
//?說明:本例代碼是在梳理前人代碼的基礎(chǔ)上整理得到,僅使用了C++常用庫與opencv2.4.5
//???????AddMotionblur的createLinearFilter函數(shù)在opencv3+版本中已經(jīng)去除,故而建議只用opencv2+
//???????ker?核的大小不能過大,例如,以lena圖為例,ker的len為20時,會導(dǎo)致無法復(fù)原。?
//?Input:?
//??????彩色三通道圖像,在讀取時轉(zhuǎn)化為灰度圖
//?output:
//??????添加運動模糊的單通道灰度圖,或去除運動模糊后的單通道灰度圖
//?version:?1.0?
//?date:?2018/6/6
//?by?xie?qunyi
//?轉(zhuǎn)載請注明:
/*------------------------------------------------------------------------------------------*/
#include?
#include?
using?namespace?std;
using?namespace?cv;
//?Create?an?image?of?complex?number?type?(2?channels?to?store?
//?real?part?and?imaginary?part)?from?an?input?grayscale?image
//?src?:?single?channel?grayscale?image?input
//?dst?:?two?channel?complex?image?output
void?i2z(cv::Mat?src?cv::Mat&?dst)
{
//convert?the?image?to?float?type?create?another?one?filled?with?zeros?
//and?make?an?array?of?these?2?images
cv::Mat?im_array[]?=?{?cv::Mat_(src)?cv::Mat::zeros(src.size()?CV_32F)?};
//combine?as?a?2?channel?image?to?represent?a?complex?number?type?image
cv::Mat?im_complex;?cv::merge(im_array?2?im_complex);
????//copy?to?destination
im_complex.copyTo(dst);
}
//?convert?a?2?channel?complex?image?to?a?single?channel?grayscale?image
//?by?getting?magnitude
//?src?:?two?channel?complex?image?input
//?dst?:?single?channel?grayscale?image?output
void?z2i(cv::Mat?src?cv::Mat&?dst)
{
????//split?the?complex?image?to?2
cv::Mat?im_tmp[2];?cv::split(src?im_tmp);
????
????//get?absolute?value
cv::Mat?im_f;?cv::magnitude(im_tmp[0]?im_tmp[1]?im_f);
????
????//copy?to?destination
im_f.copyTo(dst);
}
//?return?complex?image?C?=?A./B
//?if?A?=?a+b*i?and?B?=?c+d*i;
//?then?C?=?A./B?=?((a*c+b*d)/(c^2+d^2))+((b*c-a*d)/(c^2+d^2))*i
cv::Mat?complexDiv(const?cv::Mat&?A?const?cv::Mat&?B)
{
????cv::Mat?A_tmp[2];?cv::split(A?A_tmp);
????cv::Mat?a?b;
????A_tmp[0].copyTo(a);
????A_tmp[1].copyTo(b);
????
????cv::Mat?B_tmp[2];?cv::split(B?B_tmp);
????cv::Mat?c?d;
????B_tmp[0].copyTo(c);
????B_tmp[1].copyTo(d);
????
????cv::Mat?C_tmp[2];
????cv::Mat?g?=?(c.mul(c)+d.mul(d));
????C_tmp[0]?=?(a.mul(c)+b.mul(d))/g;
????C_tmp[1]?=?(b.mul(c)-a.mul(d))/g;
????
????cv::Mat?C;
????cv::merge(C_tmp?2?C);
????return?C;
}
//?add?motion?blur?to?the?src?image
//?motion?degree?is?depended?on?the?kernel?ker
//?ker?can?be?product?by?matlab?func?:?fspecial
//?matlab?code?:?{LEN?=?3;?THETA?=?0;?ker?=?fspecial(‘motion‘?LEN?THETA);}
cv::Mat?AddMotionblur(const?cv::Mat&?src?const?cv::Mat&?ker)??
{???
????//?convert?to?float?data
????cv::Mat?sample_float;
????src.convertTo(sample_float?CV_32FC1);
????
????//?motion?blur
????cv::Point?anchor(0?0);
????double?delta?=?0;
????cv::Mat?dst?=?cv::Mat(sample_float.size()?sample_float.type());
????Ptr?fe?=?cv::createLinearFilter(sample_float.type()?ker.typ
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-06-06?10:54??demotion
?????文件???????61764??2018-06-06?10:54??demotion
?????文件?????????558??2018-06-06?10:54??demotion
?????文件???????72440??2018-06-06?10:54??demotion
?????文件???????45323??2018-06-06?10:54??demotion
?????文件????????4966??2018-06-06?10:54??demotion
評論
共有 條評論