HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

How do I make this C# code more efficient, in terms of FPS and time complexity?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thismakemoreefficientandtimefpshowcodeterms

Problem

```
Image frame = _capture.QueryFrame();
Image grayFrame = frame.Convert();
grayFrame._EqualizeHist();
MCvAvgComp[][] facesDetected = grayFrame.DetectHaarCascade(_faces, 1.1, 1,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.FIND_BIGGEST_OBJECT, new Size(20, 20));

if (facesDetected[0].Length == 1)
{
MCvAvgComp face = facesDetected[0][0];

#region Search ROI based on Face Metric Estimation -

Int32 yCoordStartSearchEyes = face.rect.Top + (face.rect.Height * 3 / 11);
Point startingPointSearchEyes = new Point(face.rect.X, yCoordStartSearchEyes);
Size searchEyesAreaSize = new Size(face.rect.Width, (face.rect.Height * 3 / 11));
Rectangle possibleROI_eyes = new Rectangle(startingPointSearchEyes, searchEyesAreaSize);

#endregion

int widthNav = (frame.Width / 10 * 2);
int heightNav = (frame.Height / 10 * 2);
Rectangle nav = new Rectangle(new Point(frame.Width / 2 - widthNav / 2, frame.Height / 2 - heightNav / 2),
new Size(widthNav, heightNav));
frame.Draw(nav, new Bgr(Color.Lavender), 3);
Point cursor = new Point(face.rect.X + searchEyesAreaSize.Width / 2,
yCoordStartSearchEyes + searchEyesAreaSize.Height / 2);

grayFrame.ROI = possibleROI_eyes;
MCvAvgComp[][] eyesDetected = grayFrame.DetectHaarCascade(_eyes, 1.15, 3,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
grayFrame.ROI = Rectangle.Empty;

if (eyesDetected[0].Length != 0)
{
frame.Draw(face.rect, new Bgr(Color.Yellow), 1);

foreach (MCvAvgComp eye in eyesDetected[0])
{
Rectangle eyeRect = eye.rect;
eyeRect.Offset(possibleROI_eyes.X, possibleROI_eyes.Y);
grayFrame.ROI = eyeRect;
frame.Draw(eyeRect, new Bgr(Color.DarkSeaGreen), 2);

frame.Draw(possibleROI_eyes, new Bgr(Color.DeepPink), 2);

if (nav.Left < cursor.X && cursor.X < (nav.Left + nav.Width)
&& nav.Top < cursor.Y && cursor.Y < nav.Top +

Solution

I think your code is already quite good. My guess is that the calls to DetectHaarCascade are the majority of the time. Can you run a profiler on it? The Equatec profiler is free. As for the lower loop you could preallocate your colors (although I assume they're structs). Also, I don't see where possibleROI_eyes changes, so do you really need to draw that every time through the loop? Or is it drawing at the cursor position?

Context

StackExchange Code Review Q#8580, answer score: 7

Revisions (0)

No revisions yet.