Advertisement
Ang377ou

countdetection

Mar 23rd, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Imaging;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using AForge.Imaging.Filters;
  12. using AForge.Imaging;
  13. using AForge;
  14.  
  15. namespace prefinalexam
  16. {
  17. public partial class Form1 : Form
  18. {
  19.  
  20.  
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void bTNSAVE_Click(object sender, EventArgs e)
  27. {
  28. pictureBox1.Image = PanelToBitmap(paint1);
  29. SaveFileDialog sfd = new SaveFileDialog();
  30. sfd.Filter = "Image|*.jpg";
  31. sfd.FileName = "Image.jpg";
  32. sfd.Title = "Save picture";
  33.  
  34. if (DialogResult.OK == sfd.ShowDialog())
  35. {
  36. pictureBox1.Image = MakeGrayscale3((Bitmap)pictureBox1.Image);
  37. pictureBox1.Image.Save(sfd.FileName);
  38.  
  39. }
  40. }
  41. private System.Drawing.Image PanelToBitmap(Control paint1)
  42. {
  43. var bmp = new Bitmap(paint1.Width, paint1.Height);
  44. paint1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
  45. return bmp;
  46. }
  47. public Bitmap MakeGrayscale3(Bitmap original)
  48. {
  49. //create a blank bitmap the same size as original
  50. Bitmap newBitmap = new Bitmap(original.Width, original.Height);
  51.  
  52. //get a graphics object from the new image
  53. Graphics g = Graphics.FromImage(newBitmap);
  54.  
  55. //create the grayscale ColorMatrix
  56. ColorMatrix colorMatrix = new ColorMatrix(
  57. new float[][]
  58. {
  59. new float[] {.3f, .3f, .3f, 0, 0},
  60. new float[] {.59f, .59f, .59f, 0, 0},
  61. new float[] {.11f, .11f, .11f, 0, 0},
  62. new float[] {0, 0, 0, 1, 0},
  63. new float[] {0, 0, 0, 0, 1}
  64. });
  65.  
  66. //create some image attributes
  67. ImageAttributes attributes = new ImageAttributes();
  68.  
  69. //set the color matrix attribute
  70. attributes.SetColorMatrix(colorMatrix);
  71.  
  72. //draw the original image on the new image
  73. //using the grayscale color matrix
  74. g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
  75. 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
  76.  
  77. //dispose the Graphics object
  78. g.Dispose();
  79. return newBitmap;
  80. }
  81.  
  82.  
  83. private void button1_Click(object sender, EventArgs e)
  84. {
  85. SusanCornersDetector s = new SusanCornersDetector();
  86. List<IntPoint> c = s.ProcessImage((Bitmap)pictureBox1.Image);
  87. Graphics g = Graphics.FromImage(pictureBox1.Image);
  88. SolidBrush b = new SolidBrush(Color.Red);
  89. Pen p = new Pen(b);
  90.  
  91. foreach (IntPoint k in c)
  92. {
  93. g.DrawRectangle(p, k.X - 1, k.Y - 1, 3, 3);
  94. }
  95. pictureBox1.Image = (Bitmap)pictureBox1.Image ;
  96. if (c.Count == 0)
  97. {
  98. MessageBox.Show("You Draw a circle");
  99. }
  100. else if (c.Count == 3)
  101. {
  102. MessageBox.Show("You Draw a triangle");
  103. }
  104. else if (c.Count == 4)
  105. {
  106. MessageBox.Show("You Draw a Square");
  107. }
  108. else
  109. {
  110. MessageBox.Show("What kind of object you draw?");
  111. }
  112. }
  113.  
  114. private void button2_Click(object sender, EventArgs e)
  115. {
  116. paint1.DrawingShapes = new PicLoaderE1.Shapes();
  117. paint1.Refresh();
  118.  
  119. }
  120.  
  121. private void Form1_Load(object sender, EventArgs e)
  122. {
  123.  
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement