📄 edge.cpp
字号:
#include <math.h>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string.h>
using namespace std;
int main()
{
string infilename("004.bmp" );
ifstream infile( infilename.c_str() ,ios::binary );
ofstream outfile("BinPro.bmp" , ios::binary);
ofstream edgefile( "edge.bmp" , ios::binary);
if( ! edgefile )
cout<<"edgefile create failed!"<<endl;
if( ! infile )
cout<<"file open failed!"<<endl;
if( ! outfile )
cout<<"out create failed!"<<endl;
BITMAPFILEHEADER filehdr;
BITMAPINFOHEADER infohdr;
infile.read( (char * ) &filehdr , sizeof(filehdr) );
infile.read( (char * ) &infohdr , sizeof(infohdr) );
outfile.write( (char * ) &filehdr , sizeof(filehdr) ); cout<<"filehdr"<<sizeof(filehdr)<<endl;
outfile.write( (char * ) &infohdr , sizeof(infohdr) ); cout<<"infohdr"<<sizeof(infohdr)<<endl;
edgefile.write( (char * ) &filehdr , sizeof(filehdr) );
edgefile.write( (char * ) &infohdr , sizeof(infohdr) );
cout<<filehdr.bfSize<<endl;
cout<<filehdr.bfOffBits<<endl;
cout<<"width="<<infohdr.biWidth<<endl;
cout<<"height="<<infohdr.biHeight<<endl;
cout<<"biBitCount="<<infohdr.biBitCount<<endl;
cout<<"biClrUsed="<<infohdr.biClrUsed<<endl;
cout<<"biSizeImage="<<infohdr.biSizeImage<<endl;
if( infohdr.biBitCount != 24 ) //不是标准的24位位图
{
cout<<" 不是标准的24位位图! 无法打开!"<<endl;
return -1;
}
BYTE byte;
int Threshold = 128*3;
BYTE B0 = 0;
BYTE B255 = 255;
int temp = 0;
//创建0 1 矩阵存放二值化的图,用一维数组表示矩阵
BYTE * Pixel = new BYTE[infohdr.biWidth * infohdr.biHeight ];
BYTE * EdgePixel = new BYTE[ infohdr.biWidth * infohdr.biHeight];
BYTE * PixelHdr = Pixel;
BYTE * EdgePixelHdr = EdgePixel;
for(int i=0; i<infohdr.biSizeImage && !infile.eof() ; i++)
{
infile.read( (char * ) &byte, sizeof(byte) );
temp += (int)byte;
if( (i+1) % 3 == 0 )
{
if( temp >= Threshold )
{
outfile.write( (char *) &B255 , sizeof(BYTE) );
outfile.write( (char *) &B255 , sizeof(BYTE) );
outfile.write( (char *) &B255 , sizeof(BYTE) );
(* EdgePixel)= (* Pixel) = 1; // 标记浅色区域为白色
}
else
{
outfile.write( (char *) &B0 , sizeof(BYTE) );
outfile.write( (char *) &B0 , sizeof(BYTE) );
outfile.write( (char *) &B0 , sizeof(BYTE) );
(* EdgePixel)= (* Pixel) = 0; // 标记深色区域为黑色
}
Pixel++;
EdgePixel++;
temp = 0;
}
}
cout<<"i="<<i<<endl;
//下面对二值化的Pixel矩阵进行轮廓提取运算,轮廓保存在EdgePixel数组中,图像保存在bmp文件outfile中
Pixel = PixelHdr;
EdgePixel = EdgePixelHdr;
for( i=1; i<infohdr.biHeight-1; i++)
{
for(int j=1; j<infohdr.biWidth-1; j++)
{
BYTE * CurrentPixel = PixelHdr+infohdr.biWidth*i+j;
if( (* CurrentPixel) == 0 )
{
BYTE nw = * (CurrentPixel+infohdr.biWidth-1);
BYTE n = * (CurrentPixel+infohdr.biWidth);
BYTE ne = * (CurrentPixel+infohdr.biWidth+1);
BYTE e = * (CurrentPixel+1);
BYTE se = * (CurrentPixel-infohdr.biWidth+1);
BYTE s = * (CurrentPixel-infohdr.biWidth);
BYTE sw = * (CurrentPixel-infohdr.biWidth-1);
BYTE w = * (CurrentPixel-1);
if( nw+n+ne+e+se+s+sw+w == 0 )
*(EdgePixel+infohdr.biWidth*i+j) = 1;
} // 黑色区域内部点标记为白色
}
}
for(i=0; i<infohdr.biHeight; i++)
{
for(int j=0; j<infohdr.biWidth; j++)
{
if( *(EdgePixel+infohdr.biWidth*i+j) == 0 )
{
edgefile.write( (char *) &B0 , sizeof(BYTE) );
edgefile.write( (char *) &B0 , sizeof(BYTE) );
edgefile.write( (char *) &B0 , sizeof(BYTE) );
} //把剩下标记为黑色区域的点置为黑色,即是边界
else
{
edgefile.write( (char *) &B255 , sizeof(BYTE) );
edgefile.write( (char *) &B255 , sizeof(BYTE) );
edgefile.write( (char *) &B255 , sizeof(BYTE) );
} //标记为白色区域的点置为白色
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -