Ask Your Question
0

phash of img_hash test fail in java

asked 2018-01-09 22:03:53 -0600

lezo gravatar image

Hi dear: I turn the test example of img_hash into java, but test fail.

env:
opencv3.4.0 with module img_hash
JDK8
java test code:
@test
public void testHashCode() {
List codeList = Lists.newArrayList(
1,0,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1);
String expect = Joiner.on("").join(codeList);
int capacity = 32 * 32;
int value = 0;
double[] dataArr = new double[capacity];
for (int i = 0; i != capacity; ++i) {
dataArr[i] = value++;
}
Mat inMat = new Mat(32, 32, CvType.CV_8U);
inMat.put(0, 0, dataArr);
Mat outMat = new Mat();
Img_hash.pHash(inMat, outMat);
StringBuilder sb = new StringBuilder();
for (int row = outMat.rows() - 1; row >= 0; row--) {
for (int col = outMat.cols() - 1; col >= 0; col--) {
Double val = outMat.get(row, col)[0];
String origin = "00000000";
origin = origin + Long.toBinaryString(val.longValue());
sb.append(origin.substring(origin.length() - 8));
}
}
String binary = sb.toString();
Assert.assertEquals(expect, binary);
}

org.junit.ComparisonFailure: Expected :1011111101111111111111110111111111111111011111111111111101111111 Actual : 1111111111111110111111101111111011111110111111101111110011111111

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-01-10 04:28:55 -0600

berak gravatar image

updated 2018-01-10 04:45:36 -0600

imho, you should not use Strings for this at all, and your Mat access is broken, counting backwards

int capacity = 32 * 32;
double[] dataArr = new double[capacity];
for (int i = 0; i < capacity; ++i) {
    dataArr[i] = i;
}
Mat inMat = new Mat(32, 32, CvType.CV_8U);
inMat.put(0, 0, dataArr);
Mat outMat = new Mat();
Img_hash.pHash(inMat, outMat);

int bits[] = { // lsb first !
    1,0,1,1,1,1,1,1,
    0,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,
    0,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,
    0,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,
    0,1,1,1,1,1,1,1
};

for (int j=0; j<8; j++) {
    int check=0;
    for (int i=0; i<8; i++) {
       check |= (bits[i+j*8]) << i;
    }
    int out = (int)(outMat.get(0,j)[0]);
    Assert.assertEquals(check, out);
}

NOTE: this compares bytes, not bits. if you want bits, try like this:

for (int j=0; j<8; j++) {
    int out = (int)(outMat.get(0,j)[0]);
    for (int i=0; i<8; i++) {
       int a = bits[i+j*8];
       int b = (out & (1<<i)) > 0 ? 1 : 0;
       Assert.assertEquals(a, b);
    }
}
edit flag offensive delete link more
0

answered 2018-01-17 21:28:15 -0600

lezo gravatar image

thanks a lost, i run your test but fail. cause: j:0,out:255 java.lang.AssertionError: Expected :253 Actual :255

edit flag offensive delete link more

Comments

you must be doing something else then, the numbers are all correct:

for (int j=0; j<8; j++) {
    int check=0;
    for (int i=0; i<8; i++) {
       check |= (bits[i+j*8]) << i;
    }
   System.out.println(check);  
}

 [java] 253
 [java] 254
 [java] 255
 [java] 254
 [java] 255
 [java] 254
 [java] 255
 [java] 254
berak gravatar imageberak ( 2018-01-17 22:42:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-09 22:01:11 -0600

Seen: 347 times

Last updated: Jan 17 '18