Object is Currently in use elsewhere!!!! how can i fix it?

540 Views Asked by At

Please I have problem in this program Error appear (Object is currently in use elsewhere)and (Bitmap region is locked)"if you are using the graphic object after the gethdc method call the releasehdc method" I am trying to detect faces using Viola Jones and extract features by SURF algorithm then make matching : Please how can i fix this error? the code is bellow: 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

There are 0 best solutions below