Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. public static Class1 Instance
  2. {
  3. get
  4. {
  5. if(instance == null)
  6. {
  7. lock(typeof(Class1)) { instance = new Class1();}
  8. }
  9. }
  10. }
  11. private static Class1 instance;
  12. Emgu.CV.Image<Bgr ,Byte> scaledImage = new Emgu.CV.Image<Bgr , Byte>(1920,1200);
  13. Bitmap rawBitmap;
  14. Size size = new Size(x, y); // the size I want to scale the image to..
  15. bool continueCapturing = false;
  16. System.Threading.Thread thread;
  17. public Emgu.CV.Image<Bgr, Byte> getPicture()
  18. {
  19. try
  20. {
  21. lock(scaledImage)
  22. {
  23. return scaledImage.Clone(); //error appears here
  24. }
  25. }
  26. catch
  27. {
  28. return new Emgu.CV.Image<Bgr, Byte>(1,1);
  29. }
  30. }
  31.  
  32. public void startCapturing()
  33. {
  34. this.continueCapturing = true;
  35. thread = new System.Threading.Thread((obj)=>this.entryPoint();
  36. thread.Start();
  37. }
  38.  
  39. private void entryPoint()
  40. {
  41. while(Class1.Instance.continueCapturing)
  42. {
  43. rawBitmap = getBitmapFromCamera();
  44. lock(scaledImage)
  45. {
  46. scaledImage.Bitmap = rawBitmap; // gets the newest image from the camera
  47. scaledImage = scaledImage.Resize(size.Width, size.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
  48. }
  49. }
  50. }
  51.  
  52. private System.Timers.Timer timerVideo = new System.Timers.Timer(50);
  53. private System.Timers.Timer timerFOD = new System.Timers.Timer(110);
  54. private Graphics graphics;
  55. public void startAnalyzing()
  56. {
  57. graphics = Graphics.FromImage(fod);
  58.  
  59. timerVideo.Elapsed += new System.Timers.ElapsedEventHandler(this.timerElapsedVideo);
  60. timerFOD.Elapsed += new System.Timers.ElapsedEventHandler(this.timerElapsedFOD);
  61. timerVideo.Start();
  62. timerFOD.Start();
  63. }
  64. public PictureBox pictureBox;
  65. private Bitmap unlocked;
  66. private Bitmap locked
  67. {
  68. get
  69. {
  70. lock(unlocked)
  71. {
  72. return (Bitmap)unlocked.Clone();
  73. }
  74. }
  75. set
  76. {
  77. lock(unlocked)
  78. {
  79. unlocked = value;
  80. }
  81. }
  82. }
  83.  
  84. private void timerElapsedVideo(object sender, SystemTimers.ElapsedEventArgs e)
  85. {
  86. lock(graphics)
  87. {
  88. graphics.DrawImage(Class1.Instance.getPicture().ToBitmap(),0,0);
  89. graphics.DrawImage(locked, 0, 0);
  90. }
  91. if(pictureBox.InvokeRequired)
  92. {
  93. pictureBox.Invoke((MethodInvoker)delegate
  94. {
  95. pictureBox.Image = fod;
  96. });
  97. }
  98. }
  99. Image<Gray, Byte> FlowBitmapGray;
  100. Image<Gray, Byte> BackGroundGray;//this Image is being assigned somewhere else..
  101. private void timerElapsedFOD(object sender, System.Timers.ElapsedEventArgs e)
  102. {
  103. FlowBitmapGray = Class1.Instance.getPicture().Convert<Gray, Byte>();
  104. FlowBitmapGray = FlowBitmapGray.AbsDiff(BackGroundGray);
  105. FlowBitmapGray = FlowBitmapGray.SmoothMedian(25);
  106. FlowBitmapGray = FlowBitmapGray.ThresholdBinary(FlowBitmapGray.GetAverage().Intensity*6, new Emgu.Cv.Structure.Gray(255));
  107. FlowBitmapGray = FlowBitmapGray.SmoothMedian(25);
  108. locked = MakeSpecialTransparent(FlowBitmap.Gray.Convert<Emgu.CV.Structure.Bgra, Byte>().ToBitmap));
  109. }
  110.  
  111. private static Bitmap MakeSpecialTransparent(Bitmap bmp)
  112. {
  113. var bmpData = bmp.LockBits(new Rectangle(0,0, bmp.Width, bmp.Height), System.Drawing Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
  114.  
  115. long len = bmpData.Height*bmpData.Stride;
  116. byte[] data = new byte[len];
  117. System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, data.Length);
  118. for(int i = 0;i<len;i+=4)
  119. {
  120. if(data[i] == 0)
  121. data[i+3] = 0;
  122. else
  123. {
  124. data[i] = data[i+1] = 0; data[i+3]/=2;
  125. }
  126. }
  127. System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
  128. bmp.UnlockBits(bmpData);
  129. return bmp;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement