C # Digital Image processing Algorithm learning notes (III.)--Image geometric transformation
The geometric image processing includes the image translation transformation, the mirror transformation, the rotation transformation, the telescopic transformation, in this case only with the horizontal mirror as an example, through the code to understand its basic operation way:
Before flipping:
After flipping:
//Backstage code:
Public Partial classForm1:form {Private string_curfilename; PrivateBitmap _srcbitmap; PrivateBitmap _dstbitmap; PublicForm1 () {InitializeComponent (); } Private voidOpen_click (Objectsender, EventArgs e) {OpenFileDialog Opndlg=NewOpenFileDialog (); Opndlg.filter=@"all image files |*.bmp;*.pcx;*.png;*.jpg;*.gif;"+@"*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*;eps;*.emf|"+@"bitmap (*.bmp;*.jpg;*.png; ...) | *.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|"+@"vector diagram (*.wmf;*.eps;*.emf; ...) | *.DXF;*.CGM;*.WMF;*.EPS;*.EMF"; Opndlg.title=@"Open an image file"; Opndlg.showhelp=true; if(Opndlg.showdialog () = =DialogResult.OK) {_curfilename=Opndlg.filename; Try{_srcbitmap=(Bitmap) image.fromfile (_curfilename); } Catch(Exception ee) {MessageBox.Show (EE. Message); }} Invalidate (); } Private voidClose_click (Objectsender, EventArgs e) {application.exit (); } Private voidForm1_paint (Objectsender, PaintEventArgs e) {Graphics g=E.graphics; if(_srcbitmap! =NULL) {g.drawimage (_srcbitmap, the, -, _srcbitmap.width, _srcbitmap.height); } }
//Flip processing function, here is the two pictures to flip the operation, or can be flipped directly with a picture, but to pay special attention to each pixel accounted for 3 byte bits, and flip the whole pixel is flipped together Private voidMirror_click (Objectsender, EventArgs e) { if(_srcbitmap! =NULL) {_dstbitmap=(Bitmap) _srcbitmap.clone (); BitmapData Bmpdata= _srcbitmap.lockbits (NewRectangle (0,0, _srcbitmap.width, _srcbitmap.height), Imagelockmode.readwrite, _srcbitmap.pixe Lformat); BitmapData Dstdata= _dstbitmap.lockbits (NewRectangle (0,0, _dstbitmap.width, _dstbitmap.height), Imagelockmode.readwrite, _dstbitmap.pixelformat); unsafe { byte* p = (byte*) Bmpdata.scan0; for(inty =0; Y < Bmpdata.height; y++) { for(intx =0; x < bmpdata.width; X + +) { byte* DSTP = (byte*) Dstdata.scan0 + y*dstdata.stride + x*3; dstp[0] = p[bmpdata.width*3-(x +1)*3]; dstp[1] = p[bmpdata.width*3-(x +1)*3+1]; dstp[2] = p[bmpdata.width*3-(x +1)*3+2]; } P+ = Bmpdata.stride;//Stride for scan width Stride > bmpdata.width * 3, Width indicates the number of pixels per row, 3 byte bits per pixel} _srcbitmap.unlockbits (Bmpdata); _dstbitmap.unlockbits (Dstdata); } _srcbitmap=(Bitmap) _dstbitmap.clone (); Invalidate (); } } }
Other geometries are similar to this operation, which operates directly on the original data through pointers, but pay particular attention to the cross-check of pointers.
C # Digital Image processing Algorithm learning notes (III.)--Image geometric transformation