資源簡(jiǎn)介
實(shí)現(xiàn)了bmp格式的圖像的灰度化,二值化,其中二值化應(yīng)用了最大類(lèi)間距的方法確定閥域。適合新手學(xué)習(xí)。
代碼片段和文件信息
#include
#include
#include
using?namespace?std;
FILE?*pPic=NULL;
BITMAPFILEHEADER?*pBitmapFileHeader;
BITMAPINFOHEADER?*pBitmapInfoHeader;
RGBQUAD?*pPalette?*pGrayPalette*pBinaryPalette;
int?linePixelgrayLinePixelbinaryLinePixel;//the?line?of?pixel?number
int?threshold=256/2;
unsigned?char?*pDIB*pGrayDIB*pBinaryDIB*pBigDIB;
void?openFileGetInfo()
{
????char??picName[20]=“your.bmp“;
????pBitmapFileHeader=new?BITMAPFILEHEADER;
????pBitmapInfoHeader=new?BITMAPINFOHEADER;
????FILE?*pPic=fopen(picName“rb“);
????if(pPic==0)
????{
????????cout<<“open?the?file?fail“< ????????exit(0);
????}
????/*void?getBITMAPFILEHEADER(){}*/
????fread(pBitmapFileHeadersizeof(BITMAPFILEHEADER)1pPic);
????if(pBitmapFileHeader->bfType!=0x4d42)
????{
????????cout<<“this?is?not?a?BMP?file\n“;
????????exit(0);
????}
????/*void?getBITMAPINFOHEADER(){}*/
????fread(pBitmapInfoHeadersizeof(BITMAPINFOHEADER)1pPic);
????/*void?getPalette(){}*/
???//?cout<<“用多少位表示顏色?=?“<biBitCount< ????if(pBitmapInfoHeader->biBitCount<24)
????{
????????cout<<“this?is?not?color?picture?!?\n“;
????????pPalette=new?RGBQUAD[1<biBitCount];
????????fread(pPalettesizeof(RGBQUAD)1<biBitCountpPic);
????}
????/*void?getDIB(FILE?*pic){}*/
????linePixel=((pBitmapInfoHeader->biWidth*pBitmapInfoHeader->biBitCount)/8+3)/4*4;
????pDIB=new?unsigned?char?[pBitmapInfoHeader->biHeight*linePixel];
????fread(pDIB1pBitmapInfoHeader->biHeight*linePixelpPic);
????fclose(pPic);
}
void?modifyFileHeader()
{
????pBitmapInfoHeader->biBitCount=8;
????pBitmapInfoHeader->biClrUsed=0;
????pBitmapFileHeader->bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256;
????grayLinePixel=?((pBitmapInfoHeader->biWidth*pBitmapInfoHeader->biBitCount)/8+3)/4*4;
????pBitmapInfoHeader->biSizeImage=grayLinePixel*pBitmapInfoHeader->biHeight;
????pBitmapFileHeader->bfSize=pBitmapFileHeader->bfOffBits+?pBitmapInfoHeader->biSizeImage;
}
void?createPalette()
{
????int?i=255;
????pGrayPalette=new?RGBQUAD[i+1];
????while(i>=0)
????{
????????pGrayPalette[i].rgbBlue=(byte)i;
????????pGrayPalette[i].rgbRed=(byte)i;
????????pGrayPalette[i].rgbGreen=(byte)i;
????????i--;
????}
}
void?modifyDIB()
{
????int?ij=0temp=0;
????int?countWidth=0;
????int?biWidth=pBitmapInfoHeader->biWidth;
????int?gapGray=grayLinePixel-biWidth;
????int?gapColour=linePixel-biWidth*3;
????pGrayDIB=new?unsigned?char?[grayLinePixel*pBitmapInfoHeader->biHeight];
????for(i=0;?ibiHeight;?i++)
????{
????????if(countWidth==biWidth)
????????{
????????????countWidth=0;
????????????j+=gapColour;
????????????for(temp=0;?temp ????????????{
????????????????pGrayDIB[i]=pGrayDIB[i-1];
????????????????i++;
????????????}
????????????i--;
????????????continue;
????????}
????????countWidth++;
??
評(píng)論
共有 條評(píng)論