|
Re: 有关移植问题请教
// ************************************************************************
// 文件名:morph.cpp
//
// 图像形态学变换API函数库:
// ThiningDIB() - 图像细化
//
// ************************************************************************
#include
#include
#include
#include
#define FALSE 0
#define TRUE 1
typedef struct tagBITMAPINFOHEADER{ // bmih
unsigned int biSize; // 本结构所占用字节数
int biwidth; // 位图的宽度,以像素为单位
int biheight; // 位图的高度,以像素为单位
unsigned short biPlanes; // 目标设备的级别,必须为1
unsigned short biBitCount ;// 每个像素所需的位数,必须是1(双色),4(16色),8(256色)或24(真彩色)之一
unsigned int biCompression; // 位图压缩类型,必须是 0(不压缩),1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
unsigned int biSizeImage; // 位图的大小,以字节为单位
int biXPelsPerMeter; // 位图水平分辨率,每米像素数
int biYPelsPerMeter; // 位图垂直分辨率,每米像素数
unsigned int biClrUsed; // 位图实际使用的颜色表中的颜色数
unsigned int biClrImportant; // 位图显示过程中重要的颜色数
} BITMAPINFOHEADER;
unsigned char* inp_buffer=(unsigned char*) 0x80000000;
FILE *input_file;
int readSize;
int ThiningDIB(unsigned char* lpDIBBits, long lWidth, long lHeight)
{
// 指向源图像的指针
unsigned char* lpSrc;
// 指向缓存图像的指针
unsigned char* lpDst;
// 指向缓存DIB图像的指针
unsigned char* hNewDIBBits;
//unsigned char* hNewDIBBits;
//脏标记
int bModified;
//循环变量
int i;
int j;
int n;
int m;
//四个条件
int bCondition1;
int bCondition2;
int bCondition3;
int bCondition4;
//计数器
unsigned char nCount;
//像素值
unsigned char pixel;
//5×5相邻区域像素值
unsigned char neighbour[5][5];
// 暂时分配内存,以保存新图像
hNewDIBBits = (unsigned char *)malloc(lWidth * lHeight);
if (hNewDIBBits == NULL)
{
//分配内存失败
return FALSE;
}
// 锁定内存
//hNewDIBBits = (char * )LocalLock(hNewDIBBits);
// 初始化新分配的内存,设定初始值为255
lpDst = /*(unsigned char *)*/hNewDIBBits;
memset(lpDst, (unsigned char)255, lWidth * lHeight);
//memset(hNewDIBBits, (unsigned char)255, lWidth * lHeight);
bModified=TRUE;
while(bModified)
{
bModified = FALSE;
// 初始化新分配的内存,设定初始值为255
lpDst = (unsigned char *)hNewDIBBits;
memset(lpDst, (unsigned char)255, lWidth * lHeight);
for(j = 2; j = 2 && nCount <=6)
bCondition1 = TRUE;
//判断Z0(P1)=1
nCount = 0;
if (neighbour[1][2] == 0 && neighbour[1][1] == 1)
nCount++;
if (neighbour[1][1] == 0 && neighbour[2][1] == 1)
nCount++;
if (neighbour[2][1] == 0 && neighbour[3][1] == 1)
nCount++;
if (neighbour[3][1] == 0 && neighbour[3][2] == 1)
nCount++;
if (neighbour[3][2] == 0 && neighbour[3][3] == 1)
nCount++;
if (neighbour[3][3] == 0 && neighbour[2][3] == 1)
nCount++;
if (neighbour[2][3] == 0 && neighbour[1][3] == 1)
nCount++;
if (neighbour[1][3] == 0 && neighbour[1][2] == 1)
nCount++;
if (nCount == 1)
bCondition2 = TRUE;
//判断P2*P4*P8=0 or Z0(p2)!=1
if (neighbour[1][2]*neighbour[2][1]*neighbour[2][3] == 0)
bCondition3 = TRUE;
else
{
nCount = 0;
if (neighbour[0][2] == 0 && neighbour[0][1] == 1)
nCount++;
if (neighbour[0][1] == 0 && neighbour[1][1] == 1)
nCount++;
if (neighbour[1][1] == 0 && neighbour[2][1] == 1)
nCount++;
if (neighbour[2][1] == 0 && neighbour[2][2] == 1)
nCount++;
if (neighbour[2][2] == 0 && neighbour[2][3] == 1)
nCount++;
if (neighbour[2][3] == 0 && neighbour[1][3] == 1)
nCount++;
if (neighbour[1][3] == 0 && neighbour[0][3] == 1)
nCount++;
if (neighbour[0][3] == 0 && neighbour[0][2] == 1)
nCount++;
if (nCount != 1)
bCondition3 = TRUE;
}
//判断P2*P4*P6=0 or Z0(p4)!=1
if (neighbour[1][2]*neighbour[2][1]*neighbour[3][2] == 0)
bCondition4 = TRUE;
else
{
nCount = 0;
if (neighbour[1][1] == 0 && neighbour[1][0] == 1)
nCount++;
if (neighbour[1][0] == 0 && neighbour[2][0] == 1)
nCount++;
if (neighbour[2][0] == 0 && neighbour[3][0] == 1)
nCount++;
if (neighbour[3][0] == 0 && neighbour[3][1] == 1)
nCount++;
if (neighbour[3][1] == 0 && neighbour[3][2] == 1)
nCount++;
if (neighbour[3][2] == 0 && neighbour[2][2] == 1)
nCount++;
if (neighbour[2][2] == 0 && neighbour[1][2] == 1)
nCount++;
if (neighbour[1][2] == 0 && neighbour[1][1] == 1)
nCount++;
if (nCount != 1)
bCondition4 = TRUE;
}
if(bCondition1 && bCondition2 && bCondition3 && bCondition4)
{
*lpDst = (unsigned char)255;
bModified = TRUE;
}
else
{
*lpDst = (unsigned char)0;
}
}
}
// 复制腐蚀后的图像
memcpy(lpDIBBits, hNewDIBBits, lWidth * lHeight);
}
// 复制腐蚀后的图像
memcpy(lpDIBBits, hNewDIBBits, lWidth * lHeight);
// 释放内存
// 返回
return TRUE;
}
int main()
{
int width,height,kk;
//BITMAPINFOHEADER *bmih;
input_file =fopen("F:\\c6000\\0332333\\robinhood\\test.bmp","rb");
if(input_file==NULL) return 0;
readSize=fread(inp_buffer,sizeof(unsigned char),81080,input_file);
fclose(input_file);
BITMAPINFOHEADER *bmih = (BITMAPINFOHEADER *)(inp_buffer+14);
width = bmih->biwidth ;
height = bmih->biheight ;
ThiningDIB(inp_buffer+1078,400, 200);
return 1;
}
这是我移植后的程序 |
|