Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8602263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:02:34+00:00 2026-06-12T02:02:34+00:00

I started working with EmguCV, but I stumble upon a performance issue trying to

  • 0

I started working with EmguCV, but I stumble upon a performance issue trying to convert from the grabber’s bitmap to a Texture2D

This is my code

    private void FrameGrabber()
    {
        NamePersons.Add("");
        currentFrame = grabber.QueryFrame().Resize(320, 240, INTER.CV_INTER_CUBIC);
        gray = currentFrame.Convert<Gray, Byte>();
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
        foreach (MCvAvgComp f in facesDetected[0])
        {
            t = t + 1;
            result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
            currentFrame.Draw(f.rect, new Bgr(System.Drawing.Color.Red), 2);
            if (trainingImages.ToArray().Length != 0)
            {
                MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
                EigenObjectRecognizer recognizer = new EigenObjectRecognizer(trainingImages.ToArray(), labels.ToArray(), 2500, ref termCrit);
                name = recognizer.Recognize(result);
                currentFrame.Draw(name, ref font, new System.Drawing.Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(System.Drawing.Color.LightGreen));
            }
            NamePersons[t - 1] = name;
            NamePersons.Add("");
        }
        t = 0;
        for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
            names = names + NamePersons[nnn] + ", ";
        names = "";
        NamePersons.Clear();

        Bitmap b = currentFrame.ToBitmap();

        //slow
        using (MemoryStream s = new MemoryStream())
        {
            b.Save(s, System.Drawing.Imaging.ImageFormat.Png);
            s.Seek(0, SeekOrigin.Begin); //must do this, or error is thrown in next line
            frame = Texture2D.FromStream(GraphicsDevice, s);
        }

        ////second option but image is bluish and still slow
        //GraphicsDevice.Textures[0] = null;
        //if (frame == null || b.Width != frame.Width || b.Height != frame.Height)
        //    frame = new Texture2D(GraphicsDevice, b.Width, b.Height);
        //BitmapData bData = b.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(), b.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
        //int byteCount = bData.Stride * b.Height;
        //byte[] bmpBytes = new byte[byteCount];
        //Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);
        //b.UnlockBits(bData);
        //frame.SetData(bmpBytes);
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        grabber = new Capture();
    }

    protected override void UnloadContent()
    {
        grabber.Dispose();
    }

    protected override void Update(GameTime gameTime)
    {
        FrameGrabber();            
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.LightGray);
        spriteBatch.Begin();
        if (frame !=null)
            spriteBatch.Draw(frame, new Microsoft.Xna.Framework.Rectangle(0, 0, ScreenWidth, ScreenHeight), Microsoft.Xna.Framework.Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }

I searched the web for documentation but with no luck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:02:35+00:00Added an answer on June 12, 2026 at 2:02 am

    Well I solved my problem creating a class that start a parallel thread and handle the frame conversion coming from the webcam.
    If anyone is interested, to convert to texture and also handle the face recognition I used this:

        private Image<Gray, byte> gray = null;
        private HaarCascade haarCascade = new HaarCascade("haarcascade_frontalface_default.xml");
    
        private void QueryFrame()
        {
            while (is_running)
            {
                nextFrame = capture.QueryFrame().Flip(FLIP.HORIZONTAL);
                if (nextFrame != null)
                {
                    gray = nextFrame.Convert<Gray, Byte>();
                    MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(haarCascade, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new System.Drawing.Size(20, 20));
                    foreach (MCvAvgComp face in facesDetected[0])
                        nextFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Red), 2);
                    byte[] bgrData = nextFrame.Bytes;
                    for (int i = 0; i < colorData.Length; i++)
                        colorData[i] = new Color(bgrData[3 * i + 2], bgrData[3 * i + 1], bgrData[3 * i]);
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently started working with the scrapy library. I am trying to scrape from a
Started working on a new application this week that is running the latest rails
I started working on a project which uses Net_URL_Mapper from PEAR (php) as the
I've just started working with Propel and I love it, but I have a
I started working on Android applications and I find little difficult to understand this
I started working on yii framework in last 2-3 days.... i am trying to
I started working on a web application. This application needs lot of image handling.
I just started working with jqGrid this week. I'm attempting to make a generic
I've started working on Play almost a week ago, what I'm trying to do
I started working on mysql c++ connector API. But I can't find any online

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.