关于对象跟踪 [英] About Object Tracing

查看:72
本文介绍了关于对象跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人:



现在我正在尝试使用我的网络摄像头做一些对象追踪。我读了一些文章说我需要抓住一张图片作为背景并使用另一张图片找出不同的部分来循环它。

但是当我编译我的程序时,它说GDI +错误....我不知道这是我的逻辑部分不正确或我需要改变什么。



Dear someone:

Now I am trying to do some "object Tracing" by using my web-camera. I read some paper said that I need to catch one picture to be the background and use another picture to find out different part to circular it out.
But when I compiled my program, it said that GDI+ error....I don''t know it''s my logical part incorrect or what should I need to change.

private void timer2_Tick(object sender, EventArgs e)
        {
            Bitmap myBitmap1 = pictureBox1.Image as Bitmap;
            if (myBitmap1 == null)
            {
                timer2.Stop();
            }
            else
            {
                // Here to Get BackGround Image and save to disk
                int[, ,] ImgData1 = GetImgData(myBitmap1);
                GrayProcess(ImgData1);
                GrayThreshold(ImgData1);
                Bitmap ProcessedBitmap1 = CreateBitmap(ImgData1);
                ProcessedBitmap1.Save(@"c:\temp1.jpg");

                delay(1);
                
                // Here to Get another Image and save to disk
                int[, ,] ImgData2 = GetImgData(myBitmap1);
                GrayProcess(ImgData2);
                GrayThreshold(ImgData2);
                Bitmap ProcessedBitmap2 = CreateBitmap(ImgData2);
                ProcessedBitmap2.Save(@"c:\temp2.jpg");
            }

            // Here I try to calculate changed pixel and compare both pictures to find out location
            Bitmap source1 = new Bitmap(@"c:\temp1.jpg");
            Bitmap source2 = new Bitmap(@"c:\temp2.jpg");

            int[, ,] New1 = new int[source1.Width, source1.Height, 3];
            int[, ,] New2 = new int[source2.Width, source2.Height, 3];
            BitmapData byteArray1 = source1.LockBits(new Rectangle(0, 0, source1.Width, source1.Height), 

ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            BitmapData byteArray2 = source2.LockBits(new Rectangle(0, 0, source2.Width, source2.Height), 

ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            int ByteOfSkip1 = byteArray1.Stride - byteArray1.Width * 3;
            int ByteOfSkip2 = byteArray2.Stride - byteArray2.Width * 3;
            unsafe
            {
                byte* imgPtr1 = (byte*)(byteArray1.Scan0);
                byte* imgPtr2 = (byte*)(byteArray2.Scan0);
                for (int y = 0; y <byteArray1.Height; y++)
                {
                    for (int x = 0; x < byteArray1.Width; x++)
                    {
                        New1[x, y, 2] = (int)*(imgPtr1);
                        New1[x, y, 1] = (int)*(imgPtr1+1);
                        New1[x, y, 0] = (int)*(imgPtr1+2);
                        New2[x, y, 2] = (int)*(imgPtr2);
                        New2[x, y, 1] = (int)*(imgPtr2 + 1);
                        New2[x, y, 0] = (int)*(imgPtr2 + 2);
                        imgPtr1 += 3;
                        imgPtr2 += 3;

                        int a = New1[x, y, 1];
                        int b = New2[x, y, 1];

                        if (a != b)
                        {

                            //Here to identify the location and circular it out
                            Graphics g = pictureBox1.CreateGraphics();
                            Pen pen = new Pen(Color.Red, 5);
                            int width, height,width1,height1;
                            width = source1.Width;
                            height = source1.Height;
                            width1 = pictureBox1.Size.Width;
                            height1 = pictureBox1.Size.Height;
                            int width_diff, height_diff;
                            width_diff = width1 - width;
                            height_diff = height1 - height;
                            g.DrawRectangle(pen, x, y, width_diff, height_diff);
                            //Refresh();
                        }

                    }
                    imgPtr1 += ByteOfSkip;
                    imgPtr2 += ByteOfSkip;
                }
            }
            source1.UnlockBits(byteArray1);
            source2.UnlockBits(byteArray2);
        }

推荐答案

否。

Tick事件处理程序是 void 方法 - 它不能返回任何值(它不是直接从你的代码中调用的,所以无处可以将值 返回到



并且Tick even处理程序也没有采用整数参数,所以你也不能这样做!

而不是,创建一个类级私有变量,它可以在两个Tick处理程序之间传递信息。



但请注意Tick事件处理程序可能没有像你期望的那样精确调用:它们不适用于真正的实时处理。



你想要实现什么,你认为这是一个好主意(甚至是必要的) - 可能有更好的方法来处理整个事情。
No.
The Tick event handler is a void method - it cannot return any value (it isn''t called directly from your code, so there is nowhere to return the value to)

And the Tick even handler does not take an integer parameter either, so you can''t do that either!
Instead, create a class level private variable which can transfer the information between the two Tick handlers.

But be aware that Tick event handler may not be called as precisely as you expect: they are not meant for true real-time processing.

What are you trying to achieve, that you think this is a good idea (or even necessary) - there may be a better way to handle the whole thing.


这篇关于关于对象跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆