Loading png image always result in an empty Matrix

asked 2017-06-23 07:20:21 -0500

evilkid gravatar image

I am trying to load a simple png image with opencv (3.2.0) with java, using the x64 library. But each time i get an empty matrix (all the pixels are 0). However, when i convert the image to jpg, it works just fine. I have tried to run imread method with every flag possible, I even did this (iterated over every possible flag f):

for (int k = 0; k < 130; f++) {
    Mat img = Imgcodecs.imread(inFile, f);
    for (int i = 0; i < img.cols(); i++) {
        for (int j = 0; j < img.rows(); j++) {
            if (img.get(i, j) != null && img.get(i, j)[0] > 0) {

Here is the image I'm running imread against. enter image description here

edit retag flag offensive close merge delete


unrelated, but , please never try to loop over pixels thatway. use Mat.dump() instead.

you also got rows and cols in reverse in get() , so for sure you should not trust any ouptut of that.

what does System.out.println(img) print ?

why the silly loop over k ? the only flags suppported are -1, 0 and 1.

all and all, i'm suspecting, that each hapless attempt at debugging it, gets you farther away from the truth, not closer.

berak gravatar imageberak ( 2017-06-23 07:30:52 -0500 )edit

ok ill make sure to use dump in the future, but all i sout(img) gave me: Mat [ 0*0*CV_8UC1, isCont=false, isSubmat=false, nativeObj=0xe390b0, dataAddr=0x0 ] and sout(img.dump()) gave me and empty array [] for the silly loop, was a desperate move ...

evilkid gravatar imageevilkid ( 2017-06-23 14:24:36 -0500 )edit

I noticed you said the only supported flags are -1,0,1. So i tried with -1 and worked, weird because i have tried with Imgcodecs.IMREAD_UNCHANGED earlier and it didnt work.

evilkid gravatar imageevilkid ( 2017-06-23 14:40:21 -0500 )edit

also, be careful with png images containing alpha !

in the case of the image above, the color channels are all 0, the only information is in alpha. (someone tried to make it extra hard to decrypt this ;)

berak gravatar imageberak ( 2017-06-24 02:31:57 -0500 )edit