//选择处理的视频
void CDetectDlg::OnBnClickedBrowse()
{
// TODO: 在此添加控件通知处理程序代码
TCHAR curDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH,curDir);
CString videoPath;
CFileDialog videoDlg(TRUE,_T(".mp4"),_T("视频文件"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("视频文件(.mp4)|*.mp4||"),this);
if (videoDlg.DoModal()==IDOK)
{
videoPath = videoDlg.GetPathName();
m_etvFilePath = videoPath;
UpdateData(false);
}
SetCurrentDirectory(curDir);
}
//开始处理
void CDetectDlg::OnBnClickedBtStart()
{
// TODO: 在此添加控件通知处理程序代码
int length = m_etvFilePath.GetLength();
TCHAR filePath[MAX_PATH];
lstrcpy(filePath,m_etvFilePath.GetBuffer(length));
char file[MAX_PATH];
//获取字节长度
int iLength = WideCharToMultiByte(CP_ACP, 0, filePath, -1, NULL, 0, NULL, NULL);
//将tchar值赋给_char
WideCharToMultiByte(CP_ACP, 0, filePath, -1, file, iLength, NULL, NULL);
m_etvFilePath.ReleaseBuffer();
capture = cvCreateFileCapture(file);
CvMat *matD;
CvMat *matH;
IplImage *srcshift;
CvMat *Fourier;
IplImage *image_re,*image_im;
IplImage *dst;
IplImage* grayImage;
cvNamedWindow("LPF");
if (NULL != capture)
{
while (1)
{
frame = cvQueryFrame(capture);
if (frame != NULL)
{
grayImage = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
cvCvtColor(frame,grayImage,CV_BGR2GRAY);
int M = grayImage->height;
int N = grayImage->width;
matD = cvCreateMat(M,N,CV_32FC1);
CDM(M,N,matD);
matH = cvCreateMat(M,N,CV_32FC1);
float D0 = 40.0;
BLPF(matD,matH,D0,1);
srcshift = cvCloneImage(grayImage);
Fourier = cvCreateMat( M, N, CV_32FC2 );
cvShiftDFT(srcshift,srcshift);
fft2(srcshift,Fourier);
image_re = cvCreateImage(cvGetSize(grayImage),IPL_DEPTH_32F,1);
image_im = cvCreateImage(cvGetSize(grayImage),IPL_DEPTH_32F,1);
// filter
cvSplit(Fourier,image_re,image_im,0,0);
cvMul(image_re,matH,image_re);
cvMul(image_im,matH,image_im);
//显示频谱图
//cvPow( image_re, image_re, 2.0);
//cvPow( image_im, image_im, 2.0);
//cvAdd( image_re, image_im, image_re, NULL);
//cvPow( image_re, image_re, 0.5 );
// 对数增强
// Compute log(1 + Mag)
//cvAddS( image_re, cvScalarAll(1.0), image_re, NULL ); // 1 + Mag
//cvLog( image_re, image_re ); // log(1 + Mag)
dst = cvCreateImage(cvGetSize(grayImage),IPL_DEPTH_32F,2);
cvMerge(image_re,image_im,0,0,dst);
cvDFT(dst,dst,CV_DXT_INV_SCALE);
cvZero(image_re);
cvZero(image_im);
cvSplit(dst,image_re,image_im,0,0);
cvShiftDFT(image_re,image_re);
double max,min; // normalize
cvMinMaxLoc(image_re,&min,&max,NULL,NULL);
double scale,shift;
scale = 1.0/(max-min);
shift = -min*scale;
cvCvtScale(image_re,image_re,scale,shift);
cvShowImage("LPF",image_re);
cvWaitKey(30);
}
else
{
break;
}
}
}
cvDestroyWindow("LPF");
cvReleaseImage(&image_re);
cvReleaseImage(&image_im);
cvReleaseImage(&srcshift);
//cvReleaseImage(&dst);
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
}
//中心化
void CDetectDlg::cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
{
CvMat * tmp;
CvMat q1stub, q2stub;
CvMat q3stub, q4stub;
CvMat d1stub, d2stub;
CvMat d3stub, d4stub;
CvMat * q1, * q2, * q3, * q4;
CvMat * d1, * d2, * d3, * d4;
CvSize size = cvGetSize(src_arr);
CvSize dst_size = cvGetSize(dst_arr);
int cx, cy;
if(dst_size.width != size.width ||
dst_size.height != size.height){
cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ );
}
if(src_arr==dst_arr){
tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
}
cx = size.width/2;
cy = size.height/2; // image center
q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
d1 = cvGetSubRect( dst_arr, &d1stub, cvRect(0,0,cx,cy) );
d2 = cvGetSubRect( dst_arr, &d2stub, cvRect(cx,0,cx,cy) );
d3 = cvGetSubRect( dst_arr, &d3stub, cvRect(cx,cy,cx,cy) );
d4 = cvGetSubRect( dst_arr, &d4stub, cvRect(0,cy,cx,cy) );
if(src_arr!=dst_arr){
if( !CV_ARE_TYPES_EQ( q1, d1 )){
cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
}
cvCopy(q3, d1, 0);
cvCopy(q4, d2, 0);
cvCopy(q1, d3, 0);
cvCopy(q2, d4, 0);
}
else{
cvCopy(q3, tmp, 0);
cvCopy(q1, q3, 0);
cvCopy(tmp, q1, 0);
cvCopy(q4, tmp, 0);
cvCopy(q2, q4, 0);
cvCopy(tmp, q2, 0);
}
}
//傅里叶变换
void CDetectDlg::fft2(IplImage *src,CvMat *dst)
{
IplImage * realInput;
IplImage * imaginaryInput;
IplImage * complexInput;
int dft_M, dft_N;
CvMat* dft_A, tmp;
//IplImage * image_Re;
//IplImage * image_Im;
realInput = cvCreateImage( cvGetSize(src), IPL_DEPTH_32F, 1);
imaginaryInput = cvCreateImage( cvGetSize(src), IPL_DEPTH_32F, 1);
complexInput = cvCreateImage( cvGetSize(src), IPL_DEPTH_32F, 2);
cvScale(src, realInput, 1.0, 0.0);
cvZero(imaginaryInput);
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
//cvDFT(complexInput,dst,CV_DXT_FORWARD);
// dft_M = cvGetOptimalDFTSize( src->height - 1 );
// dft_N = cvGetOptimalDFTSize( src->width - 1 );
dft_M = src->height;
dft_N = src->width;
dft_A = cvCreateMat( dft_M, dft_N, CV_32FC2 );
cvDFT( complexInput, dft_A, CV_DXT_FORWARD, complexInput->height );
cvCopy(dft_A,dst);
cvReleaseImage(&realInput);
cvReleaseImage(&imaginaryInput);
cvReleaseImage(&complexInput);
//cvReleaseImage(&image_Re);
//cvReleaseImage(&image_Im);
}
//计算H(U,V)中的D(U,V)
void CDetectDlg::CDM(int M,int N,CvMat *mat)
{
int width = mat->rows;
int height = mat->cols;
if (M != width && N != height)
{
return;
}
if (cvGetElemType(mat) < CV_32F)
{
return;
}
CvMat *U,*V;
U = cvCreateMat(M,N,CV_32FC1);
V = cvCreateMat(M,N,CV_32FC1);
for (int u = 0; u < M; ++u)
{
for (int v =0 ;v < N; ++v)
{
float tm1,tm2;
tm1 = (float)((u > cvRound(M/2))?u-M:u);
tm2 = (float)((v > cvRound(N/2))?v-N:v);
*( (float *)CV_MAT_ELEM_PTR(*U,u,v) ) = tm1;
*( (float *)CV_MAT_ELEM_PTR(*V,u,v) ) = tm2;
}
}
for ( int u2 = 0; u2 < M; ++u2)
{
for (int v2 =0 ;v2 < N; ++v2)
{
float t1,t2;
t1 = CV_MAT_ELEM(*U,float,u2,v2);
t2 = CV_MAT_ELEM(*V,float,u2,v2);
*( (float *)CV_MAT_ELEM_PTR(*mat,u2,v2) ) = sqrt(t1*t1 + t2*t2);
}
}
}
//巴特沃斯低通滤波器
void CDetectDlg::BLPF(CvMat* matD,CvMat* matH,float D0, int n)
{
if (D0 < 0)
{
return ;
}
int w = matD->rows;
int h = matD->cols;
for (int u = 0; u < w; ++u)
{
for (int v =0 ;v < h; ++v)
{
float Elem_D,t;
Elem_D = CV_MAT_ELEM(*matD,float,u,v)/D0;
t = pow(Elem_D,2*n);
*( (float *)CV_MAT_ELEM_PTR(*matH,u,v) ) = 1/(1+t);
}
}
}