我正在开发一个使用EmguCv和c#进行人脸识别的简单应用程序。我正在使用ms访问数据来存储样本数据库。每当编译代码时我都会收到错误 “OpenCV:不同大小的对象” 这是我的代码....感谢ADVANCE。
public partial class Facerecognition : Form
{
EigenObjectRecognizer Recognizer;
private Capture cam;
Image<Bgr, Byte> frame;
Image<Gray, Byte> frame2;
private HaarCascade Haar;
MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
OleDbConnection Connection = new OleDbConnection();
OleDbDataAdapter Adapter;
DataTable Table = new DataTable();
List<string> FaceName = new List<string>();
List<Image<Gray, byte>> FaceImg = new List<Image<Gray, byte>>();
int ConTrain;
string name;
Image result;
public Facerecognition()
{
InitializeComponent();
Haar = new HaarCascade("haarcascade_frontalface_default.xml");
ConnectDatabase();
}
private void button3_Click(object sender, EventArgs e)
{
Traningset t = new Traningset();
t.Show();
}
public void Process(object sender, EventArgs e)
{
frame = cam.QueryFrame();
LoadRecognizer();
pictureBox1.Image = frame.ToBitmap();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void button2_Click(object sender, EventArgs e)
{
cam = new Capture();
Application.Idle += Process;
}
/*public void FaceDetection()
{
frame2 = frame.Convert<Gray, Byte>();
var faces = frame.DetectHaarCascade(Haar, 1.2, 3, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0];
foreach (var face in faces)
{
frame.Draw(face.rect, new Bgr(Color.Black), 3);
// MessageBox.Show("Face detected " + faces.Length.ToString());
}
}*/
public void ConnectDatabase()
{
Connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=facedatabase.mdb.mdb";
Adapter = new OleDbDataAdapter("Select facename, faceimg from traningset", Connection);
OleDbCommandBuilder CommandBuilder = new OleDbCommandBuilder(Adapter);
Adapter.Fill(Table);
Connection.Open();
if (Table.Rows.Count != 0)
{
int numofrows = Table.Rows.Count;
}
Connection.Close();
}
public Image GetFaceFromDB()
{
Image Fetehedimg;
byte[] fetechedimgbytes = (byte[])Table.Rows[0]["faceimg"];
MemoryStream stream = new MemoryStream(fetechedimgbytes);
Fetehedimg = Image.FromStream(stream);
return Fetehedimg;
}
public void LoadRecognizer()
{
/////////////////////////////////////////////// FACE DETECTER /////////////////////////////////////////////////////
frame2 = frame.Convert<Gray, Byte>();
var faces = frame.DetectHaarCascade(Haar, 1.2, 3, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0];
foreach (var face in faces)
{
frame.Draw(face.rect, new Bgr(Color.Black), 3);
// MessageBox.Show("Face detected " + faces.Length.ToString());
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ConnectDatabase();
Bitmap bitimage;
Image img;
img = new Bitmap(100, 100);
int numofrows=0;
if(Table.Rows.Count!=0)
{
numofrows=Table.Rows.Count;
}
for (int i = 0; i < numofrows; i++)
{
byte[] fetchedBytes = (byte[])Table.Rows[i]["faceimg"];
MemoryStream stream = new MemoryStream(fetchedBytes);
bitimage = new Bitmap(stream);
//bitimage = bitimage.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
FaceImg.Add(new Emgu.CV.Image<Gray, Byte>(bitimage));
String faceName = (String)Table.Rows[i]["facename"];
FaceName.Add(faceName);
}
if (FaceImg.ToArray().Length != 0)
{
MCvTermCriteria termCrit = new MCvTermCriteria(4, 0.001);
Recognizer = new EigenObjectRecognizer(
FaceImg.ToArray(),
FaceName.ToArray(),
3000,
ref termCrit);
MessageBox.Show("face recognized");
// frame.Draw(name, ref font, new Point(faces.rect.X - 2, faces.rect.Y - 2), new Bgr(Color.LightGreen));
}
}
}
}
答案 0 :(得分:0)
取消注释Resize
:
for (int i = 0; i < numofrows; i++)
{
byte[] fetchedBytes = (byte[])Table.Rows[i]["faceimg"];
MemoryStream stream = new MemoryStream(fetchedBytes);
bitimage = new Bitmap(stream);
Image<Gray, byte> img = new Emgu.CV.Image<Gray, Byte>(bitimage )
Image<Gray, byte> resizedImage = img.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
FaceImg.Add(resizedImage);
String faceName = (String)Table.Rows[i]["facename"];
FaceName.Add(faceName);
}
您无法在Resize
中执行此操作,这就是为什么未注释的行会给您带来另一个错误:
// not allowed
bitimage = bitimage.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);