Wavelet Transform

asked 2014-09-18 03:50:48 -0600

Mahavir gravatar image

updated 2014-09-24 07:37:35 -0600

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)
edit retag flag offensive close merge delete

Comments

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

berak gravatar imageberak ( 2014-09-22 02:20:44 -0600 )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 ( 2014-09-22 02:41:44 -0600 )edit

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

berak gravatar imageberak ( 2014-09-22 02:44:31 -0600 )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 ( 2014-11-11 11:07:33 -0600 )edit

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

luisCV gravatar imageluisCV ( 2014-11-11 11:21:18 -0600 )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 ( 2014-11-11 11:26:06 -0600 )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 ( 2016-01-27 20:58:01 -0600 )edit

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

Rahman gravatar imageRahman ( 2016-11-29 06:10:34 -0600 )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 ( 2016-12-16 02:16:50 -0600 )edit

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

berak gravatar imageberak ( 2016-12-16 02:19:26 -0600 )edit