Ask Your Question
2

Java API : loading image from any Java InputStream

asked 2014-04-16 11:57:12 -0600

Baccata gravatar image

updated 2014-04-17 03:11:20 -0600

berak gravatar image

Hello,

is there a way, using the Java bindings to OpenCV, to load an image from an InputStream, knowing only the format of the image (tiff, in my example). I'd like to load images from the Web or from other potential sources (hdfs) ?

Thank you

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2014-04-16 20:52:37 -0600

cy937956803 gravatar image

Highgui.imencode(ext, img, buf); maybe what you want

edit flag offensive delete link more

Comments

1

Okay here's what to do (in Scala. It's the same in Java) (cannot answer my own question cause I'm a newbie on this forum)

val inputStream : InputStream = ... val buf : Array[Byte] = ... //dump the content of the stream in this array val matBuf = new MatOfByte() matBuf.fromArray(buf: _*) //trick to use a scala array as a java varargs val mat = Highgui.imdecode(matBuf, Highgui.CV_LOAD_IMAGE_UNCHANGED) //tadaaaaaaaa

Baccata gravatar imageBaccata ( 2014-04-17 04:08:55 -0600 )edit
2

answered 2014-08-17 14:01:03 -0600

Here is the helper-method that I wrote to actually perform this task:

private static Mat readInputStreamIntoMat(InputStream inputStream) throws IOException {
    // Read into byte-array
    byte[] temporaryImageInMemory = readStream(inputStream);

    // Decode into mat. Use any IMREAD_ option that describes your image appropriately
    Mat outputImage = Highgui.imdecode(new MatOfByte(temporaryImageInMemory), Highgui.IMREAD_GRAYSCALE);

    return outputImage;
}

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[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;
}
edit flag offensive delete link more

Comments

You are my hero, I was looking for this for 2 days...

Finnish gravatar imageFinnish ( 2014-10-23 11:23:59 -0600 )edit

@AlexanderPacha Today I realized it wasnt fully working. When the inFile(Source) is a jpg on Mysql 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 11:05:09 -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); I would be glad if you can help.

Finnish gravatar imageFinnish ( 2014-10-30 11:05:43 -0600 )edit

Make sure to use the correct format for imdecode(). The assertion fails, if the provided data did not match the image-format that was expected (= specified).

Alexander Pacha gravatar imageAlexander Pacha ( 2014-11-25 06:06:50 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-04-16 11:57:12 -0600

Seen: 7,857 times

Last updated: Aug 17 '14