我正在创建一个面部识别应用程序,我发现了一个名为MultiFaceRec的示例,但这是搜索以bmp格式编号的文件夹中保存的图像以及保存的txt文件中的人物名称。按顺序加上图片编号。
#region Searching for txt file
//Load of previus trainned faces and labels for each image
string Labelsinfo = File.ReadAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt");
string[] Labels = Labelsinfo.Split('%');
NumLabels = Convert.ToInt16(Labels[0]);
ContTrain = NumLabels;
string LoadFaces;
for (int tf = 1; tf < NumLabels + 1; tf++)
{
LoadFaces = "face" + tf + ".bmp";
trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/TrainedFaces/" + LoadFaces));
labels.Add(Labels[tf]);
}
#endregion
我试图通过直接查看数据库来更改此设置,在该数据库中,我有一个图像字段和一个带有人名的字段。
SqlConnection conConexao = clsdb.AbreBanco();
SqlCommand cmd = new SqlCommand("select fotografia, nome from pessoa where fotografia is not null", conConexao);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
{
byte[] imageSource = (byte[])dr["fotografia"];
Bitmap image;
using (MemoryStream stream = new MemoryStream(imageSource))
{
image = new Bitmap(stream);
}
trainingImages.Add(new Image<Gray, byte>(image));
labels.Add(dr["Nome"].ToString());
}
}
但是在这种方法中我遇到了一个错误:
#region static methods
/// <summary>
/// Caculate the eigen images for the specific traning image
/// </summary>
/// <param name="trainingImages">The images used for training </param>
/// <param name="termCrit">The criteria for tranning</param>
/// <param name="eigenImages">The resulting eigen images</param>
/// <param name="avg">The resulting average image</param>
public static void CalcEigenObjects(Image<Gray, Byte>[] trainingImages, ref MCvTermCriteria termCrit, out Image<Gray, Single>[] eigenImages, out Image<Gray, Single> avg)
{
int width = trainingImages[0].Width;
int height = trainingImages[0].Height;
IntPtr[] inObjs = Array.ConvertAll<Image<Gray, Byte>, IntPtr>(trainingImages, delegate(Image<Gray, Byte> img) { return img.Ptr; });
if (termCrit.max_iter <= 0 || termCrit.max_iter > trainingImages.Length)
termCrit.max_iter = trainingImages.Length;
int maxEigenObjs = termCrit.max_iter;
#region initialize eigen images
eigenImages = new Image<Gray, float>[maxEigenObjs];
for (int i = 0; i < eigenImages.Length; i++)
eigenImages[i] = new Image<Gray, float>(width, height);
IntPtr[] eigObjs = Array.ConvertAll<Image<Gray, Single>, IntPtr>(eigenImages, delegate(Image<Gray, Single> img) { return img.Ptr; });
#endregion
avg = new Image<Gray, Single>(width, height);
CvInvoke.cvCalcEigenObjects(
inObjs,
ref termCrit,
eigObjs,
null,
avg.Ptr);
}
错误是: Emgu.CV.Util.CvException:'OpenCV:不同大小的对象'
在数据库中搜索图像时如何解决?