Java OpenCV InputStream到Mat类型,然后模板匹配,使用;jpg";但不包括;巴布亚新几内亚“;
我想从我的mysql数据库中抓取两个图像,一个源图像和一个模板图像,然后进行模板匹配。当数据库上的源文件是jpg时,下面的方法工作,但当它是png时,它们不工作。奇怪的是,当模板文件是png时它可以工作,但当源文件是png时它就不工作了。另一方面,在我的本地计算机上,当我不使用inputstream时,无论哪一个是png/jpg,它总是工作的 //以下是我获取图像的方式:Java OpenCV InputStream到Mat类型,然后模板匹配,使用;jpg";但不包括;巴布亚新几内亚“;,java,mysql,opencv,image-processing,inputstream,Java,Mysql,Opencv,Image Processing,Inputstream,我想从我的mysql数据库中抓取两个图像,一个源图像和一个模板图像,然后进行模板匹配。当数据库上的源文件是jpg时,下面的方法工作,但当它是png时,它们不工作。奇怪的是,当模板文件是png时它可以工作,但当源文件是png时它就不工作了。另一方面,在我的本地计算机上,当我不使用inputstream时,无论哪一个是png/jpg,它总是工作的 //以下是我获取图像的方式: stmt = conn.createStatement(); String query =
stmt = conn.createStatement();
String query = "select id, template_pic, source_pic from my_table";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
int ID_pics = rs.getInt(1);
//picture Code template matching
InputStream temp_file = rs.getBinaryStream(2);
InputStream inFile = rs.getBinaryStream(3);
new MatchingDemo().run(inFile, temp_file, "Out_image"+ ID_pics +".png", Imgproc.TM_CCOEFF_NORMED);
Thread.sleep(5000);
}
//下面是MatchingDemo()类到Mat的转换:
private static byte[] readStream(InputStream stream) throws IOException {
// Copy content of the image to byte-array
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[524288]; // 16384
while ((nRead = stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] temporaryImageInMemory = buffer.toByteArray();
buffer.close();
stream.close();
return temporaryImageInMemory;
}
byte[] temporaryImageInMemory = readStream(inFile);
byte[] temporaryImageInMemory_temp = readStream(templateFile);
Mat img = Highgui.imdecode(new MatOfByte(temporaryImageInMemory), -1);
Mat templ = Highgui.imdecode(new MatOfByte(temporaryImageInMemory_temp), -1);
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method); // This line is where I get the error and the error message is as below:
//我从Eclipse获得的错误消息
OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in cv::matchTemplate, file ..\..\..\..\opencv\modules\imgproc\src\templmatch.cpp, line 249
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\imgproc\src\templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function cv::matchTemplate
]
at org.opencv.imgproc.Imgproc.matchTemplate_0(Native Method)
at org.opencv.imgproc.Imgproc.matchTemplate(Imgproc.java:7621)
at MatchingDemo.run(MatchingDemo.java:104)
如果您能帮我一把,我会很高兴。我之前说过相反的话,但现在请尝试imdecode(文件名,1)以消除可能的alphachannel@berak哇,把“-1”改成“1”行得通,我原以为保留原始文件会更好,但从没想过他的。。。更改减号使所有程序都工作了,难以置信:)非常感谢!如果你愿意的话,你可以把它作为一个带有解释的答案贴出来,我很乐意把它标记为一个答案!