梯度金字塔方向滤波

#include"cv.h"

#include"highgui.h"

 

//用梯度金字塔变换实现彩色图像四个方向的滤波

int main(int argc,char *argv[])

{

       IplImage * src=0;

       IplImage * pImg[4]={0,0,0,0};

       IplImage * pImg2[4]={0,0,0,0};

       IplImage * dstj[4]={0,0,0,0};

       IplImage * dstk[4]={0,0,0,0};

       IplImage * dstl[4]={0,0,0,0};

 

       int i=0;

 

       src=cvLoadImage("lena.jpg",1);

       for(i=0;i<4;i++)

       {

              //pow函数有警告可用for循球消除

              pImg[i] = cvCreateImage(cvSize(src->width/pow(2,i), src->height/pow(2,i)),

                  IPL_DEPTH_8U, src->nChannels);

              pImg2[i] = cvCreateImage(cvSize(src->width/pow(2,i), src->height/pow(2,i)),

                     IPL_DEPTH_8U, src->nChannels);

              dstj[i] = cvCreateImage(cvSize(src->width/pow(2,i), src->height/pow(2,i)),

                     IPL_DEPTH_8U, 1);

              dstk[i] = cvCreateImage(cvSize(src->width/pow(2,i), src->height/pow(2,i)),

                     IPL_DEPTH_8U, 1);

              dstl[i] = cvCreateImage(cvSize(src->width/pow(2,i), src->height/pow(2,i)),

                     IPL_DEPTH_8U, 1);

       }

//下面是单独对空间的申请

/*    pImg[1] = cvCreateImage(cvSize(pImg[0]->width/2, pImg[0]->height/2),

              IPL_DEPTH_8U,

              pImg[0]->nChannels);

       pImg[2] = cvCreateImage(cvSize(pImg[0]->width/4, pImg[0]->height/4),

              IPL_DEPTH_8U,

              pImg[0]->nChannels);

       pImg[3] = cvCreateImage(cvSize(pImg[0]->width/8, pImg[0]->height/8),

              IPL_DEPTH_8U,

              pImg[0]->nChannels);*/

/*    pImg2[1] = cvCreateImage(cvSize(pImg[0]->width/2, pImg[0]->height/2),

              IPL_DEPTH_8U,

              pImg[0]->nChannels);

       pImg2[2] = cvCreateImage(cvSize(pImg[0]->width/4, pImg[0]->height/4),

              IPL_DEPTH_8U,

              pImg[0]->nChannels);

       pImg2[3] = cvCreateImage(cvSize(pImg[0]->width/8, pImg[0]->height/8),

              IPL_DEPTH_8U,

              pImg[0]->nChannels);*/

 

/*    dstj[0] = cvCreateImage(cvSize(pImg[0]->width, pImg[0]->height), IPL_DEPTH_8U, 1);

       dstk[0] = cvCreateImage(cvSize(pImg[0]->width, pImg[0]->height), IPL_DEPTH_8U, 1);

       dstl[0] = cvCreateImage(cvSize(pImg[0]->width, pImg[0]->height), IPL_DEPTH_8U, 1);

       dstj[1] = cvCreateImage(cvSize(pImg[0]->width/2, pImg[0]->height/2), IPL_DEPTH_8U, 1);

       dstk[1] = cvCreateImage(cvSize(pImg[0]->width/2, pImg[0]->height/2), IPL_DEPTH_8U, 1);

       dstl[1] = cvCreateImage(cvSize(pImg[0]->width/2, pImg[0]->height/2), IPL_DEPTH_8U, 1);

       dstj[2] = cvCreateImage(cvSize(pImg[0]->width/4, pImg[0]->height/4), IPL_DEPTH_8U, 1);

       dstk[2] = cvCreateImage(cvSize(pImg[0]->width/4, pImg[0]->height/4), IPL_DEPTH_8U, 1);

       dstl[2] = cvCreateImage(cvSize(pImg[0]->width/4, pImg[0]->height/4), IPL_DEPTH_8U, 1);

       dstj[3] = cvCreateImage(cvSize(pImg[0]->width/8, pImg[0]->height/8), IPL_DEPTH_8U, 1);

       dstk[3] = cvCreateImage(cvSize(pImg[0]->width/8, pImg[0]->height/8), IPL_DEPTH_8U, 1);

       dstl[3] = cvCreateImage(cvSize(pImg[0]->width/8, pImg[0]->height/8), IPL_DEPTH_8U, 1);*/

 

 

       pImg[0]=cvCloneImage(src);

       //pImgi进行下采样

       for( i=0;i<3;i++)

              cvPyrDown(pImg[i], pImg[i+1], CV_GAUSSIAN_5x5);

 

       for( i=0;i<3;i++)

              pImg2[i]=cvCloneImage(pImg[i]);

 

       double ka[9] = { 1.f/16, 2.f/16, 1.f/16,

                     2.f/16, 4.f/16, 2.f/16,

                     1.f/16, 2.f/16, 1.f/16  };  // 这里高斯核滤波器归一化

    CvMat k1;

    k1 = cvMat( 3, 3, CV_64F, ka );

       //pImgi用高斯核滤波器滤波

       //对每个通道分别进行滤波

       for(i=0;i<4;i++)

       {

              cvSplit(pImg[i],dstj[i],dstk[i],dstl[i],NULL);

              cvFilter2D( dstj[i], dstj[i], &k1, cvPoint(-1,-1));

              cvFilter2D( dstk[i], dstk[i], &k1, cvPoint(-1,-1));

              cvFilter2D( dstl[i], dstl[i], &k1, cvPoint(-1,-1)); 

              cvMerge(dstj[i],dstk[i],dstl[i],NULL,pImg[i]);

       }

       for(i=0;i<4;i++)//与原图像相加存入pImgi

              cvAdd(pImg[i],pImg2[i],pImg[i]);

 

       //方向滤波器的构造

       double l1[2] = {1,-1};

       double l2[4]={0,-1/sqrt(2),1/sqrt(2),0};

       double l3[2]={-1,1};

       double l4[4]={-1/sqrt(2),0,0,1/sqrt(2)};

/*    CvMat lm1,lm2,lm3,lm4;

    lm1 = cvMat( 1, 2, CV_64F, l1);

      lm2 = cvMat( 2, 2, CV_64F, l2);

      lm3 = cvMat( 2, 1, CV_64F, l3);

     lm4 = cvMat( 2, 2, CV_64F, l4);*/

       CvMat lm[4];

       lm[0]=cvMat( 1, 2, CV_64F, l1);

       lm[1]=cvMat( 2, 2, CV_64F, l2);

       lm[2]=cvMat( 2, 1, CV_64F, l3);

       lm[3]=cvMat( 2, 2, CV_64F, l4);

 

       //用方向滤波器进行滤波,注意在这里只能对一方向滤波

//     for(i=0;i<4;i++)

//            cvFilter2D( pImg[i], pImg[i], &lm1, cvPoint(-1,-1));

//     for(i=0;i<4;i++)

//         cvFilter2D( pImg[i], pImg[i], &lm2, cvPoint(-1,-1));

//     for(i=0;i<4;i++)

       //     cvFilter2D( pImg[i], pImg[i], &lm3, cvPoint(-1,-1));

//     for(i=0;i<4;i++)

//            cvFilter2D( pImg[i], pImg[i], &lm4, cvPoint(-1,-1));

      

       //下面是对四个方向分别时行滤波

       for(i=0;i<4;i++)

       {           

              cvSplit(pImg[i],dstj[i],dstk[i],dstl[i],NULL);

              cvFilter2D( dstj[i], dstj[i], &lm[i], cvPoint(-1,-1));

              cvFilter2D( dstk[i], dstk[i], &lm[i], cvPoint(-1,-1));

              cvFilter2D( dstl[i], dstl[i], &lm[i], cvPoint(-1,-1));    

              cvMerge(dstj[i],dstk[i],dstl[i],NULL,pImg[i]);

       }

      

       cvNamedWindow("image0", 1);

       cvNamedWindow("image1", 1);   

       cvNamedWindow("image2", 1);

       cvNamedWindow("image3",1);

 

       cvShowImage("image0", pImg[0]);

       cvShowImage("image1", pImg[1]);

       cvShowImage("image2", pImg[2]);     

       cvShowImage("image3", pImg[3]);

       cvWaitKey(0);

      

       cvDestroyAllWindows();

       for(i=0;i<4;i++)

       {

              cvReleaseImage(&pImg[i]);

              cvReleaseImage(&pImg2[i]);

              cvReleaseImage(&dstj[i]);

              cvReleaseImage(&dstk[i]);

              cvReleaseImage(&dstl[i]);

       }

      

       return 0;

}

 

你可能感兴趣的:(图像融合,image,null,include)