图像直接滤波 opencv下的问题 显示为黑色

xavier_x xavier_x 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#include "cv.h"
#include "highgui.h"

using namespace cv;
using namespace std;

Mat scr_img ;
Mat gray_img ,blur_img;

Mat InverseFliter();

Mat InverseFliter()
{

Mat padded;
int dft_h = getOptimalDFTSize(blur_img.rows);
int dft_w = getOptimalDFTSize(blur_img.cols);
copyMakeBorder(blur_img,padded,0,dft_h-blur_img.rows,0,dft_w-blur_img.cols,BORDER_CONSTANT,Scalar::all(0));
Mat planes[] = {Mat_<float>(padded) , Mat::zeros(padded.size(),CV_32F)};
Mat complexI;
merge(planes,2,complexI);
dft(complexI,complexI);
double a,b,c,d;
int height = complexI.rows;
int width =complexI.cols;
int ch = complexI.channels();
Mat H_Function = (Mat_<float>(dft_h, dft_w ));
Mat h_fuction;
for(int j=0;j <dft_h;j++)
{
uchar *H_ptr = H_Function.ptr<uchar>(j);
for(int i=0; i<dft_w ;i++)
{
//for(int k=0; k<ch;k++)
//{
if(i < 5 && j< 5)
{
H_ptr[i ] =0.04;
}
else
{
H_ptr[i] =0.0;
}
//}
}
}
Mat planes1[] = { H_Function , Mat::zeros(H_Function.size(),CV_32F)};
merge(planes1,2,h_fuction);

dft(h_fuction,h_fuction);
Mat dst_img;
dst_img.create(dft_h, dft_w, CV_64FC2);
for(int row=0; row <height;row++)
{
uchar *Scr_ptr =complexI.ptr<uchar>(row);
uchar *H_ptr1 = h_fuction.ptr<uchar>(row);
double *Dst_ptr = dst_img.ptr<double>(row);
for (int col = 0; col< width;col++)
{

a = Scr_ptr[col*ch];
b = Scr_ptr[col*ch +1];
c = H_ptr1[col*ch];
d = H_ptr1[col*ch+1];
if(c*c +d*d >1e-3)
{
Dst_ptr[col *ch] = (a*c +b*d)/(c*c + d*d);
Dst_ptr[col *ch +1] = (b*c-a*d)/(c*c+d*d);
}

}
}
idft(dst_img, dst_img); //或者 dft(dst_img,dst_img,CV_DXT_INVERSE_SCALE);

split(dst_img,planes1);
Mat idft_img2 = planes1[0];
return idft_img2;


int main()
{
scr_img = imread("lena.jpg",1);
if(scr_img.empty ())
{
cout<<"Miss the Image File"<<endl;
return -1;
}
cvtColor(scr_img,gray_img,CV_BGR2GRAY);
blur(gray_img,blur_img,Size(4,4));
Mat scr = InverseFliter();
//s_img.convertTo(blur_img,CV_8UC3,1,0);
imshow("window2",scr);
waitKey(0);
}


...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
xavier_x
图形处理/算法 19,463 成员 50,678 内容