OpenCv 3.1 中给定矩阵的错误形状 - Java
Posted
技术标签:
【中文标题】OpenCv 3.1 中给定矩阵的错误形状 - Java【英文标题】:Wrong Shapes for Given Matrices in OpenCv 3.1 - Java 【发布时间】:2017-05-19 09:56:23 【问题描述】:我已经训练了 FaceRecognizer,所有图像的大小都相同,并将灰度图像传递给预测器。但我面临这个错误
OpenCV 错误:错误参数(给定矩阵的形状错误。大小(src)=(1,150544),大小(W)=(37636,5)。)在 subspaceProject,文件 /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();
【问题讨论】:
请不要发布整个代码,否则会导致没有人会阅读您的问题。 @BahramdunAdil 谢谢你的建议。但问题是我认为这种问题必须与整个代码一起发布。这只是我正在谈论的问题的代码 @Hasnain 您需要创建一个演示问题的最小代码示例,首先一遍又一遍地删除无关代码,直到您拥有仅包含复制问题所需的代码的内容。见:How to create a Minimal, Complete, and Verifiable example @MarkOmo 非常感谢你,下次我会照顾它的。 【参考方案1】:我正在尝试 Image.cvtColor 并使用 Imagecodecs 转换为灰度增加了 4 倍。 解决方法是
Imgproc.cvtColor(resizedCapture, resizedCapture, Imgproc.COLOR_BGR2GRAY);
Answer Given on opencv community
【讨论】:
以上是关于OpenCv 3.1 中给定矩阵的错误形状 - Java的主要内容,如果未能解决你的问题,请参考以下文章