dwt modified

This commit is contained in:
rafat.hsn@gmail.com 2011-08-20 11:52:06 +00:00
parent af5ff4aaa9
commit fb8b82719e

View File

@ -1,197 +1,197 @@
//============================================================================ //============================================================================
// Name : imagedemo1.cpp // Name : imagedemo1.cpp
// Author : Rafat Hussain // Author : Rafat Hussain
// Version : // Version :
// Copyright : // Copyright :
// Description : DWT of arbitrary size image using symmetric or periodic extension // Description : DWT of arbitrary size image using symmetric or periodic extension
//============================================================================ //============================================================================
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <complex> #include <complex>
#include <cmath> #include <cmath>
#include <algorithm> #include <algorithm>
#include "wavelet2d.h" #include "wavelet2d.h"
#include "cv.h" #include "cv.h"
#include "highgui.h" #include "highgui.h"
#include "cxcore.h" #include "cxcore.h"
using namespace std; using namespace std;
using namespace cv; using namespace cv;
void* maxval(vector<vector<double> > &arr, double &max){ void* maxval(vector<vector<double> > &arr, double &max){
max = 0; max = 0;
for (unsigned int i =0; i < arr.size(); i++) { for (unsigned int i =0; i < arr.size(); i++) {
for (unsigned int j =0; j < arr[0].size(); j++) { for (unsigned int j =0; j < arr[0].size(); j++) {
if (max <= arr[i][j]){ if (max <= arr[i][j]){
max = arr[i][j]; max = arr[i][j];
} }
} }
} }
return 0; return 0;
} }
void* maxval1(vector<double> &arr, double &max){ void* maxval1(vector<double> &arr, double &max){
max = 0; max = 0;
for (unsigned int i =0; i < arr.size(); i++) { for (unsigned int i =0; i < arr.size(); i++) {
if (max <= arr[i]){ if (max <= arr[i]){
max = arr[i]; max = arr[i];
} }
} }
return 0; return 0;
} }
int main() { int main() {
IplImage* img = cvLoadImage("snow.jpg"); IplImage* img = cvLoadImage("snow.jpg");
if (!img){ if (!img){
cout << " Can't read Image. Try Different Format." << endl; cout << " Can't read Image. Try Different Format." << endl;
exit(1); exit(1);
} }
int height, width; int height, width;
height = img->height; height = img->height;
width = img->width; width = img->width;
int nc = img->nChannels; int nc = img->nChannels;
// uchar* ptr2 =(uchar*) img->imageData; // uchar* ptr2 =(uchar*) img->imageData;
int pix_depth = img->depth; int pix_depth = img->depth;
CvSize size; CvSize size;
size.width =width; size.width =width;
size.height=height; size.height=height;
cout << "depth" << pix_depth << "Channels" << nc << endl; cout << "depth" << pix_depth << "Channels" << nc << endl;
cvNamedWindow("Original Image", CV_WINDOW_AUTOSIZE); cvNamedWindow("Original Image", CV_WINDOW_AUTOSIZE);
cvShowImage("Original Image", img); cvShowImage("Original Image", img);
cvWaitKey(); cvWaitKey();
cvDestroyWindow("Original Image"); cvDestroyWindow("Original Image");
cvSaveImage("orig.bmp",img); cvSaveImage("orig.bmp",img);
int rows =(int) height; int rows =(int) height;
int cols =(int) width; int cols =(int) width;
Mat matimg(img); Mat matimg(img);
vector<vector<double> > vec1(rows, vector<double>(cols)); vector<vector<double> > vec1(rows, vector<double>(cols));
int k =1; int k =1;
for (int i=0; i < rows; i++) { for (int i=0; i < rows; i++) {
for (int j =0; j < cols; j++){ for (int j =0; j < cols; j++){
unsigned char temp; unsigned char temp;
temp = ((uchar*) matimg.data + i * matimg.step)[j * matimg.elemSize() + k ]; temp = ((uchar*) matimg.data + i * matimg.step)[j * matimg.elemSize() + k ];
vec1[i][j] = (double) temp; vec1[i][j] = (double) temp;
} }
} }
string nm = "db3"; string nm = "db3";
vector<double> l1,h1,l2,h2; vector<double> l1,h1,l2,h2;
filtcoef(nm,l1,h1,l2,h2); filtcoef(nm,l1,h1,l2,h2);
// unsigned int lf=l1.size(); // unsigned int lf=l1.size();
// int rows_n =(int) (rows+ J*(lf-1)); // int rows_n =(int) (rows+ J*(lf-1));
// int cols_n =(int) (cols + J * ( lf -1)); // int cols_n =(int) (cols + J * ( lf -1));
// Finding 2D DWT Transform of the image using symetric extension algorithm // Finding 2D DWT Transform of the image using symetric extension algorithm
// Extension is set to 3 (eg., int e = 3) // Extension is set to 3 (eg., int e = 3)
vector<int> length; vector<int> length;
vector<double> output,flag; vector<double> output,flag;
int J =3; int J =3;
dwt_2d(vec1,J,nm,output,flag,length); dwt_2d_sym(vec1,J,nm,output,flag,length);
double max; double max;
vector<int> length2; vector<int> length2;
// This algorithm computes DWT of image of any given size. Together with convolution and // This algorithm computes DWT of image of any given size. Together with convolution and
// subsampling operations it is clear that subsampled images are of different length than // subsampling operations it is clear that subsampled images are of different length than
// dyadic length images. In order to compute the "effective" size of DWT we do additional // dyadic length images. In order to compute the "effective" size of DWT we do additional
// calculations. // calculations.
dwt_output_dim_sym(length,length2,J); dwt_output_dim_sym(length,length2,J);
// length2 is gives the integer vector that contains the size of subimages that will // length2 is gives the integer vector that contains the size of subimages that will
// combine to form the displayed output image. The last two entries of length2 gives the // combine to form the displayed output image. The last two entries of length2 gives the
// size of DWT ( rows_n by cols_n) // size of DWT ( rows_n by cols_n)
int siz = length2.size(); int siz = length2.size();
int rows_n=length2[siz-2]; int rows_n=length2[siz-2];
int cols_n = length2[siz-1]; int cols_n = length2[siz-1];
vector<vector< double> > dwtdisp(rows_n, vector<double>(cols_n)); vector<vector< double> > dwtdisp(rows_n, vector<double>(cols_n));
dispDWT(output,dwtdisp, length ,length2, J); dispDWT(output,dwtdisp, length ,length2, J);
// dispDWT returns the 2D object dwtdisp which will be displayed using OPENCV's image // dispDWT returns the 2D object dwtdisp which will be displayed using OPENCV's image
// handling functions // handling functions
vector<vector<double> > dwt_output= dwtdisp; vector<vector<double> > dwt_output= dwtdisp;
maxval(dwt_output,max);// max value is needed to take care of overflow which happens because maxval(dwt_output,max);// max value is needed to take care of overflow which happens because
// of convolution operations performed on unsigned 8 bit images // of convolution operations performed on unsigned 8 bit images
//Displaying Scaled Image //Displaying Scaled Image
// Creating Image in OPENCV // Creating Image in OPENCV
IplImage *cvImg; // image used for output IplImage *cvImg; // image used for output
CvSize imgSize; // size of output image CvSize imgSize; // size of output image
imgSize.width = cols_n; imgSize.width = cols_n;
imgSize.height = rows_n; imgSize.height = rows_n;
cvImg = cvCreateImage( imgSize, 8, 1 ); cvImg = cvCreateImage( imgSize, 8, 1 );
// dwt_hold is created to hold the dwt output as further operations need to be // dwt_hold is created to hold the dwt output as further operations need to be
// carried out on dwt_output in order to display scaled images. // carried out on dwt_output in order to display scaled images.
vector<vector<double> > dwt_hold(rows_n, vector<double>( cols_n)); vector<vector<double> > dwt_hold(rows_n, vector<double>( cols_n));
dwt_hold = dwt_output; dwt_hold = dwt_output;
// Setting coefficients of created image to the scaled DWT output values // Setting coefficients of created image to the scaled DWT output values
for (int i = 0; i < imgSize.height; i++ ) { for (int i = 0; i < imgSize.height; i++ ) {
for (int j = 0; j < imgSize.width; j++ ){ for (int j = 0; j < imgSize.width; j++ ){
if ( dwt_output[i][j] <= 0.0){ if ( dwt_output[i][j] <= 0.0){
dwt_output[i][j] = 0.0; dwt_output[i][j] = 0.0;
} }
if ( i <= (length2[0]) && j <= (length2[1]) ) { if ( i <= (length2[0]) && j <= (length2[1]) ) {
((uchar*)(cvImg->imageData + cvImg->widthStep*i))[j] = ((uchar*)(cvImg->imageData + cvImg->widthStep*i))[j] =
(char) ( (dwt_output[i][j] / max) * 255.0); (char) ( (dwt_output[i][j] / max) * 255.0);
} else { } else {
((uchar*)(cvImg->imageData + cvImg->widthStep*i))[j] = ((uchar*)(cvImg->imageData + cvImg->widthStep*i))[j] =
(char) (dwt_output[i][j]) ; (char) (dwt_output[i][j]) ;
} }
} }
} }
cvNamedWindow( "DWT Image", 1 ); // creation of a visualisation window cvNamedWindow( "DWT Image", 1 ); // creation of a visualisation window
cvShowImage( "DWT Image", cvImg ); // image visualisation cvShowImage( "DWT Image", cvImg ); // image visualisation
cvWaitKey(); cvWaitKey();
cvDestroyWindow("DWT Image"); cvDestroyWindow("DWT Image");
cvSaveImage("dwt.bmp",cvImg); cvSaveImage("dwt.bmp",cvImg);
// Finding IDWT // Finding IDWT
vector<vector<double> > idwt_output(rows, vector<double>(cols)); vector<vector<double> > idwt_output(rows, vector<double>(cols));
idwt_2d(output,flag, nm, idwt_output,length); idwt_2d_sym(output,flag, nm, idwt_output,length);
//Displaying Reconstructed Image //Displaying Reconstructed Image
IplImage *dvImg; IplImage *dvImg;
CvSize dvSize; // size of output image CvSize dvSize; // size of output image
dvSize.width = idwt_output[0].size(); dvSize.width = idwt_output[0].size();
dvSize.height = idwt_output.size(); dvSize.height = idwt_output.size();
cout << idwt_output.size() << idwt_output[0].size() << endl; cout << idwt_output.size() << idwt_output[0].size() << endl;
dvImg = cvCreateImage( dvSize, 8, 1 ); dvImg = cvCreateImage( dvSize, 8, 1 );
for (int i = 0; i < dvSize.height; i++ ) for (int i = 0; i < dvSize.height; i++ )
for (int j = 0; j < dvSize.width; j++ ) for (int j = 0; j < dvSize.width; j++ )
((uchar*)(dvImg->imageData + dvImg->widthStep*i))[j] = ((uchar*)(dvImg->imageData + dvImg->widthStep*i))[j] =
(char) (idwt_output[i][j]) ; (char) (idwt_output[i][j]) ;
cvNamedWindow( "Reconstructed Image", 1 ); // creation of a visualisation window cvNamedWindow( "Reconstructed Image", 1 ); // creation of a visualisation window
cvShowImage( "Reconstructed Image", dvImg ); // image visualisation cvShowImage( "Reconstructed Image", dvImg ); // image visualisation
cvWaitKey(); cvWaitKey();
cvDestroyWindow("Reconstructed Image"); cvDestroyWindow("Reconstructed Image");
cvSaveImage("recon.bmp",dvImg); cvSaveImage("recon.bmp",dvImg);
return 0; return 0;
} }