Additional information: OpenCV: Different sizes of objects using c#

杀马特。学长 韩版系。学妹 提交于 2019-12-11 03:00:58

问题


Currently, I am facing problem with my EmguCV c# code. I am trying to recognize my images from the database, but its not working. Once my face is detected, its crashing and then this error shows up

Additional information: OpenCV: Different sizes of objects.

I tried searching for this error but I am clueless.

This is my code:

//Action for each element detected
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);
   //draw the face detected in the 0th (gray) channel with blue color
   currentFrame.Draw(f.rect, new Bgr(Color.Green), 2);

   //Database select the image row and pass to the eigenobjectrecognizer

   //ConnectToDatabase();
   if (Connection.State.Equals(ConnectionState.Open))
   {
      Connection.Close();
      TSTable.Clear();
      ConnectToDatabase();
   }
   //Connection.Open();
   OleDbCommand OledbSelect = new OleDbCommand("Select FaceName, FaceImage From TrainingSet1",Connection);
   OleDbDataReader reader = OledbSelect.ExecuteReader();

   while (reader.Read())
   {
      labels.Add(reader.GetValue(1).ToString()); 
      trainingImages.Add(gray);
   }

   if (TSTable.Rows.Count != 0)
   {
      //TermCriteria for face recognition with numbers of trained images like maxIteration
      MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);

      //Eigen face recognizer
      EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
                trainingImages.ToArray(), //database faceimage list
                labels.ToArray(), //facename list
                3000,
                ref termCrit);

      name = recognizer.Recognize(result);

      //Draw the label for each face detected and recognized
      currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));

   }

I currently still new to EmguCV and C#. So some of the exceptions I don't understand. Can anyone help me with this.

And once the code breaks, it goes to the EigenObjectRecognizer.cs. This is the code where it breaks:

 public static float[] EigenDecomposite(Image<Gray, Byte> src, Image<Gray, Single>[] eigenImages, Image<Gray, Single> avg)
  {
     return CvInvoke.cvEigenDecomposite(
         src.Ptr,
         Array.ConvertAll<Image<Gray, Single>, IntPtr>(eigenImages, delegate(Image<Gray, Single> img) { return img.Ptr; }),
         avg.Ptr);
  }

回答1:


I guess, problem is in trainingImages. The width and height of each eigenImages must be the same as the width and height of the input image.

Your result image has size 100x100. So, all your trainingImage should have same size. Resize them, before adding to your list.

I review your code. Trouble still in sizes. Line 305 replace to :

result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(gray.Width, gray.Height, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);


来源:https://stackoverflow.com/questions/31918751/additional-information-opencv-different-sizes-of-objects-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!