我已经训练了FaceRecognizer,所有图像都具有相同的大小,并将灰度图像传递给预测器。但我面对这个错误
OpenCV错误:参数不正确(给定矩阵的形状错误。尺寸(src)=(1,150544),尺寸(W)=(37636,5)。)in subspaceProject,file / home / * ******* / opencv / modules / core / src / lda.cpp,第182行
这是代码
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String trainingDir = "res/";
File root = new File(trainingDir);
FilenameFilter imgFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
name = name.toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".pgm") || name.endsWith(".png");
}
};
File[] imageFiles = root.listFiles(imgFilter);
ArrayList <Mat> images = new ArrayList(imageFiles.length);
Mat labels = new Mat(imageFiles.length,1,CvType.CV_32SC1);
int[][] labelsBuffer = new int[imageFiles.length][imageFiles.length];
int counter =0;
for(File image: imageFiles){
Mat img = Imgcodecs.imread(image.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
int label = Integer.parseInt(image.getName().split("\\.")[2]);
//images.add(label, img);
images.add(img);
counter++;
}
FaceRecognizer faceRecognizer = Face.createEigenFaceRecognizer();
Mat temp = new Mat();
faceRecognizer.train(images, labels);
CascadeClassifier cascadeFaceClassifier = new CascadeClassifier(
"haarcascade_frontalface_default.xml");
VideoCapture videoDevice = new VideoCapture();
videoDevice.open(0);
int x = 0;
if (videoDevice.isOpened()) {
while (true) {
Mat frameCapture = new Mat();
videoDevice.read(frameCapture);
MatOfRect faces = new MatOfRect();
cascadeFaceClassifier.detectMultiScale(frameCapture, faces, 2.0, 5, 0,new Size(),new Size());
for (Rect rect : faces.toArray()) {
Mat resizedCapture = new Mat(frameCapture,rect);
Imgproc.cvtColor(resizedCapture, resizedCapture, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Size imageSize = new Size(194,194);
Imgproc.resize(resizedCapture,resizedCapture , imageSize);
System.out.println(images.get(0).rows());
System.out.println(resizedCapture.rows());
System.out.println(images.get(0).cols());
System.out.println(resizedCapture.cols());
try{
x = faceRecognizer.predict_label(resizedCapture);
}catch(Exception e){
System.out.println(e.getMessage());
}
x++;
Imgproc.putText(frameCapture, "Face"+x, new Point(rect.x,rect.y-5), 1, 2, new Scalar(0,0,255));
Imgproc.rectangle(frameCapture, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),new Scalar(0, 100, 0),3);
}
PushImage(ConvertMat2Image(frameCapture));
//System.out.println(String.format("FACES = %s EYES = %s detected.", faces.toArray().length,eyes.toArray().length,nose.toArray().length));
}
} else {
System.out.println("Video Device Not Working");
return;
}
}
private static BufferedImage ConvertMat2Image(Mat matImage) {
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", matImage, matOfByte);
byte[] byteArray = matOfByte.toArray();
BufferedImage img = null;
try {
InputStream in = new ByteArrayInputStream(byteArray);
img = ImageIO.read(in);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return img;
}
public static void PencereHazirla() {
frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(700, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void PushImage(Image img2) {
if (frame == null)
PencereHazirla();
if (lbl != null)
frame.remove(lbl);
icon = new ImageIcon(img2);
lbl = new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.revalidate();
}
}
答案 0 :(得分:0)
我正在尝试Image.cvtColor并使用Imagecodecs转换为灰度增加4倍。 对此的解决方案是
Imgproc.cvtColor(resizedCapture, resizedCapture, Imgproc.COLOR_BGR2GRAY);