InputStream to Mat type and then templatematching, works with "jpg" but not with "png"

466 views
Skip to first unread message

damian....@gmail.com

unread,
Oct 30, 2014, 1:06:56 PM10/30/14
to jav...@googlegroups.com
Hi there I would like to grab 2 images from my mysql database, one source image and one template image and then do template matching. When the source file on database is jpg below methods work but when it is png they dont work. The weird thing is it works then the template file is png, but when source is png it doesnt work. On the other hand at my local computer, when I dont use inputstream, no matter which one is png/jpg it always works.

//Here is how I get the images:

            stmt = conn.createStatement();
            String query =  "select is, 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);
             
            }           

// Here is on MatchingDemo() class the conversion to 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:


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)


I would be glad if you can give a hand,

Best regards,
Damian

damian....@gmail.com

unread,
Oct 31, 2014, 7:02:38 AM10/31/14
to jav...@googlegroups.com
Changing

Highgui.imdecode(new MatOfByte(temporaryImageInMemory), -1);

to

Highgui.imdecode(new MatOfByte(temporaryImageInMemory), 1);

So instead of -1 , 1, the file is taken with CV_LOAD_IMAGE_COLOR instead of CV_LOAD_IMAGE_UNCHANGED the color mode works, now I will search the reason behind.
Reply all
Reply to author
Forward
0 new messages