对象目前正在其他地方使用!!!!我该怎么办呢?

时间:2017-05-27 13:28:29

标签: c# multithreading face-detection face-recognition viola-jones

请问我这个程序有问题 出现错误(对象当前正在其他地方使用)和(位图区域被锁定)“如果您在gethdc方法调用releasehdc方法后使用图形对象” 我试图使用Viola Jones检测面部并通过SURF算法提取特征然后进行匹配:请问我该如何解决这个错误?代码如下: private void btnStartCam_Click(object sender,EventArgs e)         {             if(radWepCam.Checked == true)

            if (videoSource.IsRunning)
            {
                _worker = new BackgroundWorker();
                _worker.WorkerSupportsCancellation = true;

                _worker.DoWork += new DoWorkEventHandler((state, args) =>
                {


                    do
                    {
                        try
                        {
                            var cascade = new FaceHaarCascade();
                            var detector = new HaarObjectDetector(cascade, 40);

                            detector.SearchMode = ObjectDetectorSearchMode.Average;
                            detector.Suppression = 3;
                            detector.ScalingFactor = 1.25f;
                            detector.UseParallelProcessing = true;
                            detector.ScalingMode = ObjectDetectorScalingMode.SmallerToGreater;
                            detector.UseParallelProcessing = true;


                            Bitmap image = (Bitmap)pictureBox1.Image.Clone();


                            {

                                lock (image)
                                {
                                    faceObjects = detector.ProcessFrame(image).ToArray();

                                    FaceCount = 0;

                                    foreach (var face in faceObjects)
                                    {

                                        Crop crop = new Crop(face);
                                        newimage = new Bitmap(crop.Apply(image));
                                        face.Inflate(-10, -10);
                                        facearray[FaceCount] = newimage;
                                        pictureBox2.Image = newimage;
                                        FaceCount = FaceCount + 1;
                                    }
                                }

                            int FaceCount2 = FaceCount;
                            for (int r = 0; r < FaceCount2 ; r++)
                            {

                                Bitmap imsetres = facearray[r];
                                System.Drawing.Image imgg1 = imsetres;

                                Image<Gray, Byte> g_image_1 = new Image<Gray, byte>((Bitmap)imgg1);
                                int endj = listAllEmployee.Items.Count - 1;
                                for (int j = 0; j < endj; j++)
                                {
                                    int max = 0;
                                    Bitmap imsetres2 = facearrayDB[j];
                                    imsetres2.SetResolution(320, 280);
                                    System.Drawing.Image imgg2 = imsetres2;

                                    pictureBox3.Image = imgg2;
                                    Image<Gray, Byte> g_image_2 = new Image<Gray, byte>((Bitmap)imgg2);
                                    ImageFeature[] image_feacher_1 = detect1.DetectFeatures(g_image_1, null);
                                    ImageFeature[] image_feacher_2 = detect1.DetectFeatures(g_image_2, null);

                                    Features2DTracker traker = new Features2DTracker(image_feacher_1);
                                    Features2DTracker.MatchedImageFeature[] matching = traker.MatchFeature(image_feacher_2, 2, 50);

                                    matching = Features2DTracker.VoteForUniqueness(matching, 0.9);

                                    matching = Features2DTracker.VoteForSizeAndOrientation(matching, 1.5, 20);
                                    int machno = 0;

                                    foreach (Features2DTracker.MatchedImageFeature matchedFeature in matching)
                                    {
                                        machno = machno + 1;

                                    }
                                    if (max <= machno)
                                    {
                                        max = machno;
                                    }
                                    if (max >= 30)
                                    {
                                        Image<Gray, Byte> res = g_image_1.ConcateVertical(g_image_2);
                                        MessageBox.Show("Test Found " + j + "with matching points " + max);
                                        foreach (Features2DTracker.MatchedImageFeature matchedFeature in matching)
                                        {

                                            PointF f = matchedFeature.ObservedFeature.KeyPoint.Point;
                                            f.Y += g_image_1.Height;
                                            res.Draw(new LineSegment2DF(matchedFeature.SimilarFeatures[0].Feature.KeyPoint.Point, f), new Gray(0), 1);

                                        }
                                        ImageViewer Imagee = new ImageViewer(res);
                                        Imagee.ShowDialog();
                                    }
                                    matching = null;
                                    traker = null;
                                    image_feacher_1 = null;
                                    image_feacher_2 = null;

                                }
                            }

                        }


                        }


                        catch (Exception ex)
                        { 
                            MessageBox.Show("Error" + ex.Message);

                        }


                    }

                    while (!_worker.CancellationPending);
                });
                _worker.RunWorkerAsync();
                btnStartCam.Enabled = false;
                btnPauseCam.Enabled = true;
            }

0 个答案:

没有答案