📄 morph.c
字号:
if(hBitmap!=NULL)
DeleteObject(hBitmap);
hDc=GetDC(hWnd);
hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
(LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
(LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
if(Hori)
hf=_lcreat("c:\\hdilation.bmp",0);
else
hf=_lcreat("c:\\vdilation.bmp",0);
_lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
_lwrite(hf,(LPSTR)lpTempImgData,BufSize);
_lclose(hf);
ReleaseDC(hWnd,hDc);
LocalUnlock(hTempImgData);
LocalFree(hTempImgData);
GlobalUnlock(hImgData);
return TRUE;
}
////////////////////////////////////////////////////////////////
BOOL MorphOpen(HWND hWnd,BOOL Hori)
{
DWORD BufSize;
LPBITMAPINFOHEADER lpImgData;
LPSTR lpPtr;
HLOCAL hTempImgData;
LPBITMAPINFOHEADER lpTempImgData;
LPSTR lpTempPtr;
HLOCAL hTempImgData1;
LPBITMAPINFOHEADER lpTempImgData1;
LPSTR lpTempPtr1;
HDC hDc;
HFILE hf;
LONG x,y;
unsigned char num;
int i;
if( NumColors!=256){
MessageBox(hWnd,"Must be a mono bitmap with grayscale palette!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
{
MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if((hTempImgData1=LocalAlloc(LHND,BufSize))==NULL)
{
MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
LocalFree(hTempImgData);
return FALSE;
}
//step 1: erosion,the result is stored in lpTempImgData
lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
//copy image data
memcpy(lpTempImgData,lpImgData,BufSize);
if(Hori)
{
for(y=0;y<bi.biHeight;y++){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes)+1;
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes)+1;
for(x=1;x<bi.biWidth-1;x++){
num=(unsigned char)*lpPtr;
if (num==0){
*lpTempPtr=(unsigned char)0;
for(i=0;i<3;i++){
num=(unsigned char)*(lpPtr+i-1);
if(num==255){
*lpTempPtr=(unsigned char)255;
break;
}
}
}
else *lpTempPtr=(unsigned char)255;
lpPtr++;
lpTempPtr++;
}
}
}
else{
for(y=1;y<bi.biHeight-1;y++){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
for(x=0;x<bi.biWidth;x++){
num=(unsigned char)*lpPtr;
if (num==0){
*lpTempPtr=(unsigned char)0;
for(i=0;i<3;i++){
num=(unsigned char)*(lpPtr+(i-1)*LineBytes);
if(num==255){
*lpTempPtr=(unsigned char)255;
break;
}
}
}
else *lpTempPtr=(unsigned char)255;
lpPtr++;
lpTempPtr++;
}
}
}
//step 2: dilation,the result is stored in lpTempImgData1
//copy image data
lpTempImgData1=(LPBITMAPINFOHEADER)LocalLock(hTempImgData1);
//copy image data
memcpy(lpTempImgData1,lpTempImgData,BufSize);
if(Hori)
{
for(y=0;y<bi.biHeight;y++){
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes)+1;
lpTempPtr1=(char *)lpTempImgData1+(BufSize-LineBytes-y*LineBytes)+1;
for(x=1;x<bi.biWidth-1;x++){
num=(unsigned char)*lpTempPtr;
if (num==255){
*lpTempPtr1=(unsigned char)255;
for(i=0;i<3;i++){
num=(unsigned char)*(lpTempPtr+i-1);
if(num==0){
*lpTempPtr1=(unsigned char)0;
break;
}
}
}
else *lpTempPtr1=(unsigned char)0;
lpTempPtr++;
lpTempPtr1++;
}
}
}
else{
for(y=1;y<bi.biHeight-1;y++){
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr1=(char *)lpTempImgData1+(BufSize-LineBytes-y*LineBytes);
for(x=0;x<bi.biWidth;x++){
num=(unsigned char)*lpTempPtr;
if (num==255){
*lpTempPtr1=(unsigned char)255;
for(i=0;i<3;i++){
num=(unsigned char)*(lpTempPtr+(i-1)*LineBytes);
if(num==0){
*lpTempPtr1=(unsigned char)0;
break;
}
}
}
else *lpTempPtr1=(unsigned char)0;
lpTempPtr++;
lpTempPtr1++;
}
}
}
if(hBitmap!=NULL)
DeleteObject(hBitmap);
hDc=GetDC(hWnd);
hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData1, (LONG)CBM_INIT,
(LPSTR)lpTempImgData1+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
(LPBITMAPINFO)lpTempImgData1, DIB_RGB_COLORS);
if(Hori)
hf=_lcreat("c:\\hmopen.bmp",0);
else
hf=_lcreat("c:\\vmopen.bmp",0);
_lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
_lwrite(hf,(LPSTR)lpTempImgData1,BufSize);
_lclose(hf);
ReleaseDC(hWnd,hDc);
LocalUnlock(hTempImgData);
LocalFree(hTempImgData);
LocalUnlock(hTempImgData1);
LocalFree(hTempImgData1);
GlobalUnlock(hImgData);
return TRUE;
}
////////////////////////////////////////////////////////////////
BOOL MorphClose(HWND hWnd,BOOL Hori)
{
DWORD BufSize;
LPBITMAPINFOHEADER lpImgData;
LPSTR lpPtr;
HLOCAL hTempImgData;
LPBITMAPINFOHEADER lpTempImgData;
LPSTR lpTempPtr;
HLOCAL hTempImgData1;
LPBITMAPINFOHEADER lpTempImgData1;
LPSTR lpTempPtr1;
HDC hDc;
HFILE hf;
LONG x,y;
unsigned char num;
int i;
if( NumColors!=256){
MessageBox(hWnd,"Must be a mono bitmap with grayscale palette!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
{
MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if((hTempImgData1=LocalAlloc(LHND,BufSize))==NULL)
{
MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
LocalFree(hTempImgData);
return FALSE;
}
//step 1: dilation,the result is stored in lpTempImgData
lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
//copy image data
memcpy(lpTempImgData,lpImgData,BufSize);
if(Hori)
{
for(y=0;y<bi.biHeight;y++){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes)+1;
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes)+1;
for(x=1;x<bi.biWidth-1;x++){
num=(unsigned char)*lpPtr;
if (num==255){
*lpTempPtr=(unsigned char)255;
for(i=0;i<3;i++){
num=(unsigned char)*(lpPtr+i-1);
if(num==0){
*lpTempPtr=(unsigned char)0;
break;
}
}
}
else *lpTempPtr=(unsigned char)0;
lpPtr++;
lpTempPtr++;
}
}
}
else{
for(y=1;y<bi.biHeight-1;y++){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
for(x=0;x<bi.biWidth;x++){
num=(unsigned char)*lpPtr;
if (num==255){
*lpTempPtr=(unsigned char)255;
for(i=0;i<3;i++){
num=(unsigned char)*(lpPtr+(i-1)*LineBytes);
if(num==0){
*lpTempPtr=(unsigned char)0;
break;
}
}
}
else *lpTempPtr=(unsigned char)0;
lpPtr++;
lpTempPtr++;
}
}
}
//step 2: erosion,the result is stored in lpTempImgData1
//copy image data
lpTempImgData1=(LPBITMAPINFOHEADER)LocalLock(hTempImgData1);
//copy image data
memcpy(lpTempImgData1,lpTempImgData,BufSize);
if(Hori)
{
for(y=0;y<bi.biHeight;y++){
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes)+1;
lpTempPtr1=(char *)lpTempImgData1+(BufSize-LineBytes-y*LineBytes)+1;
for(x=1;x<bi.biWidth-1;x++){
num=(unsigned char)*lpTempPtr;
if (num==0){
*lpTempPtr1=(unsigned char)0;
for(i=0;i<3;i++){
num=(unsigned char)*(lpTempPtr+i-1);
if(num==255){
*lpTempPtr1=(unsigned char)255;
break;
}
}
}
else *lpTempPtr1=(unsigned char)255;
lpTempPtr++;
lpTempPtr1++;
}
}
}
else{
for(y=1;y<bi.biHeight-1;y++){
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr1=(char *)lpTempImgData1+(BufSize-LineBytes-y*LineBytes);
for(x=0;x<bi.biWidth;x++){
num=(unsigned char)*lpTempPtr;
if (num==0){
*lpTempPtr1=(unsigned char)0;
for(i=0;i<3;i++){
num=(unsigned char)*(lpTempPtr+(i-1)*LineBytes);
if(num==255){
*lpTempPtr1=(unsigned char)255;
break;
}
}
}
else *lpTempPtr1=(unsigned char)255;
lpTempPtr++;
lpTempPtr1++;
}
}
}
if(hBitmap!=NULL)
DeleteObject(hBitmap);
hDc=GetDC(hWnd);
hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData1, (LONG)CBM_INIT,
(LPSTR)lpTempImgData1+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
(LPBITMAPINFO)lpTempImgData1, DIB_RGB_COLORS);
if(Hori)
hf=_lcreat("c:\\hmclose.bmp",0);
else
hf=_lcreat("c:\\vmclose.bmp",0);
_lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
_lwrite(hf,(LPSTR)lpTempImgData1,BufSize);
_lclose(hf);
ReleaseDC(hWnd,hDc);
LocalUnlock(hTempImgData);
LocalFree(hTempImgData);
LocalUnlock(hTempImgData1);
LocalFree(hTempImgData1);
GlobalUnlock(hImgData);
return TRUE;
}
////////////////////////////////////////////////////////////////
BOOL Thinning(HWND hWnd)
{
DWORD BufSize;
LPBITMAPINFOHEADER lpImgData;
LPSTR lpPtr;
HLOCAL hTempImgData;
LPBITMAPINFOHEADER lpTempImgData;
LPSTR lpTempPtr;
HDC hDc;
HFILE hf;
LONG x,y;
int num;
BOOL Finished;
int nw,n,ne,w,e,sw,s,se;
static int erasetable[256]={
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,1,
1,1,0,0,1,1,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,
1,1,0,1,1,1,0,1,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,
1,1,0,1,1,1,0,0,
1,1,0,0,1,1,1,0,
1,1,0,0,1,0,0,0
};
if( NumColors!=256){
MessageBox(hWnd,"Must be a mono bitmap with grayscale palette!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
{
MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
//copy image data
memcpy(lpTempImgData,lpImgData,BufSize);
Finished=FALSE;
while(!Finished){
Finished=TRUE;
for (y=1;y<bi.biHeight-1;y++){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
x=1;
while(x<bi.biWidth-1){
if(*(lpPtr+x)==0){
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
if( (w==255)|| (e==255)){
nw=(unsigned char)*(lpPtr+x+LineBytes-1);
n=(unsigned char)*(lpPtr+x+LineBytes);
ne=(unsigned char)*(lpPtr+x+LineBytes+1);
sw=(unsigned char)*(lpPtr+x-LineBytes-1);
s=(unsigned char)*(lpPtr+x-LineBytes);
se=(unsigned char)*(lpPtr+x-LineBytes+1);
num=nw/255+n/255*2+ne/255*4+w/255*8+e/255*16+sw/255*32+s/255*64+se/255*128;
if(erasetable[num]==1){
*(lpPtr+x)=(BYTE)255;
*(lpTempPtr+x)=(BYTE)255;
Finished=FALSE;
x++;
}
}
}
x++;
}
}
for (x=1;x<bi.biWidth-1;x++){
y=1;
while(y<bi.biHeight-1){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
if(*(lpPtr+x)==0){
n=(unsigned char)*(lpPtr+x+LineBytes);
s=(unsigned char)*(lpPtr+x-LineBytes);
if( (n==255)|| (s==255)){
nw=(unsigned char)*(lpPtr+x+LineBytes-1);
ne=(unsigned char)*(lpPtr+x+LineBytes+1);
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
sw=(unsigned char)*(lpPtr+x-LineBytes-1);
se=(unsigned char)*(lpPtr+x-LineBytes+1);
num=nw/255+n/255*2+ne/255*4+w/255*8+e/255*16+sw/255*32+s/255*64+se/255*128;
if(erasetable[num]==1){
*(lpPtr+x)=(BYTE)255;
*(lpTempPtr+x)=(BYTE)255;
Finished=FALSE;
y++;
}
}
}
y++;
}
}
}
if(hBitmap!=NULL)
DeleteObject(hBitmap);
hDc=GetDC(hWnd);
hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
(LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
(LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
hf=_lcreat("c:\\thinning.bmp",0);
_lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
_lwrite(hf,(LPSTR)lpTempImgData,BufSize);
_lclose(hf);
ReleaseDC(hWnd,hDc);
LocalUnlock(hTempImgData);
LocalFree(hTempImgData);
GlobalUnlock(hImgData);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -