📄 lifting_5_3.cpp
字号:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "lifting53.h"
# define absolute(x) x > 0 ? x : -x
# define DAT_FILE 1
# define TXT_FILE 0
/****************************************************************
读取图像文件中的数据
输入参数:
int Height: 图像高(行数)
int Width: 图像宽(列数)
int ColorBit: 像素的位数,只能为8或24
FILE * fp: 文件句柄
输出参数:
unsigned char * Data:读入的图像数据,对于灰度图像大小为WxH字节;
彩色数据为WxHx3字节
****************************************************************/
int ImageDataRead( unsigned char * Data, int Height, int Width,
int ColorBit, char * filename )
{
int x,y;
unsigned char indata;
FILE * fp;
fp = fopen( filename, "rb" );
if ( fp == NULL ) return( -1 );
if ( ColorBit == 8 ) /* 对8位灰度图像的处理 */
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
{
fread( &indata, sizeof(unsigned char), 1, fp );
Data[ y*Width+x ] = indata;
}
}
else /* 对24位真彩色图像的处理 */
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
{
fread( &indata, sizeof(unsigned char), 1, fp );
Data[ (y*Width+x)*3 ] = indata; // read in B
fread( &indata, sizeof(unsigned char), 1, fp );
Data[ (y*Width+x)*3+1 ] = indata; // read in G
fread( &indata, sizeof(unsigned char), 1, fp );
Data[ (y*Width+x)*3+2 ] = indata; // read in R
}
}
fclose( fp );
return( 1 );
}
/****************************************************************
保存图像数据到文件中
输入参数:
int Height: 图像高(行数)
int Width: 图像宽(列数)
int ColorBit: 像素的位数,只能为8或24
FILE * fp: 文件句柄
unsigned char * Data:读入的图像数据,对于灰度图像大小为WxH字节;
彩色数据为WxHx3字节
输出参数:
无
****************************************************************/
int ImageDataWrite( unsigned char * Data, int Height, int Width,
int ColorBit, char * filename )
{
int x,y;
unsigned char outdata;
FILE * fp;
fp = fopen( filename, "wb" );
if ( fp == NULL ) return( -1 );
if ( ColorBit == 8 ) /* 对8位灰度图像的处理 */
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
{
outdata = Data[ y*Width+x ];
fwrite( ( unsigned char * ) &outdata, sizeof( unsigned char ), 1, fp );
}
}
else /* 对24位真彩色图像的处理 */
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
{
outdata = Data[ (y*Width+x)*3 ]; // output B
fwrite( ( unsigned char * ) &outdata, sizeof( unsigned char ), 1, fp );
outdata = Data[ (y*Width+x)*3+1 ]; // output G
fwrite( ( unsigned char * ) &outdata, sizeof( unsigned char ), 1, fp );
outdata = Data[ (y*Width+x)*3+2 ]; // output R
fwrite( ( unsigned char * ) &outdata, sizeof( unsigned char ), 1, fp );
}
}
fclose( fp );
return( 1 );
}
/*
write data into a text file
*/
int ImageDataWriteTxt( int * Data, int Height, int Width,
char * filename )
{
int x,y;
FILE * fp;
fp = fopen( filename, "wt" );
if ( fp == NULL ) return( -1 );
for ( y = 0; y < Height; y++ )
{
for ( x = 0; x < Width; x++ )
{
fprintf( fp, "%d ", Data[ y*Width+x ] );
}
fprintf( fp, "\n" );
}
fclose( fp );
return( 1 );
}
/*
determine file type, DAT_FILE or TXT_FILE
*/
int determine_datafile( char * filename )
{
int l, i;
char ch[3];
l = strlen( filename );
for ( i = 0; i < 3; i++ )
ch[3-i-1] = filename[l-i-1];
if ( ( ch[0] == 'T' || ch[0] == 't' )
&& ( ch[1] == 'X' || ch[1] == 'x' )
&& ( ch[2] == 'T' || ch[2] == 't' ) )
return( TXT_FILE );
else
return( DAT_FILE );
}
/* the main function to test each function modulars */
void main( int argc, char * argv[] )
{
int levels;
char file_gray[ 256 ], file_wavelet[ 256 ], file_rec[ 256 ];
char saveImage;
unsigned char * GrayData, * WaveletData;
int Height, Width, x, y, t, f_rv, df_flag;
int * image_d, * wavelet_d, * rec_d, max_value;
printf( "5/3 Wavelet Lifting program\n" );
printf( "Written by Y. B. Mao, with Dept. of Automation, NJUST\n" );
printf( "Oct. 22, 2005\n" );
printf( "Version 1.0\n" );
printf( "All rights reserved.\n\n" );
// check input parameters
if ( argc != 8 )
{
printf( "Usage of the program:\n\n" );
printf( "lifting_5_3 input_gray_scale_image output_wavelet reconstructed_image height width levels image_option\n\n" );
printf( "where,\n" );
printf( "input_gray_scale_image --- a 256-gray-scale image file\n" );
printf( "output_wavelet --- output file contains wavelet coefficients. There are two kinds of output file: " );
printf( "if file suffix is .txt, text-style data are output, otherwise, if suffix is .dat, binary data are output.\n" );
printf( "reconstructed_image --- a reconstructed 256-gray-scale image file. NULL for not output.\n" );
printf( "height --- height of image\n" );
printf( "width --- width of image\n" );
printf( "levels --- decomposition/reconstruction levels\n" );
printf( "image_option --- I or i means wavelet coefficients will be scaled in [0,255], D or d means the coefficients will be output as they were\n" );
printf( "\nOne example may look like this:\n" );
printf( "\nlifting_5_3 c:\\lena.dat wavelet.dat NULL 256 256 2 I\n\n" );
exit( -1 );
}
sscanf( argv[1], "%s", file_gray );
sscanf( argv[2], "%s", file_wavelet );
sscanf( argv[3], "%s", file_rec );
sscanf( argv[4], "%d", &Height );
sscanf( argv[5], "%d", &Width );
sscanf( argv[6], "%d", &levels );
sscanf( argv[7], "%c", &saveImage);
// check Height and Width of image
int h = Height>>levels;
int w = Width>>levels;
if ( h < 4 || w < 4 )
{
printf( "->> Too many decomposition/synthesis levels.\n" );
printf( "->> Program terminate.\n" );
exit( -4 );
}
df_flag = determine_datafile( file_wavelet );
// allocate memory and read in image data
GrayData = new unsigned char [ Height*Width ];
WaveletData = new unsigned char [ Height*Width ];
printf( "->> Read in data from image file, %s ...\n", file_gray );
f_rv = ImageDataRead( GrayData, Height, Width, 8, file_gray );
if ( f_rv < 0 )
{
printf( "->> Error in openning an image file.\n" );
exit( -2 );
}
// convert to int
image_d = new int [ Height*Width ];
wavelet_d = new int [ Height*Width ];
rec_d = new int [ Height*Width ];
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
image_d[ y*Width+x ] = (int)GrayData[ y*Width+x ];
// perform wavelet operation
printf( "->> Perform %d level wavelet decomposition ...\n", levels );
multilevel_decomposition_2D( image_d, Width, Height, wavelet_d, levels );
printf( "->> Perform %d level wavelet reconstruction ...\n", levels );
multilevel_reconstruction_2D( wavelet_d, Width, Height, rec_d, levels );
if ( df_flag == TXT_FILE )
{
f_rv = ImageDataWriteTxt( wavelet_d, Height, Width, file_wavelet );
printf( "->> Wavelet coefficients were output in a text file, %s.\n", file_wavelet );
}
else
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
wavelet_d[ y*Width+x ] = absolute( wavelet_d[ y*Width+x ] );
// formalize data into [0,255]
if ( saveImage == 'i' || saveImage == 'I' )
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
{
t = (int)(wavelet_d[ y*Width+x ]);
t = t > 255 ? 255 : t;
WaveletData[ y*Width+x ] = (unsigned char)t;
}
max_value = 1;
for ( y = 0; y < h; y++ )
for ( x = 0; x < w; x++ )
{
max_value = max_value > wavelet_d[ y*Width+x ] ? max_value : wavelet_d[ y*Width+x ];
}
for ( y = 0; y < h; y++ )
for ( x = 0; x < w; x++ )
{
t = (int)(wavelet_d[ y*Width+x ] * 1.0/max_value*255);
t = t > 255 ? 255 : t;
WaveletData[ y*Width+x ] = (unsigned char)t;
}
}
else
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
{
t = (int)(wavelet_d[ y*Width+x ]);
t = t > 255 ? 255 : t;
WaveletData[ y*Width+x ] = (unsigned char)t;
}
}
// save the result into file
f_rv = ImageDataWrite( WaveletData, Height, Width, 8, file_wavelet );
printf( "->> Wavelet coefficients were output in an image file, %s.\n", file_wavelet );
}
if ( strcmp( file_rec, "NULL" ) )
{
for ( y = 0; y < Height; y++ )
for ( x = 0; x < Width; x++ )
{
t = (int)(absolute(rec_d[ y*Width+x ]));
t = t > 255 ? 255 : t;
GrayData[ y*Width+x ] = (unsigned char)t;
}
printf( "->> Reconstructed image were output in a file, %s.\n", file_rec );
}
ImageDataWrite( GrayData, Height, Width, 8, file_rec );
delete image_d;
delete wavelet_d;
delete rec_d;
delete GrayData;
delete WaveletData;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -