Home > Back-end >  Read BMP file to gray scale to generate text way again
Read BMP file to gray scale to generate text way again

Time:10-11

Bosses, ask a question: after read BMP file to grayscale processing, again to write text way, some images can produce different degrees of deviation, and some of the pictures are normal grayscale images, and if it is a binarization, all images are normal,

#include
#include
#include

using namespace std;

Void main ()
{

The FILE * stream=NULL;
Errno_t err=0;
Err=fopen_s (& amp; The stream, "4. BMP," "rb");
if (err !=0)
{
cout <"File does not exist" & lt; return;
}


Int sizeFileHeader=sizeof (BITMAPFILEHEADER);//file header size
Int sizeInfoHeader=sizeof (BITMAPINFOHEADER);//information head size

BITMAPFILEHEADER * BITMAPFILEHEADER=new BITMAPFILEHEADER [sizeFileHeader];//structure pointer
BITMAPINFOHEADER * BITMAPINFOHEADER=new BITMAPINFOHEADER [sizeInfoHeader];//add a

Memset (bitmapFileHeader, 0, sizeFileHeader);//to the memory space assignment
Memset (bitmapInfoHeader, 0, sizeInfoHeader);//

Fread (bitmapFileHeader, sizeof (char), sizeFileHeader, stream).
Fseek (stream, sizeFileHeader, 0);
Fread (bitmapInfoHeader, sizeof (char), sizeInfoHeader, stream).

Int srcImageLineByteCount=(((bitmapInfoHeader - & gt; BiWidth) * 24 + 31)/32) * 4;//the original number of each row of data
Int destImageLineByteCount=(((bitmapInfoHeader - & gt; BiWidth) * 24 + 31)/32) * 4;//the result figure

//* * * * * * * * * * * * * * the bitmap information header * * * * * * * * * * * * * * * * *
BYTE BYTE oldImageData=https://bbs.csdn.net/topics/new * * * [bitmapInfoHeader -> biHeight];//pointer to a pointer, the equivalent of two dimensional array
For (int I=0; i {
OldImageData [I]=new BYTE [srcImageLineByteCount];
Memset (oldImageData [I], 0, srcImageLineByteCount);//add a
}

//* * * * * * * * * * * * * * the bitmap data * * * * * * * * * * * * * * * * * * *
Fseek (stream, sizeFileHeader + sizeInfoHeader, 0);//read from the data area began to
//read image data
For (int I=0; i{
For (int j=0; J & lt; SrcImageLineByteCount; J++)
{
Fread (& amp; OldImageData [I] [j], sizeof (BYTE), 1, stream);

}
}


//write data

BYTE BYTE newImageData=https://bbs.csdn.net/topics/new * * * [bitmapInfoHeader -> biHeight];


For (int I=0; i {
NewImageData [I]=new BYTE [destImageLineByteCount];
}

For (int I=0; i {
For (int j=0; J & lt; DestImageLineByteCount; J +=3)
{
[j]=newImageData newImageData [I] [I] [j + 1)=newImageData [I] [m + 2]=
(int) ((float) oldImageData [I] [j] * 0.114 +
(float) oldImageData [I] [j + 1) * 0.587 + (float) oldImageData [I] [m + 2) * 0.299);

/* if (newImageData [I] [j] & gt; 128)
[j]=newImageData newImageData [I] [I] [j + 1)=newImageData [I] [m + 2]=255;
The else
[j]=newImageData newImageData [I] [I] [j + 1)=newImageData [I] [m + 2]=0;
*/
}

}

//write file
The FILE * fileWrite=NULL;

Err=fopen_s (& amp; FileWrite, "19. BMP", "w +");//use wb++ is right here
Fwrite (bitmapFileHeader, sizeof (char), sizeof (bitmapFileHeader), fileWrite);
Fwrite (bitmapInfoHeader, sizeof (char), sizeof (bitmapInfoHeader), fileWrite);
//fwrite (pRgbQuards, sizeof (RGBQUAD), 256, fileWrite);

For (int I=0; i {
For (int j=0; J & lt; DestImageLineByteCount; J++)
{
Fwrite (& amp; NewImageData [I] [j], sizeof (BYTE), 1, fileWrite);
}

}
The fclose (fileWrite);

cout <"Success" & lt;
}
  • Related