📄 haar.cpp
字号:
#include "cv.h"
#include "highgui.h"
#include <math.h>
#include <stdio.h>
/*****************compute log2*******************/
int log2(int x)
{
int i = 0;
while(x != 1)
{
i++;
x = x / 2;
}
return i;
}
/*****************compute exp2*******************/
int exp2(int x)
{
int i = 1;
while(x != 0)
{
i = i * 2;
x--;
}
return i;
}
/*****************main function*****************/
void main( int argc, char** argv )
{
IplImage* src; //pointer of source image
IplImage* dst; //pointer of destination image
IplImage* sub; //pointer of subtraction image
CvSize dst_size;
CvSize sub_size;
//load image
if( argc == 2 && (src = cvLoadImage( argv[1], -1)) != 0 )
{
}
else
{
src = cvLoadImage("bike.bmp");
}
int row = src->height;
int arr = src->width;
CvScalar s;
CvScalar d;
double grayValue[300][300]; //array that saves gray values
double temp[300];
double threshold;
printf("Choose a threshold: ");
scanf("%lf", &threshold);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < arr; j++)
{
s =cvGet2D(src, i, j);
grayValue[i][j] = s.val[0];
}
}
int logRow = log2(row);
if(exp2(logRow) < row) //complement the rows
{
logRow += 1;
int newRow = exp2(logRow);
for(i = row; i < newRow; i++)
for(int j = 0; j < arr; j++)
grayValue[i][j] = 0;
row = newRow;
}
int logArr = log2(arr);
if(exp2(logArr) < arr) //complement the columns
{
logArr += 1;
int newArr = exp2(logArr);
for(i = 0; i < row; i++)
for(int j = arr; j < newArr; j++)
grayValue[i][j] = 0;
arr = newArr;
}
//haar wavelet transform
//transform on rows
for(i = 0; i < row; i++)
{
int currentArr = arr;
while(currentArr != 1)
{
for(int j = 0; j < currentArr/2; j++)
{
temp[j] = (grayValue[i][2*j] + grayValue[i][2*j+1])/2;
temp[j+currentArr/2] = grayValue[i][2*j] - temp[j];
}
for(j = 0; j < currentArr; j++)
{
grayValue[i][j] = temp[j];
}
currentArr /= 2;
}
}
//transform on rows
for(int j = 0; j < arr; j++)
{
int currentRow = row;
while(currentRow != 1)
{
for(int i = 0; i < currentRow/2; i++)
{
temp[i] = (grayValue[2*i][j] + grayValue[2*i+1][j])/2;
temp[i+currentRow/2] = grayValue[2*i][j] - temp[i];
}
for(i = 0; i < currentRow; i++)
{
if(fabs(temp[i]) < threshold) //threshold
grayValue[i][j] = 0;
else
grayValue[i][j] = temp[i];
}
currentRow /= 2;
}
}
//Decompression
//on columns
for(j = 0; j < arr; j++)
{
int currentRow = 1;
double sum = 0;
while(currentRow != row)
{
for(i = 0; i < currentRow; i++)
{
sum = grayValue[i][j] * 2;
temp[2*i] = grayValue[i][j] + grayValue[i+currentRow][j];
temp[2*i+1] = sum - temp[2*i];
}
for(i = 0; i < currentRow * 2; i++)
grayValue[i][j] = temp[i];
currentRow *= 2;
}
}
//on rows
for(i = 0; i < row; i++)
{
int currentArr = 1;
double sum = 0;
while(currentArr != arr)
{
for(j = 0; j < currentArr; j++)
{
sum = grayValue[i][j] * 2;
temp[2*j] = grayValue[i][j] + grayValue[i][j+currentArr];
temp[2*j+1] = sum - temp[2*j];
}
for(j = 0; j < currentArr * 2; j++)
grayValue[i][j] = temp[j];
currentArr *= 2;
}
}
dst_size.width = src->width;
dst_size.height = src->height;
dst = cvCreateImage(dst_size, src->depth, src->nChannels);
sub_size.width = src->width;
sub_size.height = src->height;
sub = cvCreateImage(sub_size, src->depth, src->nChannels);
for(i = 0; i < dst_size.height; i ++)
{
for(j = 0; j < dst_size.width; j++)
{
d.val[0] = grayValue[i][j];
d.val[1] = grayValue[i][j];
d.val[2] = grayValue[i][j];
cvSet2D(dst, i, j, d);
}
}
//show the src_image and the dst_image
cvNamedWindow("src", 1);
cvNamedWindow("dst", 1);
cvShowImage("src", src);
cvShowImage("dst", dst);
cvSaveImage("newbike.bmp", dst);
for(i = 0; i < src->height; i++)
{
for(j = 0; j < src->width; j++)
{
s = cvGet2D(src, i, j);
grayValue[i][j] = grayValue[i][j] - s.val[0];
}
}
//sub_image scales
double x, y; //scaling coefficients
double max = grayValue[0][0];
double min = grayValue[0][0];
for(i = 0; i < src->height; i++)
for(j = 0; j < src->width; j++)
{
if(grayValue[i][j] > max)
max = grayValue[i][j];
else if(grayValue[i][j] < min)
min = grayValue[i][j];
}
if(min < 0)
{
x = fabs(min);
y = x + max;
for(i = 0; i < src->height; i++)
for(j = 0; j < dst_size.width; j++)
{
grayValue[i][j] = (grayValue[i][j] + x) * 255 / y;
}
}
for(i = 0; i < src->height; i++)
{
for(j = 0; j < src->width; j++)
{
s.val[0] = grayValue[i][j];
s.val[1] = grayValue[i][j];
s.val[2] = grayValue[i][j];
cvSet2D(sub, i, j, s);
}
}
//show the sub_image
cvNamedWindow("sub", 1);
cvShowImage("sub", sub);
cvSaveImage("subbike.bmp", sub);
cvWaitKey(0); //wait for pressing any key
cvDestroyWindow("src");
cvDestroyWindow("dst");
cvDestroyWindow("sub");
cvReleaseImage(&src );
cvReleaseImage(&dst);
cvReleaseImage(&sub);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -