private void button3_Click(object sender, EventArgs e) { Bitmap bm = new Bitmap(original_image); Bitmap32 bm32 = new Bitmap32(bm); bm32.Grayscale(); pictureBox2.Image = bm; grayscale_image = bm; label2.ForeColor = Color.Green; ImageProcessing img_p = new ImageProcessing(); Point loc = img_p.GetLocation_Top_Left(grayscale_image); Point loc1 = img_p.GetLocation_Width_Height(grayscale_image); label3.ForeColor = Color.Green; label5.Text = String.Format("X = {0}; Y = {1}", loc.X, loc.Y); // +10 + 10 label6.Text = String.Format("XW = {0}; YH = {1}", loc1.X, loc1.Y); Bitmap bmp_gr = new Bitmap(grayscale_image); Image <Bgr, Byte> sourceImage1 = new Image <Bgr, Byte>(bmp_gr); sourceImage1.ROI = new Rectangle(loc.X + 10, loc.Y + 10, (loc1.X - loc.X) - 20, (loc1.Y - loc.Y) - 20); pictureBox2.Image = sourceImage1.Clone().ToBitmap(); cliped_image = sourceImage1.Clone().ToBitmap(); pictureBox2.SizeMode = PictureBoxSizeMode.AutoSize; label4.ForeColor = Color.Green; }
private void button6_Click(object sender, EventArgs e) { Bitmap bm = new Bitmap(cv_image); Bitmap32 bm32 = new Bitmap32(bm); bm32.Grayscale(); pictureBox2.Image = bm; ImageProcessing pr = new ImageProcessing(); List <char> res = pr.ProcessResults(pictureBox2.Image); for (int i = 0; i < 10; i++) { textBox2.Text += string.Format("{0}) {1} \n", i + 1, res[i]); } }
// Make a deep copy of this object. public Bitmap32 Clone() { // See if we are locked. bool was_locked = this.IsLocked; // Lock this bitmap. this.LockBitmap(); // Perform a shallow copy. Bitmap32 result = (Bitmap32)this.MemberwiseClone(); // Copy the Bitmap. result.Bitmap = new Bitmap(this.Bitmap.Width, this.Bitmap.Height); result._IsLocked = false; // Unlock if appropriate. if (!was_locked) { this.UnlockBitmap(); } // Return the result. return(result); }
// Apply a filter to the image. public Bitmap32 ApplyFilter(Filter filter, bool lock_result) { // Make a copy of this Bitmap32. Bitmap32 result = this.Clone(); // Lock both bitmaps. bool was_locked = this.IsLocked; this.LockBitmap(); result.LockBitmap(); // Apply the filter. int xoffset = -(int)(filter.Kernel.GetUpperBound(1) / 2); int yoffset = -(int)(filter.Kernel.GetUpperBound(0) / 2); int xmin = -xoffset; int xmax = Bitmap.Width - filter.Kernel.GetUpperBound(1); int ymin = -yoffset; int ymax = Bitmap.Height - filter.Kernel.GetUpperBound(0); int row_max = filter.Kernel.GetUpperBound(0); int col_max = filter.Kernel.GetUpperBound(1); for (int x = xmin; x <= xmax; x++) { for (int y = ymin; y <= ymax; y++) { // Skip the pixel if any under the kernel // is completely transparent. bool skip_pixel = false; // Apply the filter to pixel (x, y). float red = 0, green = 0, blue = 0; for (int row = 0; row <= row_max; row++) { for (int col = 0; col <= col_max; col++) { int ix = x + col + xoffset; int iy = y + row + yoffset; byte new_red, new_green, new_blue, new_alpha; this.GetPixel(ix, iy, out new_red, out new_green, out new_blue, out new_alpha); // See if we should skip this pixel. if (new_alpha == 0) { skip_pixel = true; break; } red += new_red * filter.Kernel[row, col]; green += new_green * filter.Kernel[row, col]; blue += new_blue * filter.Kernel[row, col]; } if (skip_pixel) { break; } } if (!skip_pixel) { // Divide by the weight, add the offset, and // make sure the result is between 0 and 255. red = filter.Offset + red / filter.Weight; if (red < 0) { red = 0; } if (red > 255) { red = 255; } green = filter.Offset + green / filter.Weight; if (green < 0) { green = 0; } if (green > 255) { green = 255; } blue = filter.Offset + blue / filter.Weight; if (blue < 0) { blue = 0; } if (blue > 255) { blue = 255; } // Set the new pixel's value. result.SetPixel(x, y, (byte)red, (byte)green, (byte)blue, this.GetAlpha(x, y)); } } } // Unlock the bitmaps. if (!lock_result) { result.UnlockBitmap(); } if (!was_locked) { this.UnlockBitmap(); } // Return the result. return(result); }