无法在C#中转换类型的对象

时间:2022-09-02 12:01:54

I am trying to read my images from my MS Access database into my eigenobjectrecognizer. But now, I am getting an error which is

我试图将我的MS Access数据库中的图像读入我的eigenobjectrecognizer。但现在,我收到了一个错误

Unable to cast object of type 'System.Byte[]' to type 'Emgu.CV.Image`2[Emgu.CV.Structure.Gray,System.Byte]'.

无法将类型为'System.Byte []'的对象转换为'Emgu.CV.Image`2 [Emgu.CV.Structure.Gray,System.Byte]'。

I dont know what is this error is about. Below is my code.

我不知道这个错误是什么。以下是我的代码。

int count = reader.FieldCount;

while (reader.Read())
{
    labels.Add(reader["FaceName"].ToString());
    trainingImages.Add((Image<Gray,byte>)reader["FaceImage"]);
}   
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);

Can someone help me with this. I am trying this for more than a week already but still I cant get any solution. Thank you.

有人可以帮我弄这个吗。我已经尝试了一个多星期但是我仍然无法得到任何解决方案。谢谢。

2 个解决方案

#1


0  

If reader["FaceImage"] is the image file then you need to create a new image from the file:

如果reader [“FaceImage”]是图像文件,那么您需要从文件中创建一个新图像:

Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(reader["FaceImage"]);
trainingImages.Add(image);

#2


0  

The problem is this line.

问题是这一行。

(Image<Gray,byte>)reader["FaceImage"]

The problem is that reader["FaceImage"] is of type byte[] and Image<Gray,byte>

问题是reader [“FaceImage”]的类型为byte [],Image为Gray,byte>

  1. Is not a base class of byte[]
  2. 不是byte []的基类

  3. Does not have an explicit nor implicit conversion operator from byte[].
  4. 没有来自byte []的显式或隐式转换运算符。

What you want is

你想要的是什么

{
    labels.Add(reader["FaceName"].ToString());
    byte[] buffer = (byte[]) reader["FaceImage"];
    Image<Gray,byte> image = new Image<Gray,byte>(buffer);
    trainingImages.Add(image);
} 

#1


0  

If reader["FaceImage"] is the image file then you need to create a new image from the file:

如果reader [“FaceImage”]是图像文件,那么您需要从文件中创建一个新图像:

Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(reader["FaceImage"]);
trainingImages.Add(image);

#2


0  

The problem is this line.

问题是这一行。

(Image<Gray,byte>)reader["FaceImage"]

The problem is that reader["FaceImage"] is of type byte[] and Image<Gray,byte>

问题是reader [“FaceImage”]的类型为byte [],Image为Gray,byte>

  1. Is not a base class of byte[]
  2. 不是byte []的基类

  3. Does not have an explicit nor implicit conversion operator from byte[].
  4. 没有来自byte []的显式或隐式转换运算符。

What you want is

你想要的是什么

{
    labels.Add(reader["FaceName"].ToString());
    byte[] buffer = (byte[]) reader["FaceImage"];
    Image<Gray,byte> image = new Image<Gray,byte>(buffer);
    trainingImages.Add(image);
}