图像数据访问中的C#和EMgu CV&图像匹配

时间:2013-12-04 14:07:53

标签: c# opencv sqldatasource emgucv face-recognition

我目前正在使用C#和Emgu CV在学生注册系统中创建一个实时的面部识别程序。 在开发它的过程中,我在访问图像数据和为其分配值时发现了几个问题。

我的问题如下:

我是否知道如何直接访问从网络摄像头捕获的图像中的图像数据?或者,如何将来自网络摄像头的“实时图像”连接到我的图像数据以处理面部图像匹配?

欢迎任何有关解决此问题的建议。

谢谢&问候,

Caulson Chua

2 个答案:

答案 0 :(得分:0)

要在EmguCV中捕获Image,您必须使用capture对象。

Capture capture = new Capture(); //create a camera capture

然后你可以像这样添加一个事件处理程序,以便在应用程序处于空闲状态时捕获一个新帧。

Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
{  

 Image<Bgr,Byte> latestFrame = capture.QueryFrame(); //draw the image obtained from camera

}); 

现在这个latestFrame变量将包含从网络摄像头捕获的当前帧,您可以在其上应用各种类型的图像处理。

PS:为了更多地了解EmguCV以及如何构建人脸识别系统,我建议你查看link

答案 1 :(得分:0)

   private void DetectFaces()
    {
        try
        {
            Image<Gray, byte> grayframe = TestImage.Convert<Gray, byte>();

            //Assign user-defined Values to parameter variables:
            MinNeighbors = int.Parse(comboBoxMinNeigh.Text);  // the 3rd parameter
            WindowsSize = int.Parse(textBoxWinSiz.Text);   // the 5th parameter
            ScaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text); //the 2nd parameter

            //detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]'
            var faces = grayframe.DetectHaarCascade(haar, ScaleIncreaseRate, MinNeighbors,
                                    HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new Size(WindowsSize, WindowsSize))[0];
            if (faces.Length > 0)
            {
                MessageBox.Show("Total Faces Detected: " + faces.Length.ToString());

                Bitmap BmpInput = grayframe.ToBitmap();
                Bitmap ExtractedFace;  // an empty "box"/"image" to hold the extracted face.

                Graphics FaceCanvas;

                //Set The Face Number
                //FaceCollection = Directory.GetFiles(@"Face Collection\", "*.bmp");
                //int FaceNo = FaceCollection.Length;

                //A Bitmap Array to hold the extracted faces
                EXfaces = new Bitmap[faces.Length];
                int i = 0;

                //draw a green rectangle on each detected face in image
                foreach (var face in faces)
                {
                    //locate the detected face & mark with a rectangle
                    TestImage.Draw(face.rect, new Bgr(Color.Green), 3);

                    //set the size of the empty box(ExtractedFace) which will later contain the detected face
                    ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height);

                    //set empty image as FaceCanvas, for painting
                    FaceCanvas = Graphics.FromImage(ExtractedFace);

                    //graphics draws the located face on the faceCancas, thus filling the empty ExtractedFace 
                    //image with exact pixels of the face to be extracted from input image
                    FaceCanvas.DrawImage(BmpInput, 0, 0, face.rect, GraphicsUnit.Pixel);

                    //save this extracted face to hard disk
                    //ExtractedFace.Save(@"Face Collection\" + "Face_" + (FaceNo++) + ".bmp");//save images in folder

                    //Save this extracted face to array
                    EXfaces[i] = ExtractedFace;
                    i++;

                }
                //Display the detected faces in imagebox
                imageBox1.Image = TestImage;

                MessageBox.Show(faces.Length.ToString() + " Face(s) Extracted sucessfully!");
                imageBox2.Image = new Emgu.CV.Image<Bgr, Byte>(EXfaces[0]);
                button3.Enabled = true;
                textBox1.Enabled = true;

            }
            else
                MessageBox.Show("NO faces Detected!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
            MessageBox.Show(ex.StackTrace.ToString());
        }
    }

这是我可以帮助你的代码