Open BMP format Image file
BMP files are divided into bitmapfileheader,bitmapinforheader,rgbquad Three-part file header BF contains the type of file, the size of the file, the offset of the bitmap data from the file header, and so on,BI is the information that describes the bitmap, the number of color bits of the bitmap biBitCount, The height width of the bitmap, and the size of the bitmap data , can be decoded by reading this information from the BMP format file, opening the BMP file.
Routines:
Select File Lpctstr lpszfilter = "BMP files (*.bmp) |*.bmp| any file |*.*| |"; CFileDialog Dlg1 (true,lpszfilter,null,ofn_hidereadonly| Ofn_overwriteprompt,lpszfilter,null); CString filename; CFile file;if (Dlg1. DoModal () ==idok) {//read in file filename = Dlg1. GetPathName (); if (file. Open (filename,cfile::moderead| Cfile::sharedenynone,null) (==0) {AfxMessageBox ("Cannot open file", mb_ok,0); return;} Reads the header of the file, passing the size of sizeof (BF) data into the buffer bffile. Read (&bf,sizeof (BF)); Determine if the BMP file if (bf.bftype!=0x4d42) {AfxMessageBox ("Non-BMP file!", mb_ok,0); return;} Determine if the file is corrupt if (files). GetLength ()!=bf.bfsize) {AfxMessageBox ("file is corrupted, please check!"); return;} Reads the information header file. Read (&bi,sizeof (BI));//Calculate the number of palettes Numquad = 0;if (Bi.bibitcount < 24) {//if 1,4,8, 1 moves the corresponding number of digits to the left, the corresponding number of palettes is 2, 16,256numquad = 1<<bi.bibitcount;} For image information PBI request space PBI = (bitmapinfo*) HeapAlloc (GetProcessHeap (), 0,sizeof (Bitmapinfoheader) +numquad*sizeof (Rgbquad)); memcpy (pbi,&bi,sizeof (BI)), quad = (rgbquad*) ((byte*) pbi+sizeof (Bitmapinfoheader)); Read the palette if (numquad!=0) {file. Read (quad,sizeof (Rgbquad) *numquad);} Request space for image data Bi.bisizeimage = Bf.bfsize-bf.bfoffbits;lpbuf = (byte*) HeapAlloc (GetProcessHeap (), 0,bi.bisizeimage); Htempbuf=localalloc (Lhnd,bi.bisizeimage); lptempbuf= (byte*) LocalLock (HTEMPBUF);//LONG llinebytes;//lLineBytes =WI Dthbytes (3*LWIDTH*8); Calculates the number of bytes per line//bi.biheight*llinebytes//reads the image data file. Read (lpbuf,bi.bisizeimage);//image read complete, close file, set flag memcpy (lptempbuf,lpbuf,bi.bisizeimage); file. Close (); flag = 1; }
Grayscale processing (black and white effects):
Image grayscale is the color component of three colorsR,G,BBecause the value range of the color value is[0,255], so the grayscaleLevel only theGray-scale images can only represent thegray color, commonly used have3method of Processing:
*Maximum Value method(Maximum):R=g=b=max (r,g,b), the brightness of gray image will be higher after this method is processed.
*Average method(Average):r=g=b= (r+g+b)/3, the brightness of the gray image is softer after the method is processed.
*Weighted mean value method(Weighted Average):
R=g=b=wr*r+wg*g+wb*b,WR,Wg,WB, respectively, isR,G,Bthe weight value. When the weight value of different values, can form different gray gray image, because the human eye to the highest sensitivity to green, red followed by the lowest blue, so whenWG > WR > WB, the resulting grayscale image is more in line with the visual perception of the human eye. UsuallyWr=30%,Wg=59%,Wb=11%, the gray level of the image is most reasonable.
Routines:
cpicturedoc* PDoc = GetDocument (); Assert_valid (PDOC);//Copy the lpbuf pointer to lpdibbitslpstr lpdibbits = (LPSTR) globallock (PDOC->LPBUF); The pointer to the starting position of the image data long lwidth = pdoc->bi.biwidth; Source image width, number of pixels long lheight = pdoc->bi.biheight; Source image width, number of pixels unsigned char* lpsrc; A pixel corresponding to the pointer int gray; Gray corresponds to the pointer long i,j,llinebytes;llinebytes =widthbytes (3*lwidth*8); Calculates the number of bytes per row for (i=0;i<lheight;i++) {for (j=0;j<lwidth;j++) {lpsrc = (unsigned char*) lpdibbits+llinebytes* ( lheight-1-i) +3*j;gray = ((*LPSRC) *11+ (* (lpsrc+1)) *59+ (* (lpsrc+2)) *30)/100;*lpsrc = gray;* (lpsrc+1) = gray;* (lpSrc+2) = Gray;}} Bgray = 1;
cpicturedoc* PDoc = GetDocument (); Assert_valid (PDOC);//Copy the lpbuf pointer to lpdibbitslpstr lpdibbits = (LPSTR) globallock (PDOC->LPBUF); The pointer to the starting position of the image data long lwidth = pdoc->bi.biwidth; Source image width, number of pixels long lheight = pdoc->bi.biheight; Source image width, number of pixels unsigned char* lpsrc; A pixel corresponding to the pointer int gray; Gray corresponds to the pointer long i,j,llinebytes;llinebytes =widthbytes (3*lwidth*8); Calculates the number of bytes per row for (i=0;i<lheight;i++) {for (j=0;j<lwidth;j++) {lpsrc = (unsigned char*) lpdibbits+llinebytes* ( lheight-1-i) +3*j;gray = ((*LPSRC) *11+ (* (lpsrc+1)) *59+ (* (lpsrc+2)) *30)/100;*lpsrc = gray;* (lpsrc+1) = gray;* (lpSrc+2) = Gray;}} Bgray = 1;
Grayscale stretching:
Belongs to image enhancement technology, object grayscale range to improve the image, also if image grayscale image quality
I am stretched object grayscale range a Span style= "font-family: Arial" > The following pixel grayscale changes to 0 b pixels above becomes 255 a b 0-255
Grayscale is set to gray , then: 0 Gray < a
Gray = (*lpsrc-low_value) *rate+c a<gray<b
255 gray>b
Where low_value high_value c low_value~high_value 0~255 c take 0.5 rate = (255-0+1)/(high_value-low_value+1)
Routines: This routine sets the grayscale value near 0 10% to 0, which will be close to 255 's 10% set to 255, others for grayscale stretching
cpicturedoc* PDoc = GetDocument (); Assert_valid (PDOC);//Copy the lpbuf pointer to lpdibbitslpstr lpdibbits = (LPSTR) globallock (PDOC->LPBUF); The pointer to the starting position of the image data long lwidth = (pdoc->bi.biwidth); Source image width, number of pixels long lheight = pdoc->bi.biheight; Source image width, number of pixels unsigned char* lpsrc; A pointer to a pixel long i,j,llinebytes;llinebytes =widthbytes (lwidth*8); Computes the number of bytes per row byte bmap[256]; The gray value after storing the gray-scale stretch float rate=0; int temp;float stretch_num[256]; The number of times to store each gray level appears float stretch_p[256]; The ratio of each gray level to float stretch_sum[256]; The probability of storing each gray level and/or emptying three arrays memset (stretch_p,0,sizeof (stretch_p)); Memset (Stretch_sum,0,sizeof (stretch_sum)); memset (Stretch_num,0,sizeof (Stretch_num)); int low_value,high_value; The number of occurrences of each grayscale level for the image is stored for (i=0;i<lheight;i++) {for (j=0;j<lwidth;j++) {lpsrc = (unsigned char*) lpdibbits+llinebytes* (lheight-1-i) +j; stretch_num[*lpsrc]++;}} The probability of the occurrence of each gray level of the image for (i=0;i<256;i++) {stretch_p[i]=stretch_num[i]/(lwidth*lheight);} The probability of storing each gray level before and for (i=0;i<256;i++) {for (j=0;j<=i;j++) {stretch_sum[i]+=stretch_p[j];}} A value of two threshold points is counted for (i=0;i<256;i++) {if (stretch_sum[i]<0.1)//low_value takes a grayscale value of nearly 10% of the total pixels {low_value=i;} if (stretch_sum[i]>0.9)//high_value takes a grayscale value of nearly 90% of the total pixel {high_value=i;break;}} Rate= (float) 256/(high_value-low_value+1); Perform a grayscale stretch for (i=0;i<lheight;i++) {for (j=0;j<lwidth;j++) {lpsrc = (unsigned char*) lpdibbits+llinebytes* ( Lheight-1-i) +j;if (*lpsrc<low_value) {*lpsrc = 0;} else if (*lpsrc>high_value) {*lpsrc = 255;} else {temp= (*lpsrc-low_value) *rate) +0.5;if (temp<=255) {*lpsrc = temp;} else{*lpsrc = 255;}}}
Image corrosion:
After the z -translation of the morphological structure element B , if the structure elements are all contained in set A , then z belongs to the corroded collection. The meaning of this definition is to walk through all the pixels from the first pixel of the image, at each pixel point, to determine whether the structure element is all in the set A , if so the point belongs to the corroded collection, need to retain this point, Otherwise, the pity Dorado is reversed (the Pity Dorado grayscale value is set to 0 according to the following symbolic convention ).
If the image of white on black paper is corroded, it is only in the range of whiteness, if the left or right is black, it is set to black, otherwise unchanged.
Routines:
cpicturedoc* PDoc = GetDocument (); Assert_valid (PDOC);//Copy the lpbuf pointer to lpdibbitslpstr lpdibbits = (LPSTR) globallock (PDOC->LPBUF); The pointer to the starting position of the image data LPSTR Lptempdib = (LPSTR) globallock (PDOC->LPTEMPBUF); LONG lwidth = (pdoc->bi.biwidth); Source image width, number of pixels long lheight = pdoc->bi.biheight; Source image width, pixel number BYTE *lpsrc,*lptempsrc; A pointer to a pixel long i,j,llinebytes;llinebytes =widthbytes (lwidth*8); Calculates the number of bytes per row memcpy (pdoc->lptempbuf,pdoc->lpbuf,pdoc->bi.bisizeimage); Corrosion operation in horizontal direction for (i=0;i<lheight;i++) {for (j=3;j<lwidth-7;j=j+3)//Note to prevent out-of-bounds, J ranges from 1 to (width-2) {//LPSRC points to the original data LPSRC = (unsigned char*) lpdibbits+llinebytes* (lheight-1-i) +j;lptempsrc = (unsigned char*) lptempdib+llinebytes* (lheight-1-i) +j; if (*lptempsrc==255) {for (int x=0;x<7;x=x+3) {if ((* (* (lptempsrc+x-3))!=255) { If one of itself and the left and right neighbor is not a white point, it will corrode *lpsrc=* (lptempsrc+x-3); *(lpsrc+1) =* (lptempsrc+x-3); * (lpsrc+2) =* (lptempsrc+x-3); Break;}}}}
Image subtraction:
In image processing, the image subtraction is also a common concept, such as the image and background image subtraction can get the image of the object, the image and the image after the erosion can be obtained the outline of the object. Image subtraction is the corresponding pixel subtraction, relatively simple.
Routines:
cpicturedoc* PDoc = GetDocument (); Assert_valid (PDOC);//Copy the lpbuf pointer to lpdibbitslpstr lpdibbits = (LPSTR) globallock (PDOC->LPBUF); The pointer to the starting position of the image data LPSTR Lptempdib = (LPSTR) globallock (PDOC->LPTEMPBUF); LONG lwidth = (pdoc->bi.biwidth); Source image width, number of pixels long lheight = pdoc->bi.biheight; Source image width, number of pixels unsigned char* lpsrc,*lptempsrc; A pointer to a pixel long i,j,llinebytes;llinebytes =widthbytes (lwidth*8); Calculates the number of bytes per row for (i=0;i<lheight;i++) {for (j=0;j<lwidth;j++) {lpsrc = (unsigned char*) lpdibbits+llinebytes* ( lheight-1-i) +j;lptempsrc = (unsigned char*) lptempdib+llinebytes* (lheight-1-i) +j;*lpsrc = *lpsrc-*lpTempSrc;}}
Median filter:
In image processing, often encounter various noise (that is, unwanted pixels), median filter is to remove the unwanted pixels, to preserve the need for a pixel point method. It saves the five pixels from the top of the processing pixel to an array (y-2,y-1,y,y+1,y+2), compares its size, sorts it, and takes the median value as the pixel value of that point, which removes the highlighted pixel points.
Routines:
cpicturedoc* PDoc = GetDocument (); Assert_valid (PDOC);//Copy the lpbuf pointer to lpdibbitslpstr lpdibbits = (LPSTR) globallock (PDOC->LPBUF); The pointer to the starting position of the image data LPSTR Lptempdib = (LPSTR) globallock (PDOC->LPTEMPBUF); LONG lwidth = (pdoc->bi.biwidth); Source image width, number of pixels long lheight = pdoc->bi.biheight; Source image width, number of pixels unsigned char *lpsrc,*lptempsrc; A pointer to a pixel long i,j,llinebytes;llinebytes =widthbytes (lwidth*8); Computes the number of bytes per line int pfilter_image_pixel[5];//window pixel value int mid_pixel_value=0; Middle value int flag;int temp=0;//Intermediate variable//median filter memcpy (pdoc->lptempbuf,pdoc->lpbuf,pdoc->bi.bisizeimage); for (i=2;i <lheight-2;i++) {for (j=0;j<lwidth;j++) {lpsrc = (unsigned char*) lpdibbits+llinebytes* (lheight-1-i) +j; LPTEMPSRC = (unsigned char*) lptempdib+llinebytes* (lheight-1-i) +j;//put all the pixel values of the 5*1 Shield window into Pfilter_image_pixel[m]int m=0; for (int y=-2;y<=2;y++) {pfilter_image_pixel[m]=* (lptempsrc-llinebytes*y); m++;} Sort the values in pfilter_image_pixel[m] in descending order do{flag=0;for (int m=0;m<4;m++) {if (pfilter_image_pIxel[m]<pfilter_image_pixel[m+1]) {temp=pfilter_image_pixel[m];p filter_image_pixel[m]=pfilter_image_pixel[m+ 1];p filter_image_pixel[m+1]=temp;flag=1;} }}while (Flag==1); mid_pixel_value=pfilter_image_pixel[2];//The median value mid_pixel_value*lpsrc=mid_pixel_value;//assigns the median value to the current point of the target image}}
Binary Value:
Binary is a method of threshold transformation, that is, set a threshold value, the pixel is greater than the threshold value is set to 255, less than the threshold is set to 0, through two value to make the image more convenient to extract features.
Routines:
if (Bgray = = 0) {AfxMessageBox ("Grayscale processing First", MB_OK);} else{ cpicturedoc* pDoc = GetDocument (); Assert_valid (PDOC); LPSTR lpdibbits = (LPSTR) globallock (PDOC->LPBUF); The pointer to the starting position of the image data LONG lwidth = (pdoc->bi.biwidth); Source image width LONG lheight = pdoc->bi.biheight; Source image width BYTE bthre =; Threshold value unsigned char* lpsrc; A pixel corresponding to the pointer LONG i,j,llinebytes; Llinebytes =widthbytes (lwidth*8); Calculates the number of bytes per row for (i=0;i<lheight;i++) {for (j=0;j<lwidth;j++) { lpsrc = (unsigned char*) lpdibbits+ Llinebytes* (lheight-1-i) +j; if ((*LPSRC) <bthre) { *lpsrc = 0;} else{ *lpsrc = 255;}}}
Original:
After grayscale processing:
After grayscale stretching:
After corrosion:
After subtracting:
After the binary value:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Image base operation (with code)