Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Class1 Instance
- {
- get
- {
- if(instance == null)
- {
- lock(typeof(Class1)) { instance = new Class1();}
- }
- }
- }
- private static Class1 instance;
- Emgu.CV.Image<Bgr ,Byte> scaledImage = new Emgu.CV.Image<Bgr , Byte>(1920,1200);
- Bitmap rawBitmap;
- Size size = new Size(x, y); // the size I want to scale the image to..
- bool continueCapturing = false;
- System.Threading.Thread thread;
- public Emgu.CV.Image<Bgr, Byte> getPicture()
- {
- try
- {
- lock(scaledImage)
- {
- return scaledImage.Clone(); //error appears here
- }
- }
- catch
- {
- return new Emgu.CV.Image<Bgr, Byte>(1,1);
- }
- }
- public void startCapturing()
- {
- this.continueCapturing = true;
- thread = new System.Threading.Thread((obj)=>this.entryPoint();
- thread.Start();
- }
- private void entryPoint()
- {
- while(Class1.Instance.continueCapturing)
- {
- rawBitmap = getBitmapFromCamera();
- lock(scaledImage)
- {
- scaledImage.Bitmap = rawBitmap; // gets the newest image from the camera
- scaledImage = scaledImage.Resize(size.Width, size.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
- }
- }
- }
- private System.Timers.Timer timerVideo = new System.Timers.Timer(50);
- private System.Timers.Timer timerFOD = new System.Timers.Timer(110);
- private Graphics graphics;
- public void startAnalyzing()
- {
- graphics = Graphics.FromImage(fod);
- timerVideo.Elapsed += new System.Timers.ElapsedEventHandler(this.timerElapsedVideo);
- timerFOD.Elapsed += new System.Timers.ElapsedEventHandler(this.timerElapsedFOD);
- timerVideo.Start();
- timerFOD.Start();
- }
- public PictureBox pictureBox;
- private Bitmap unlocked;
- private Bitmap locked
- {
- get
- {
- lock(unlocked)
- {
- return (Bitmap)unlocked.Clone();
- }
- }
- set
- {
- lock(unlocked)
- {
- unlocked = value;
- }
- }
- }
- private void timerElapsedVideo(object sender, SystemTimers.ElapsedEventArgs e)
- {
- lock(graphics)
- {
- graphics.DrawImage(Class1.Instance.getPicture().ToBitmap(),0,0);
- graphics.DrawImage(locked, 0, 0);
- }
- if(pictureBox.InvokeRequired)
- {
- pictureBox.Invoke((MethodInvoker)delegate
- {
- pictureBox.Image = fod;
- });
- }
- }
- Image<Gray, Byte> FlowBitmapGray;
- Image<Gray, Byte> BackGroundGray;//this Image is being assigned somewhere else..
- private void timerElapsedFOD(object sender, System.Timers.ElapsedEventArgs e)
- {
- FlowBitmapGray = Class1.Instance.getPicture().Convert<Gray, Byte>();
- FlowBitmapGray = FlowBitmapGray.AbsDiff(BackGroundGray);
- FlowBitmapGray = FlowBitmapGray.SmoothMedian(25);
- FlowBitmapGray = FlowBitmapGray.ThresholdBinary(FlowBitmapGray.GetAverage().Intensity*6, new Emgu.Cv.Structure.Gray(255));
- FlowBitmapGray = FlowBitmapGray.SmoothMedian(25);
- locked = MakeSpecialTransparent(FlowBitmap.Gray.Convert<Emgu.CV.Structure.Bgra, Byte>().ToBitmap));
- }
- private static Bitmap MakeSpecialTransparent(Bitmap bmp)
- {
- var bmpData = bmp.LockBits(new Rectangle(0,0, bmp.Width, bmp.Height), System.Drawing Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
- long len = bmpData.Height*bmpData.Stride;
- byte[] data = new byte[len];
- System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, data.Length);
- for(int i = 0;i<len;i+=4)
- {
- if(data[i] == 0)
- data[i+3] = 0;
- else
- {
- data[i] = data[i+1] = 0; data[i+3]/=2;
- }
- }
- System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
- bmp.UnlockBits(bmpData);
- return bmp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement