简体   繁体   English

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

[英]Unable to cast object of type in C#

I am trying to read my images from my MS Access database into my eigenobjectrecognizer. 我正在尝试将图像从MS Access数据库读取到我的eigenobjectrecognizer中。 But now, I am getting an error which is 但是现在,我收到一个错误

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. 谢谢。

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[] 不是byte[]的基类
  2. Does not have an explicit nor implicit conversion operator from byte[] . 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);
} 

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM