我有以下代码来计算Daubechie 4小波变换图像(LL,LH,HL,HH子带)和能量特征。在this论文中是一篇关于如何从复杂背景中检测文本的教程,但我不知道如何继续这样做。有人可以帮助我,也许可以解释我如何计算图像的小波能量直方图,或者如何实现纸上的内容?
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <math.h>
#define h0 (1+sqrt(3))/(4*sqrt(2))
#define h1 (3+sqrt(3))/(4*sqrt(2))
#define h2 (3-sqrt(3))/(4*sqrt(2))
#define h3 (1-sqrt(3))/(4*sqrt(2))
#define g0 h3
#define g1 -h2
#define g2 h1
#define g3 -h0
using namespace std;
using namespace cv;
vector<Mat> wavelet(Mat im)
{
float a,b,c,d,e,f;
Mat im1, im2, im3, im4, im5, im6;
Mat 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/4,im.cols,CV_32F);
im2=Mat::zeros(im.rows/4,im.cols,CV_32F);
im3=Mat::zeros(im.rows/4,im.cols/4,CV_32F);
im4=Mat::zeros(im.rows/4,im.cols/4,CV_32F);
im5=Mat::zeros(im.rows/4,im.cols/4,CV_32F);
im6=Mat::zeros(im.rows/4,im.cols/4,CV_32F);
for(int rcnt=0;rcnt+3<im.rows;rcnt+=4)
{
for(int ccnt=0;ccnt<im.cols;ccnt++)
{
a=im.at<float>(rcnt,ccnt);
b=im.at<float>(rcnt+1,ccnt);
c=im.at<float>(rcnt+2,ccnt);
d=im.at<float>(rcnt+3,ccnt);
e=(a*h0)+(b*h1)+(c*h2)+(d*h3);
f=(a*g0)+(b*g1)+(c*g2)+(d*g3);
int _rcnt=rcnt/4;
im1.at<float>(_rcnt,ccnt)=e;
im2.at<float>(_rcnt,ccnt)=f;
}
}
for(int rcnt=0;rcnt<im.rows/4;rcnt++)
{
for(int ccnt=0;ccnt+3<im.cols;ccnt+=4)
{
a=im1.at<float>(rcnt,ccnt);
b=im1.at<float>(rcnt,ccnt+1);
c=im1.at<float>(rcnt,ccnt+2);
d=im1.at<float>(rcnt,ccnt+3);
e=(a*h0)+(b*h1)+(c*h2)+(d*h3);
f=(a*g0)+(b*g1)+(c*g2)+(d*g3);
int _ccnt=ccnt/4;
im3.at<float>(rcnt,_ccnt)=e;
im4.at<float>(rcnt,_ccnt)=f;
}
}
for(int rcnt=0;rcnt<im.rows/4;rcnt++)
{
for(int ccnt=0;ccnt+3<im.cols;ccnt+=4)
{
a=im2.at<float>(rcnt,ccnt);
b=im2.at<float>(rcnt,ccnt+1);
c=im2.at<float>(rcnt,ccnt+2);
d=im2.at<float>(rcnt,ccnt+3);
e=(a*h0)+(b*h1)+(c*h2)+(d*h3);
f=(a*g0)+(b*g1)+(c*g2)+(d*g3);
int _ccnt=ccnt/4;
im5.at<float>(rcnt,_ccnt)=e;
im6.at<float>(rcnt,_ccnt)=f;
}
}
Mat LL = Mat(im3);
Mat LH = Mat(im4);
Mat HL = Mat(im5);
Mat HH = Mat(im6);
LL.convertTo(LL,CV_8U);
LH.convertTo(LH,CV_8U);
HL.convertTo(HL,CV_8U);
HH.convertTo(HH,CV_8U);
vector<Mat> elements;
elements.push_back(LL);
elements.push_back(LH);
elements.push_back(HL);
elements.push_back(HH);
return elements;
}
int main(int argc, char **argv)
{
Mat image = imread(argv[1],0);
vector<Mat> subbands = wavelet(image);
int rows = subbands[0].rows;
int cols = subbands[0].cols;
Mat energy(rows,cols,CV_8UC1);
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
int x=subbands[1].at<uchar>(i,j);
int y=subbands[2].at<uchar>(i,j);
int z=subbands[3].at<uchar>(i,j);
energy.at<uchar>(i,j)=sqrt(x*x+y*y+z*z);
}
}
imshow("energy",energy);
waitKey(0);
}