Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mat.createBuffer.get() returns data inconsistent with Mat.row().col().data.get() #2265

Open
iis-MarkKuang opened this issue Aug 7, 2024 · 2 comments

Comments

@iis-MarkKuang
Copy link

iis-MarkKuang commented Aug 7, 2024

original code

// 1. download image
String url = "https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
byte[] bytes = HttpUtils.downloadBytes(url); // simply downloads the image to bytes

// 2. parse the byte array to Bytedeco opencv mat
Mat mat = new Mat(bytes);

// 3. define normalize function
public Mat normalize(Mat mat) {
  Mat cloneMat = null;
  try (Size size = new Size(IMAGE_CONFIG.pHashImgWidth, IMAGE_CONFIG.pHashImgHeight);
       Rect rect = new Rect(0, 0, IMAGE_CONFIG.pHashDimSize, IMAGE_CONFIG.pHashDimSize)) {
    resize(mat, mat, size);

    cloneMat = mat.clone();
    cloneMat.convertTo(mat, CV_32FC1);
    dct(mat, mat);
    cloneMat.release();
    cloneMat.close();
    return new Mat(mat, rect);
  }
}

// 4. Normalize
mat = normalize(imdecode(mat, opencv_imgcodecs.IMREAD_GRAYSCALE));


// 5. get mat data to buffer, then to array
float[] data = new float[(int)mat.total() * mat.channels()];
FloatBuffer buffer = mat.createBuffer();
buffer.get(data);

// 6. Compare buffer data with Mat data, the first row is consistent, but the rest is not;
for (int i = 0; i < rows; i++) {
  for (int j = 0; j < cols; j++) {
    System.out.println(data[i * cols + j] + " " + mat.row(i).col(j).data().getFloat());
  }
}

// output
// First row, consistent
// 4231.4995 4231.4995
// 10.374468 10.374468
// -258.0788 -258.0788
// 130.85948 130.85948
// 130.10544 130.10544
// -110.18334 -110.18334
// -133.00644 -133.00644
// 76.05422 76.05422

// Second row onwards, inconsistent
// 52.51634 8.2322445
// -46.74162 42.705864
// -6.3990197 -45.12134
// -13.678903 32.170387
// -15.738309 -25.068441
// 26.591276 -4.0662885
// -15.69106 29.847733
// -45.807583 -18.502712
// 24.062498 22.55215
// 30.870342 0.975155
// -26.865278 212.19658
// -13.313238 -140.92426
// 9.827322 -144.26242
// 5.082619 98.95524
// -2.573051 130.77933
// 6.37136 -64.56056
// -14.64978 -2.9284372
// -11.879759 15.052319

Please help check.

@iis-MarkKuang
Copy link
Author

Hi, can I get some priority on this matter? It's been a blocker.

@saudet
Copy link
Member

saudet commented Aug 9, 2024

Try to use createIndexer() instead: http://bytedeco.org/news/2014/12/23/third-release/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants