简体   繁体   English

用于模板匹配的OpenCV阅读图像

[英]OpenCV Reading Image for Template Matching

Im using OpenCV in java, developing with eclipse and the images are read this way for template matching. 我在Java中使用OpenCV,使用eclipse进行开发,并且以这种方式读取图像以进行模板匹配。

String inFile = "C:/image.png";
Mat img = Highgui.imread(inFile);

This is nice but my images are not in my local computer. 很好,但是我的图像不在本地计算机中。 I should compare 2000 images on a server in mysql database. 我应该比较mysql数据库中服务器上的2000个映像。 Saving 2000 images to my computer and then reading them does not make sense. 将2000张图像保存到我的计算机,然后读取它们没有任何意义。

So what I need is that Highgui.imread(inFile) reads an image: Highgui.imread(Image inImage) or maybe Highgui.imread(File inFile) , I couldnt find the java source to edit/modify. 所以我需要的是Highgui.imread(inFile)读取图像:Highgui.imread(Image inImage)或Highgui.imread(File inFile),我找不到要编辑/修改的Java源代码。 I need someway to convert my images which will be coming from the mysql DataBase to Mat type for comparison(template matching) ... 我需要某种方式将来自mysql数据库的图像转换为Mat类型以进行比较(模板匹配)...

More info: I have a mysql table with columns: ID, Name, Description, Image1, Image2. 更多信息:我有一个带有以下列的mysql表:ID,名称,描述,Image1,Image2。 The type of Image1 and 2 columns are "mediumblob" and when you right click to the column Image1 or Image2 in "MySQL Workbench" I choose "Open Value in Editor" then there are 3 tabs; Image1和2列的类型为“ mediumblob”,当右键单击“ MySQL Workbench”中的Image1或Image2列时,我选择“在编辑器中打开值”,然后有3个标签; Binary, Text, Image, at Image my image is displayed perfectly. 二进制,文本,图像,在图像处我的图像完美显示。 The type of Images are png. 图片类型为png。 In the end I want to Compare Image1 with Image2. 最后,我想比较Image1和Image2。 Image2 is inFile and Image1 is templateFile. Image2是inFile,Image1是templateFile。

I'm sorry I thought my question was clear. 对不起,我以为我的问题很清楚。 I will try to rephrase it. 我将尝试改写它。 I have a local computer and a server. 我有一台本地计算机和一台服务器。 The images are stored in the mysql database on server. 图像存储在服务器上的mysql数据库中。 I want to run an application from my local computer that retrieves/accesses 2 images on the server and then compares them via OpenCV template matching. 我想从本地计算机运行一个应用程序,该应用程序检索/访问服务器上的2张图像,然后通过OpenCV模板匹配比较它们。 Afaik you can't access a column of mysql with /server/home/user/mysql/image_table/mypicture.png , I'm writing this because of "guneykayim" 's suggestion. Afaik您无法使用/server/home/user/mysql/image_table/mypicture.png访问mysql的列,由于“ guneykayim”的建议,我正在写这篇文章。 So how Im planning to retrieve the images, havent tried yet but the plan is : 所以我打算如何检索图像,还没有尝试过,但是计划是:

Blob imageBlob = resultSet.getBlob(yourBlobColumnIndex);
InputStream binaryStream = imageBlob.getBinaryStream(0, imageBlob.length());

Or 要么

InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex);

Or 要么

try{
    Class.forName(driverName);
    con = DriverManager.getConnection(url+dbName,userName,password);
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select image from image");
    int i = 0;
    while (rs.next()) {
        InputStream in = rs.getBinaryStream(1);
        OutputStream f = new FileOutputStream(new File("test"+i+".jpg"));
        i++;
        int c = 0;
        while ((c = in.read()) > -1) {
            f.write(c);
        }
        f.close();
        in.close();
    }
}catch(Exception ex){
    System.out.println(ex.getMessage());
}

My Question: I want to compare 2 images, I retrieved from the mysql database. 我的问题:我想比较从mysql数据库检索到的2张图像。

Highgui.imread(); 

This method expects a String but I want to give the InputStream or the OutputStream file... Like I said I don't want to save 2000 images on my local computer. 此方法需要一个String,但是我想提供InputStream或OutputStream文件...就像我说的那样,我不想在本地计算机上保存2000个图像。 I'm open for alternative ways as well. 我也欢迎其他方式。

Final Simplest Rephrasing : How can I use template matching of java OpenCV on 2 images stored on a mysql database. 最终最简单的重述 :如何在存储在mysql数据库中的2张图像上使用Java OpenCV的模板匹配。

In other words how can I convert InputStream object to Mat object? 换句话说,如何将InputStream对象转换为Mat对象?

Thanks for reading. 谢谢阅读。

我在这里找到了对这个家伙的荣誉,他是我的英雄:)完美工作,如果您想要原始颜色而不是灰度的图像,则应将Highgui.IMREAD_GRAYSCALE更改为Highgui.IMREAD_UNCHANGED以获取更多信息, 请检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM