Wavelet Transform

asked Sep 18 '14

Mahavir gravatar image

updated Sep 24 '14

Hello, I have done haar wavelet transform in opencv. the code is as below

            #include <opencv2\highgui\highgui.hpp>
            #include <opencv2\core\core.hpp>
            #include <opencv2\core\mat.hpp>
            #include <opencv2\imgproc\imgproc.hpp>
            #include<iostream>
            #include<math.h>
            #include<conio.h>
            using namespace std;
            using namespace cv;

            class image
            {
            public:
                Mat im,im1,im2,im3,im4,im5,im6,temp,im11,im12,im13,im14,imi,imd,imr;
                float a,b,c,d;
                int getim();
            };

            int image::getim()
            {
                im=imread("lena.jpg",0); //Load image in Gray Scale
                imi=Mat::zeros(im.rows,im.cols,CV_8U);
                im.copyTo(imi);

                im.convertTo(im,CV_32F,1.0,0.0);
                im1=Mat::zeros(im.rows/2,im.cols,CV_32F);
                im2=Mat::zeros(im.rows/2,im.cols,CV_32F);
                im3=Mat::zeros(im.rows/2,im.cols/2,CV_32F);
                im4=Mat::zeros(im.rows/2,im.cols/2,CV_32F);
                im5=Mat::zeros(im.rows/2,im.cols/2,CV_32F);
                im6=Mat::zeros(im.rows/2,im.cols/2,CV_32F);

                //--------------Decomposition-------------------

                for(int rcnt=0;rcnt<im.rows;rcnt+=2)
                {
                    for(int ccnt=0;ccnt<im.cols;ccnt++)
                    {

                        a=im.at<float>(rcnt,ccnt);
                        b=im.at<float>(rcnt+1,ccnt);
                        c=(a+b)*0.707;
                        d=(a-b)*0.707;
                        int _rcnt=rcnt/2;
                        im1.at<float>(_rcnt,ccnt)=c;
                        im2.at<float>(_rcnt,ccnt)=d;
                    }
                }

                for(int rcnt=0;rcnt<im.rows/2;rcnt++)
                {
                    for(int ccnt=0;ccnt<im.cols;ccnt+=2)
                    {

                        a=im1.at<float>(rcnt,ccnt);
                        b=im1.at<float>(rcnt,ccnt+1);
                        c=(a+b)*0.707;
                        d=(a-b)*0.707;
                        int _ccnt=ccnt/2;
                        im3.at<float>(rcnt,_ccnt)=c;
                        im4.at<float>(rcnt,_ccnt)=d;
                    }
                }

                for(int rcnt=0;rcnt<im.rows/2;rcnt++)
                {
                    for(int ccnt=0;ccnt<im.cols;ccnt+=2)
                    {

                        a=im2.at<float>(rcnt,ccnt);
                        b=im2.at<float>(rcnt,ccnt+1);
                        c=(a+b)*0.707;
                        d=(a-b)*0.707;
                        int _ccnt=ccnt/2;
                        im5.at<float>(rcnt,_ccnt)=c;
                        im6.at<float>(rcnt,_ccnt)=d;
                    }
                }

                imr=Mat::zeros(256,256,CV_32F);
                imd=Mat::zeros(256,256,CV_32F);
                im3.copyTo(imd(Rect(0,0,128,128)));
                im4.copyTo(imd(Rect(0,127,128,128)));
                im5.copyTo(imd(Rect(127,0,128,128)));
                im6.copyTo(imd(Rect(127,127,128,128)));


                //---------------------------------Reconstruction-------------------------------------

                im11=Mat::zeros(im.rows/2,im.cols,CV_32F);
                im12=Mat::zeros(im.rows/2,im.cols,CV_32F);
                im13=Mat::zeros(im.rows/2,im.cols,CV_32F);
                im14=Mat::zeros(im.rows/2,im.cols,CV_32F);

                for(int rcnt=0;rcnt<im.rows/2;rcnt++)
                {
                    for(int ccnt=0;ccnt<im.cols/2;ccnt++)
                    {
                        int _ccnt=ccnt*2;
                        im11.at<float>(rcnt,_ccnt)=im3.at<float>(rcnt,ccnt);     //Upsampling of stage I
                        im12.at<float>(rcnt,_ccnt)=im4.at<float>(rcnt,ccnt);
                        im13.at<float>(rcnt,_ccnt)=im5.at<float>(rcnt,ccnt);
                        im14.at<float>(rcnt,_ccnt)=im6.at<float>(rcnt,ccnt);
                    }
                }


                for(int rcnt=0;rcnt<im.rows/2;rcnt++)
                {
                    for(int ccnt=0;ccnt<im.cols;ccnt+=2)
                    {

                        a=im11.at ...
(more)
Preview: (hide)

Comments

hey, that's cool. is there any question ?

berak gravatar imageberak (Sep 22 '14)edit

I have no question about Wavelet. I gave this so that others can use it. I have question of SVD 1. cv::SVDecomp(im_enc1,w,u,vt);

where w,u,vt are SVD components and im_enc1 is input image.

 2. cv::SVBackSubst(w,u,vt,rhs,im_enc2);

where w,u,vt are SVD components and im_enc2 is output image. what is rhs array and how to get it?

Mahavir gravatar imageMahavir (Sep 22 '14)edit

ah, ok. but please then start a new question for that ;)

berak gravatar imageberak (Sep 22 '14)edit

hello, I try to run this code but I have some errors, please can you help me ? , I´m desperate, Thank you

luisCV gravatar imageluisCV (Nov 11 '14)edit

please a need some help with this code, I have some errors, can you help me, thanks you so much

luisCV gravatar imageluisCV (Nov 11 '14)edit

luisCV, site is under spam attack, all posts get moderated, a bit patience needed.

in the meantime, collect your errors, add them to your post, how else could we help ?

berak gravatar imageberak (Nov 11 '14)edit

After debugging the code, I found out the section of the code that causes the problem

change this

 imr=Mat::zeros(256,256,CV_32F);
            imd=Mat::zeros(256,256,CV_32F);
            im3.copyTo(imd(Rect(0,0,128,128)));
            im4.copyTo(imd(Rect(0,127,128,128)));
            im5.copyTo(imd(Rect(127,0,128,128)));
            im6.copyTo(imd(Rect(127,127,128,128)));

to this

  imr=Mat::zeros(512,512,CV_32F);
    imd=Mat::zeros(512,512,CV_32F);
    im3.copyTo(imd(Rect(0,0,256,256)));
    im4.copyTo(imd(Rect(0,255,256,256)));
    im5.copyTo(imd(Rect(255,0,256,256)));
    im6.copyTo(imd(Rect(255,255,256,256)));
Bryan Loo gravatar imageBryan Loo (Jan 28 '16)edit

Can you help me how to find out the wavelet coefficient and pixels values from the above program?

Rahman gravatar imageRahman (Nov 29 '16)edit

It gives error as below for odd number of rows/columns size images. How can we correct for odd number image sizes?

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1<< 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file d:\library\opencv\opencv-3.0.0\build\include\opencv2\core\mat.inl.hpp, line 894

serseri123 gravatar imageserseri123 (Dec 16 '16)edit

@serserri123, please do not post answers here, if you have a question or comment. thank you.

berak gravatar imageberak (Dec 16 '16)edit