Home > Software engineering >  Using VC how to realize the edge detection, urgent for great god
Using VC how to realize the edge detection, urgent for great god

Time:10-31


I use this source code can change VC the wrong




# define ff (x, y) pBmpBuf0 [256 * (y) + (x)]
# define gg (x, y) pBmpBuf9 [256 * (y) + (x)]
#include
#include
#include
#include
Unsigned char * pBmpBuf;//read in the image data pointer
Int bmpWidth;//image of wide
Int bmpHeight;//image of high
The RGBQUAD * pColorTable;//color table pointer
Int biBitCount;//image type, each pixel
//read the bitmap image data, wide, high, color table and per pixel data into memory, such as in the corresponding global variable
Bool readBmp (char * bmpName)
{
The FILE * fp=fopen (bmpName, "rb");//binary read way to open the specified image file
If (fp==0)
return 0;
//skip the image file header BITMAPFILEHEADER structure
Fseek (fp, sizeof (BITMAPFILEHEADER), 0).
//bitmap information header structure variables, read bitmap information header into the memory, in variable head
BITMAPFILEHEADER head;
Fread (& amp; The head, sizeof (BITMAPFILEHEADER), 1, fp);//get the image width, height, each pixel of digit information such as
BmpWidth=head. BiWidth;
BmpHeight=head. BiHeight;
BiBitCount=head. BiBitCount;
//, define a variable based on each pixel of the image bytes (must be in multiples of 4)
Int lineByte=(bmpWidth * biBitCount/8 + 3)/4 * 4;//gray image have a color chart, and color table entries for 256
If (biBitCount==8)
{
//application space, color table need to read in the color table memory
PColorTable=new RGBQUAD [256].
Fread (pColorTable, sizeof (RGBQUAD), 256, fp);
}
//needed to apply for bitmap data space, read the bitmap data memory
PBmpBuf=new unsigned char [lineByte * bmpHeight];
Fread (pBmpBuf, 1, lineByte * bmpHeight, fp);
The fclose (fp);//close the file
return 1;//read the file success
}
//given a bitmap image data, a wide, high, color table pointer and each pixel of the digit information, it writes a specified file
Bool saveBmp (char * bmpName, unsigned char * imgBuf, int width, int height, int biBitCount, RGBQUAD * pColorTable)
{
//if the bitmap data pointer is 0, then there is no incoming data, the function returns
if(! ImgBuf)
return 0;
//color table size, in bytes, grayscale color table of 1024 bytes
Int colorTablesize=0;
If (biBitCount==8)
ColorTablesize=1024;
//to store image data bytes to 4 in a row of multiple
Int lineByte=(width * biBitCount/8 + 3)/4 * 4;
//written in binary mode to open the file
The FILE * fp=fopen (bmpName, "wb");
If (fp==0) return 0;
//for the bitmap file header structure variables, fill in the file header information
BITMAPFILEHEADER fileHead;
FileHead. BfType=0 x4d42;//BMP type
//bfsize image file is the sum of four components of
FileHead. BfSize=sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + colorTablesize + lineByte * height;
FileHead. BfReservedl=0;
FileHead. BfReserved2=0;
//bfOffBits is the sum of three part of the space needed before the image file
FileHead. BfOffBits=54 + colorTablesize;
//write head into the file
Fwrite (& amp; FileHead, sizeof (BITMAPFILEHEADER), 1, fp);
//application bitmap information header structure variables, fill in the information header
BITMAPINFOHEADER head;
Head. BiBitCount=biBitCount;
Head. BiClrImportant=0;
Head. The biClrUsed=0;
Head. The biCompression=0;
Head. BiHeight=height;
Head. BiPlanes=1;
Head. BiSize=40;
Head. BiSizeImage=lineByte * height;
Head. BiWidth=width;
Head. BiXPelsPerMeter=0;
Head. BiYPelsPerMeter=0;
//write bitmap information header into the memory
Fwrite (& amp; The head, sizeof (BITMAPINFOHEADER), 1, fp);
//if there is a gray image, a color table, write to the file
Fwrite (pColorTable, sizeof (RGBQUAD), 256, fp);
//write carry figure data into the file
Fwrite (imgBuf, height * lineByte, 1, fp);
//close the file
The fclose (fp);
return 1;
}

//the edge detection operator algorithm
//function declaration
Void Roberts (unsigned char * pBmpBufroberts);
Void sobel (unsigned char * pBmpBufsobel);
Void Robinson (unsigned char * pBmpBufrobinson);
Void prewitt (unsigned char * pBmpBufprewitt);
Void main ()
{
//read in specified BMP file into memory
Char readPath []="c:/text. BMP";
ReadBmp (readPath);
//output image information
Printf (" width=% d, height=% d, biBitCount=% d \ n ", bmpWidth, bmpHeight, biBitCount);
//function call
Roberts (pBmpBuf);
Sobel (pBmpBuf);
Robinson (pBmpBuf);
Prewitt (pBmpBuf);
//clear the buffer, pBmpBuf and pcolorTable is global variables in the file into application space
The delete [] pBmpBuf;
The delete [] pColorTable;
}
//the function definition
//Roberts operator
Void Roberts (unsigned char * pBmpBufroberts)
{
Unsigned char * pBmpBuf0;
Unsigned char * pBmpBuf9 (256 * 256),
Int ix, iy, vx, vy, lx=256;
PBmpBuf0=pBmpBufroberts;
//template 1
For (x=1; Ix<255; Ix++)
{
For (iy=1; Iy<255; Iy++)
{
Vx=ff (ix + 1, iy) - ff (ix, iy);
Vy=ff (ix, iy + 1) - ff (ix, iy);
Gg (ix, iy)=abs (ag) + abs (vy);
}
}
//the data image save
Char writePath []="textroberts1. BMP";
SaveBmp (writePath, pBmpBuf9, bmpWidth, bmpHeight, biBitCount, pColorTable);
2//template
For (x=1; Ix<255; Ix++)
For (iy=1; Iy<255; Iy++)
{
Vx=ff (ix, iy) - ff (iy ix + 1, + 1);
Vy=ff (ix + 1, iy) - ff (ix, iy + 1);
Gg (ix, iy)=abs (ag) + abs (vy);
}
//save the image data
Char writePath2 []="textroberts2. BMP";
SaveBmp (writePath2 pBmpBuf9, bmpWidth, bmpHeight, biBitCount, pColorTable);
}

//Robinson,
Void Robinson (unsigned char * pBmpBufrobin)
{
Unsigned char * pBmpBuf0;
PBmpBuf0=pBmpBufrobin;
Unsigned char pBmpBuf9 (256 * 256),
Int ix, iy, mx, my, jx, jy, JPV, mxav, im;
Static int mask [8] [3] [3]=
,2,1,0,0,0 {1, 1, 2, 1,
0, 1, 1, 2, 1, 0,
1, 1, - 2,0,2, 1, 1,
2, 1, 0, 1,0,1,0, 1, 2,
1, 2, 1,0,0,0,1,2,1,
0, 1, 2, 0, 1,2,1,0,
,2,0,2,1,0, 1, 0, - 1-1,
2,1,01,0, 1, 0, 1, 2,
};

For (x=1; Ix<255; Ix++)
For (iy=1; Iy<255; Iy++)
{
For (im=0; Im<=7. Im++)
{
JPV=0;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related