数字图像处理中对图像进行几何操作,内容包括:图像平移,图像旋转,图像缩放,图像转置,图像镜像等。
图像平移:
/*************************************************************************
* 函数名称:Translation()
* 函数参数:
* LPSTR lpSrcStartBits,指向源DIB起始像素的指针
* long lWidth,DIB图象的宽度
* long lHeight,DIB图象的高度
* long xOff,X方向偏移量
* long yOff,Y方向偏移量
* long lLineBytes,DIB图象的行字节数,为4的倍数
* 函数类型: BOOL
* 函数功能: 该函数用来平移DIB图象
************************************************************************/
BOOL Translate(LPSTR lpSrcStartBits , long lWidth , long lHeight ,
long xOff , long yOff , long lLineBytes , int bitCount)
{
HLOCAL hDstDibBits;
LPSTR lpDstStartBits;
LPSTR lpDstDibBits;
LPSTR lpSrcDibBits;
hDstDibBits = LocalAlloc(LHND , lWidth * lLineBytes );
lpDstStartBits = (char* ) LocalLock ( hDstDibBits );
//临时指针,指向变换后的数据
if ( hDstDibBits == NULL )
{
return false;
}
if( bitCount == 8 ) //256色图像
{
for(int i=0 ; i<lHeight; i++)
{
for(int j=0 ; j<lWidth ; j++)
{
//指向临时dib中的第i行,第j列像素的指针
lpDstDibBits = (char *)lpDstStartBits +
lLineBytes * (lHeight-1-i) + j ;
if( (j-xOff>0) && (j-xOff<lWidth)
&& (i-yOff>0) && (i-yOff<lHeight) )
{
// 指向源DIB第i0行,第j0个像素的指针
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * (lHeight-1-(i-yOff)) + (j-xOff);
*lpDstDibBits = * lpSrcDibBits; // 复制像素
}
else
{
* ((unsigned char*) lpDstDibBits)= 255; //超出范围赋值255
}
}
}
}
else if ( bitCount == 24 ) //24位真彩图像
{
for(int i=0 ; i<lHeight; i++)
{
for(int j=0 ; j<lWidth ; j++)
{
//指向临时dib中的第i行,第j列像素的指针
lpDstDibBits = (char *)lpDstStartBits +
lLineBytes * (lHeight-1-i) + j*3 ;
if( (j-xOff>0) && (j-xOff<lWidth)
&& (i-yOff>0) && (i-yOff<lHeight) )
{
// 指向源DIB第i0行,第j0个像素的指针
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * (lHeight-1-(i-yOff)) + (j-xOff)*3;
*lpDstDibBits = * lpSrcDibBits; // 复制像素
lpDstDibBits++;
lpSrcDibBits++;
*lpDstDibBits = * lpSrcDibBits;
lpDstDibBits++;
lpSrcDibBits++;
*lpDstDibBits = * lpSrcDibBits;
}
else
{
* ((unsigned char*) lpDstDibBits)= 255; //超出范围赋值255
lpDstDibBits++;
* ((unsigned char*) lpDstDibBits)= 255;
lpDstDibBits++;
* ((unsigned char*) lpDstDibBits)= 255;
}
}
}
}
else
{
return false;
}
memcpy( lpSrcStartBits , lpDstStartBits , lLineBytes * lHeight);
LocalUnlock( hDstDibBits );
LocalFree( hDstDibBits );
return true;
}
图像旋转:
/*************************************************************************
* 函数名称:Rotate()
* 函数参数:
* LPSTR lpSrcStartBits,指向源DIB的起始像素的指针
* long lWidth,源DIB图象宽度
* long lHeight,源DIB图象高度
* double angle,旋转的角度
* int xCenter,旋转后的新中心水平位置
* int yCenter,旋转后的新中心垂直位置
* long lLineBytes,源DIB图象字节宽度(4的倍数)
* int bitCount,图像位数
* 函数类型:BOOL
* 函数功能:用来旋转DIB图象
************************************************************************/
BOOL Rotate(LPSTR lpSrcStartBits , long lWidth , long lHeight , double angle ,
int xCenter , int yCenter , long lLineBytes , int bitCount)
{
int xOriCenter = (int) lWidth/2;
int yOriCenter = (int) lHeight/2;
int i,j,tempI,tempJ;
HLOCAL hDstDibBits;
LPSTR lpDstStartBits;
LPSTR lpDstDibBits;
LPSTR lpSrcDibBits;
hDstDibBits = LocalAlloc(LHND , lWidth * lLineBytes );
lpDstStartBits = (char* ) LocalLock ( hDstDibBits );
//临时指针,指向变换后的数据
if ( hDstDibBits == NULL )
{
return false;
}
if( bitCount == 8 ) //256色图像
{
for( i=0 ; i<lHeight; i++)
{
for( j=0 ; j<lWidth ; j++)
{
//指向临时dib中的第i行,第j列像素的指针
lpDstDibBits = (char *)lpDstStartBits +
lLineBytes * (lHeight-1-i) + j ;
tempJ = (int) (j*cos(angle) + i*sin(angle)
- xCenter*cos(angle) - yCenter*sin(angle) + xOriCenter + 0.5);
tempI = (int) (-j*sin(angle) + i*cos(angle)
+ xCenter*sin(angle) - yCenter*cos(angle) + yOriCenter + 0.5);
if( (tempI>0) && (tempI<lHeight)
&& (tempJ>0) && (tempJ<lWidth) )
{
// 指向源DIB第i0行,第j0个像素的指针
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * (lHeight-1-tempI) + tempJ;
*lpDstDibBits = * lpSrcDibBits; // 复制像素
}
else
{
* ((unsigned char*) lpDstDibBits)= 255; //超出范围赋值255
}
}
}
}
else if ( bitCount == 24 ) //24位真彩图像
{
for(int i=0 ; i<lHeight; i++)
{
for(int j=0 ; j<lWidth ; j++)
{
//指向临时dib中的第i行,第j列像素的指针
lpDstDibBits = (char *)lpDstStartBits +
lLineBytes * (lHeight-1-i) + j*3 ;
tempJ = (int) (j*cos(angle) + i*sin(angle)
- xCenter*cos(angle) - yCenter*sin(angle) + xOriCenter + 0.5);
tempI = (int) (-j*sin(angle) + i*cos(angle)
+ xCenter*sin(angle) - yCenter*cos(angle) + yOriCenter + 0.5);
if( (tempI>0) && (tempI<lHeight)
&& (tempJ>0) && (tempJ<lWidth) )
{
// 指向源DIB第i0行,第j0个像素的指针
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * (lHeight-1-tempI) + tempJ*3;
*lpDstDibBits = * lpSrcDibBits; // 复制像素
lpDstDibBits++;
lpSrcDibBits++;
*lpDstDibBits = * lpSrcDibBits;
lpDstDibBits++;
lpSrcDibBits++;
*lpDstDibBits = * lpSrcDibBits;
}
else
{
* ((unsigned char*) lpDstDibBits)= 255; //超出范围赋值255
lpDstDibBits++;
* ((unsigned char*) lpDstDibBits)= 255;
lpDstDibBits++;
* ((unsigned char*) lpDstDibBits)= 255;
}
}
}
}
else
{
return false;
}
memcpy( lpSrcStartBits , lpDstStartBits , lLineBytes * lHeight);
LocalUnlock( hDstDibBits );
LocalFree( hDstDibBits );
return true;
}
图像缩放:
/*************************************************************************
* 函数名称:Zoom()
* 函数参数:
* LPSTR lpSrcDib,指向源DIB的指针
* LPSTR lpSrcStartBits,指向源DIB的起始像素的指针
* long lWidth,源DIB图象宽度
* long lHeight,源DIB图象高度
* long lLineBytes,源DIB图象字节宽度(4的倍数)
* double xRatio,水平缩放比例
* double yRatio,垂直缩放比例
* int bitCount,图像位数
* WORD palSize,源DIB图象调色板大小
* 函数类型:HGLOBAL
* 函数功能:用来缩放DIB图象
************************************************************************/
HGLOBAL Zoom(LPSTR lpSrcDib, LPSTR lpSrcStartBits,long lWidth, long lHeight,
long lLineBytes, double xRatio, double yRatio, DWORD palSize, int bitCount)
{
LPSTR lpDstDib; //指向临时图象的指针
LPSTR lpSrcDIBBits; //指向源像素的指针
LPSTR lpDstDIBBits; //指向临时图象对应像素的指针
LPSTR lpDstStartBits; //指向临时图象对应像素的指针
LPBITMAPINFOHEADER lpbmi;// 指向BITMAPINFO结构的指针
long lDstWidth = (long) (lWidth * xRatio + 0.5);
long lDstHeight = (long) (lHeight * yRatio + 0.5);
int lDstLineBytes = (int) (lDstWidth * bitCount + 31) / 32 * 4;
int i , j , i1 , j1;
// 分配内存,以保存缩放后的DIB
HGLOBAL hDIB = (HGLOBAL) ::GlobalAlloc(GHND, lDstLineBytes* lDstHeight
+ *(LPDWORD)lpSrcDib +palSize);
//注: lpSrcDib指向的第一个数据为DWORD biSize;
if (hDIB == NULL)
{
return FALSE;
}
lpDstDib= (char * )::GlobalLock((HGLOBAL) hDIB);
// 复制DIB信息头和调色板
memcpy(lpDstDib, lpSrcDib, *(LPDWORD)lpSrcDib +palSize);
// 更新DIB中图象信息头
lpbmi = (LPBITMAPINFOHEADER)lpDstDib;
lpbmi->biWidth = lDstWidth;
lpbmi->biHeight = lDstHeight;
//更新DIB中像素数据
lpDstStartBits=lpDstDib+ *(LPDWORD)lpDstDib
+palSize;
if(bitCount == 8)
{
for(i = 0; i < lDstHeight; i++)// 行操作
{
for(j = 0; j < lDstWidth; j++)// 列操作
{
// 指向新DIB第i行,第j个像素的指针
lpDstDIBBits= (char *)lpDstStartBits
+ lDstLineBytes * (lDstHeight-1-i)+j;
i1= (long) (i / yRatio + 0.5);// 计算该像素在源DIB中的坐标
j1= (long) (j / xRatio + 0.5);
if( (j1>= 0) && (j1< lWidth) && (i1>= 0) && (i1< lHeight))
{// 判断是否在源图内
lpSrcDIBBits= (char *)lpSrcStartBits
+ lLineBytes * (lHeight - 1 -i1) + j1;// 指向源DIB第i行,第j个像素的指针
*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
}
else
{
* ((unsigned char*)lpDstDIBBits) = 255;// 源图中不存在的像素,赋为255
}
}
}
return hDIB;
}
else if (bitCount == 24)
{
for(i = 0; i < lDstHeight; i++)// 行操作
{
for(j = 0; j < lDstWidth; j++)// 列操作
{
// 指向新DIB第i行,第j个像素的指针
lpDstDIBBits= (char *)lpDstStartBits
+ lDstLineBytes * (lDstHeight-1-i)+j*3;
i1= (long) (i / yRatio + 0.5);// 计算该像素在源DIB中的坐标
j1= (long) (j / xRatio + 0.5);
if( (j1>= 0) && (j1< lWidth) && (i1>= 0) && (i1< lHeight))
{// 判断是否在源图内
lpSrcDIBBits= (char *)lpSrcStartBits
+ lLineBytes * (lHeight - 1 -i1) + j1*3;// 指向源DIB第i行,第j个像素的指针
*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
lpDstDIBBits++;
lpSrcDIBBits++;
*lpDstDIBBits= *lpSrcDIBBits;
lpDstDIBBits++;
lpSrcDIBBits++;
*lpDstDIBBits= *lpSrcDIBBits;
}
else
{
* ((unsigned char*)lpDstDIBBits) = 255;// 源图中不存在的像素,赋为255
lpDstDIBBits++;
* ((unsigned char*)lpDstDIBBits) = 255;
lpDstDIBBits++;
* ((unsigned char*)lpDstDIBBits) = 255;
}
}
}
return hDIB;
}
else
{
return NULL;
}
}
图像转置:
/*************************************************************************
* 函数名称:Transpose()
* 函数参数:
* LPSTR lpSrcDib,指向源DIB的指针
* LPSTR lpSrcStartBits,指向源DIB的起始像素的指针
* long lWidth,源DIB图象宽度
* long lHeight,源DIB图象高度
* WORD palSize,源DIB图象调色板大小
* int bitCount,图像位数
* 函数类型: BOOL
* 函数功能: 用来转置DIB图象
************************************************************************/
BOOL Transpose(LPSTR lpSrcDib, LPSTR lpSrcStartBits,long lWidth, long lHeight,
DWORD palSize, int bitCount)
{
LPSTR lpDstDib; //指向临时图象的指针
LPSTR lpSrcDIBBits; //指向源像素的指针
LPSTR lpDstDIBBits; //指向临时图象对应像素的指针
LPSTR lpDstStartBits; //指向临时图象对应像素的指针
LPBITMAPINFOHEADER lpbmi;// 指向BITMAPINFO结构的指针
long lDstWidth = lHeight;
long lDstHeight = lWidth;
int lSrcLineBytes = (int) (lWidth * bitCount + 31) / 32 * 4;
int lDstLineBytes = (int) (lDstWidth * bitCount + 31) / 32 * 4;
// 分配内存,以保存缩放后的DIB
HGLOBAL hDIB = (HGLOBAL) ::GlobalAlloc(GHND, lDstLineBytes* lDstHeight
+ *(LPDWORD)lpSrcDib +palSize);
//注: lpSrcDib指向的第一个数据为DWORD biSize;
if (hDIB == NULL)
{
return FALSE;
}
lpDstDib= (char * )::GlobalLock((HGLOBAL) hDIB);
// 更新DIB中图象信息头
lpbmi = (LPBITMAPINFOHEADER)lpSrcDib;
lpbmi->biWidth = lDstWidth;
lpbmi->biHeight = lDstHeight;
//更新DIB中像素数据
lpDstStartBits=lpDstDib+ *(LPDWORD)lpDstDib
+palSize;
if( bitCount == 8 )
{
for(int i = 0; i < lDstHeight; i++)// 行操作
{
for(int j = 0; j < lDstWidth; j++)// 列操作
{
// 指向新DIB第i行,第j个像素的指针
lpDstDIBBits= (char *)lpDstStartBits
+ lDstLineBytes * (lDstHeight-1-i)+j;
// 指向源DIB第j行,第i个像素的指针
lpSrcDIBBits= (char *)lpSrcStartBits
+ lSrcLineBytes * (lHeight-1-j) + i;
*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
}
}
memcpy(lpSrcStartBits , lpDstStartBits , lDstLineBytes* lDstHeight);
LocalUnlock( hDIB );
LocalFree( hDIB );
return true;
}
else if ( bitCount == 24 )
{
for(int i = 0; i < lDstHeight; i++)// 行操作
{
for(int j = 0; j < lDstWidth; j++)// 列操作
{
// 指向新DIB第i行,第j个像素的指针
lpDstDIBBits= (char *)lpDstStartBits
+ lDstLineBytes * (lDstHeight-1-i)+j*3;
// 指向源DIB第j行,第i个像素的指针
lpSrcDIBBits= (char *)lpSrcStartBits
+ lSrcLineBytes * (lHeight-1-j) + i*3;
*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
lpDstDIBBits++;
lpSrcDIBBits++;
*lpDstDIBBits= *lpSrcDIBBits;
lpDstDIBBits++;
lpSrcDIBBits++;
*lpDstDIBBits= *lpSrcDIBBits;
}
}
memcpy(lpSrcStartBits , lpDstStartBits , lDstLineBytes* lDstHeight);
LocalUnlock( hDIB );
LocalFree( hDIB );
return true;
}
else
{
return false;
}
}
图像镜像:
/*************************************************************************
* 函数名称:Mirror()
* 函数参数:
* LPSTR lpSrcStartBits,指向源DIB的起始像素的指针
* long lWidth,源DIB图象宽度
* long lHeight,源DIB图象高度
* int bitCount,图像位数
* int mirrorType, 镜像的类型
* 函数类型: BOOL
* 函数功能: 用来转置DIB图象
************************************************************************/
BOOL Mirror(LPSTR lpSrcStartBits,long lWidth,
long lHeight,int bitCount,int mirrorType)
{
HLOCAL hDstDibBits;
LPSTR lpDstStartBits;
LPSTR lpDstDibBits;
LPSTR lpSrcDibBits;
long lLineBytes = (lWidth*bitCount + 31) / 32 * 4;
hDstDibBits = LocalAlloc(LHND , lWidth * lLineBytes );
lpDstStartBits = (char* ) LocalLock ( hDstDibBits );
//临时指针,指向变换后的数据
if ( hDstDibBits == NULL )
{
return false;
}
if( bitCount == 8 ) //256色图像
{
for(int i=0 ; i<lHeight; i++)
{
for(int j=0 ; j<lWidth ; j++)
{
//指向临时dib中的第i行,第j列像素的指针
lpDstDibBits = (char *)lpDstStartBits +
lLineBytes * (lHeight-1-i) + j ;
if(mirrorType == MIRRORTYPE_VER)
{
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * i + j;
}
else if(mirrorType == MIRRORTYPE_HORI)
{
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * (lHeight-1-i) + lWidth-1-j;
}
else
{
return false;
}
*lpDstDibBits = * lpSrcDibBits; // 复制像素
}
}
}
else if ( bitCount == 24 ) //24位真彩图像
{
for(int i=0 ; i<lHeight; i++)
{
for(int j=0 ; j<lWidth ; j++)
{
//指向临时dib中的第i行,第j列像素的指针
lpDstDibBits = (char *)lpDstStartBits +
lLineBytes * (lHeight-1-i) + j*3 ;
if(mirrorType == MIRRORTYPE_VER)
{
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * i + j*3;
}
else if(mirrorType == MIRRORTYPE_HORI)
{
lpSrcDibBits = (char *)lpSrcStartBits +
lLineBytes * (lHeight-1-i) + (lWidth-1-j)*3;
}
else
{
return false;
}
*lpDstDibBits = * lpSrcDibBits; // 复制像素
lpDstDibBits++;
lpSrcDibBits++;
*lpDstDibBits = * lpSrcDibBits;
lpDstDibBits++;
lpSrcDibBits++;
*lpDstDibBits = * lpSrcDibBits;
}
}
}
else
{
return false;
}
memcpy( lpSrcStartBits , lpDstStartBits , lLineBytes * lHeight);
LocalUnlock( hDstDibBits );
LocalFree( hDstDibBits );
return true;
}