Ask Your Question
0

Reading Image for Template Matching for Java

asked 2014-10-22 10:31:16 -0600

Finnish gravatar image

updated 2014-10-23 07:42:15 -0600

Im using OpenCV in java and the images are read this way for template matching.

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. Saving 2000 images to my computer and then reading them does not make sense.

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. I need someway to convert my images which will be coming from the DB to Mat type for comparison(template matching) ...

More info: I have a mysql table: ID, Name, Description, 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; Binary, Text, Image, at Image my image is displayed perfectly. The type of Images are png. In the end I want to Compare Image1 with Image2. Image2 is inFile and Image1 is 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. 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. 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.

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. 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.

Thanks for reading.

edit retag flag offensive close merge delete

Comments

I'm sorry, but I don't understand what you want. In which format do you have your images? Do you want to read them from a binary format from the db?

FooBar gravatar imageFooBar ( 2014-10-22 10:47:34 -0600 )edit

imdecode) at least lets you read an image from memory

berak gravatar imageberak ( 2014-10-22 10:53:37 -0600 )edit

@FooBar I have a mysql table: ID, Name, Description, 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, Binary, Text, Image, at Image my image is displayed perfectly. The type of Images are png. In the end I want to Compare Image1 with Image2. Image2 is inFile and Image1 is templateFile.

Finnish gravatar imageFinnish ( 2014-10-22 11:10:38 -0600 )edit

could you add, in what format you get your images from the db ? like is it a byte[] ? from your description we already know, that it's a 'complete' image ( like on disk), so the missing link is, how to feed it to imdecode again, to get a Mat.

berak gravatar imageberak ( 2014-10-23 04:58:54 -0600 )edit
1

@berak I updated the question, but imdecode() is expecting a Mat file which I dont have...

Finnish gravatar imageFinnish ( 2014-10-23 07:44:58 -0600 )edit

untested, so not an answer (yet):

 InputStream in = rs.getBinaryStream(1);
 byte[] somebytes = new byte[???];
 in.read(somebytes);

 MatOfByte mb = new MatOfByte();
 mb.put(0,0,somebytes);
 Mat image = imdecode(mb);
berak gravatar imageberak ( 2014-10-23 07:56:11 -0600 )edit

@berak imdecode() requires a flag as well, and when I go to imdecode() explanation it says the same flags as imread, but imread() does not have flags... So what integer value shall I give? I will also check the cpp equivalents...

Finnish gravatar imageFinnish ( 2014-10-23 09:28:01 -0600 )edit

here's the flags the default is 1 (force to bgr), but you probably want -1 (leave as is)

berak gravatar imageberak ( 2014-10-23 09:35:58 -0600 )edit
Finnish gravatar imageFinnish ( 2014-10-23 09:41:48 -0600 )edit

well, one of it. i'd still propose CV_LOAD_IMAGE_UNCHANGED (you'd want to preserve alpha in a png, if present)

berak gravatar imageberak ( 2014-10-23 09:50:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-10-24 11:23:59 -0600

Finnish gravatar image

I found the answer here kudos to this guy, he is my hero :) Works perfectly, if you want the images in original color and not in grayscale you should change Highgui.IMREAD_GRAYSCALE with Highgui.IMREAD_UNCHANGED for more info check

edit flag offensive delete link more

Comments

honestly, i kinda hoped, you would make a real answer here, not just a link to some SO post.

berak gravatar imageberak ( 2014-10-24 12:36:34 -0600 )edit
1

@break Im sorry if you got disappointed but in Stackoverflow when you copy someone else's answer and then post it as yours, you get a BIG "boO", I wouldnt like to take credits for what someone else did. What I did was just copy paste his code and then it worked, I haven't fixed or modified it. Today I realized it wasnt fully working. When the inFile(Source) is a jpg on Database it works but when it is a png on Database I get the error below (Not enough space). PS: On my local computer both jpg and png work :( without the inputStream stuff.

Finnish gravatar imageFinnish ( 2014-10-30 10:59:34 -0600 )edit

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) On line 104 : Imgproc.matchTemplate(img, templ, result, match_method);

Finnish gravatar imageFinnish ( 2014-10-30 11:01:12 -0600 )edit

@berak Sorry wrote the @ wrongly and cant edit the comment now :S

Finnish gravatar imageFinnish ( 2014-10-30 11:17:33 -0600 )edit

no worries, you tried, it's ok. (my favourite typo, btw ;) )

berak gravatar imageberak ( 2014-10-30 11:20:46 -0600 )edit

@berak do you have any suggestions for the png problem? Or maybe the problem is not png something else... but works when the source is jpg in DB and works when the source is png/jpg at local computer!

Finnish gravatar imageFinnish ( 2014-10-30 11:25:16 -0600 )edit

@berak may I draw your attention to this question please?

Finnish gravatar imageFinnish ( 2015-05-04 10:24:31 -0600 )edit

Question Tools

Stats

Asked: 2014-10-22 10:31:16 -0600

Seen: 2,535 times

Last updated: Oct 24 '14