C # simple image edge extraction,
It's easy for the blogger to take a look ......
The algorithm used is the robert operator, which is a relatively simple algorithm:
F (x, y) = sqrt (g (x, y)-g (x + 1, y + 1) ^ 2 + (g (x + 1, y) -g (x, y + 1) ^ 2)
The blogger has written a total of three sections of code, the first section is edge extraction, the second section is the rough line, the third section is the coincidence between the source image and the edge image, and the three sections of code can be put together, but I separated them for clarity.
Simple, rough, and direct code!
Private void Image_Test () {if (this. pBox. Image! = Null) {int Height = this. pBox. image. height; int Width = this. pBox. image. width; Bitmap bitmap = new Bitmap (Width, Height, PixelFormat. format24bppRgb); Bitmap MyBitmap = (Bitmap) this. pBox. image; BitmapData oldData = MyBitmap. lockBits (new Rectangle (0, 0, Width, Height), ImageLockMode. readOnly, PixelFormat. format24bppRgb); // The original BitmapData newData = bitmap. lockBits (new Rectangle (0, 0, Width, Height), ImageLockMode. readWrite, PixelFormat. format24bppRgb); // The new graph is the edge graph unsafe {// The first code is to extract the edge, the edge is set to black, and the other part is set to white byte * pin_1 = (byte *) (oldData. scan0.ToPointer (); byte * pin_2 = pin_1 + (oldData. stride); byte * pout = (byte *) (newData. scan0.ToPointer (); for (int y = 0; y <oldData. height-1; y ++) {for (int x = 0; x <oldData. width; x ++) {// use the robert operator double B = System. math. sqrt (double) pin_1 [0]-(double) (pin_2 [0] + 3) * (double) pin_1 [0]-(double) (pin_2 [0] + 3) + (double) (pin_1 [0] + 3)-(double) pin_2 [0]) * (double) (pin_1 [0] + 3)-(double) pin_2 [0]); double g = System. math. sqrt (double) pin_1 [1]-(double) (pin_2 [1] + 3) * (double) pin_1 [1]-(double) (pin_2 [1] + 3) + (double) (pin_1 [1] + 3)-(double) pin_2 [1]) * (double) (pin_1 [1] + 3)-(double) pin_2 [1]); double r = System. math. sqrt (double) pin_1 [2]-(double) (pin_2 [2] + 3) * (double) pin_1 [2]-(double) (pin_2 [2] + 3) + (double) (pin_1 [2] + 3)-(double) pin_2 [2]) * (double) (pin_1 [2] + 3)-(double) pin_2 [2]); double bgr = B + g + r; // The blogger has been entangled in not dividing by 3, I don't feel bad. Just adjust it when selecting the threshold.-if (bgr> 80) // The threshold is determined as edge when the threshold is exceeded (select an appropriate threshold) {B = 0; g = 0; r = 0;} else {B = 255; g = 255; r = 255;} pout [0] = (byte) (B ); pout [1] = (byte) (g); pout [2] = (byte) (r); pin_1 = pin_1 + 3; pin_2 = pin_2 + 3; pout = pout + 3;} pin_1 + = oldData. stride-oldData. width * 3; pin_2 + = oldData. stride-oldData. width * 3; pout + = newData. stride-newData. width * 3;} // here, the blogger adds a rough line---. If you do not like it, you can delete this code byte * pin_5 = (byte *) (newData. scan0.ToPointer (); for (int y = 0; y <oldData. height-1; y ++) {for (int x = 3; x <oldData. width; x ++) {if (pin_5 [0] = 0 & pin_5 [1] = 0 & pin_5 [2] = 0) {pin_5 [-3] = 0; pin_5 [-2] = 0; pin_5 [-1] = 0; // set the first pixel of the edge point to Black (note that it must be the pixel that has been traversed)} pin_5 + = 3;} pin_5 + = newData. stride-newData. width * 3;} // This code overwrites the source image and the edge image in byte * pin_3 = (byte *) (oldData. scan0.ToPointer (); byte * pin_4 = (byte *) (newData. scan0.ToPointer (); for (int y = 0; y <oldData. height-1; y ++) {for (int x = 0; x <oldData. width; x ++) {if (pin_4 [0] = 255 & pin_4 [1] = 255 & pin_4 [2] = 255) {pin_4 [0] = pin_3 [0]; pin_4 [1] = pin_3 [1]; pin_4 [2] = pin_3 [2];} pin_3 + = 3; pin_4 + = 3;} pin_3 + = oldData. stride-oldData. width * 3; pin_4 + = newData. stride-newData. width * 3 ;}//...... bitmap. unlockBits (newData); MyBitmap. unlockBits (oldData); this. pBox. image = bitmap ;}}}
Example 1:
Example 2: