bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.51k stars 1.58k forks source link

How to implement numpy squeeze in JavaCV (remove dimensions of size 1) #1954

Closed cansik closed 1 year ago

cansik commented 1 year ago

Is it possible to implement the numpy squeeze method in JavaCV?

The output of the dnn forward() method sometimes includes the batch number and is of shape [1, nc, nr], which means we first have to unpack the first dimension before processing the results. In python there is the nice method called np.squeeze() to remove dimension of size 1. How can we implement that in JavaCV? In python you could also just reshape the array, but this does not work in JavaCV:

Python

>>> import numpy as np
>>> m = np.zeros((1, 200, 300))
>>> m
array([[[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]]])
>>> np.reshape(m, (200, 300))
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])

Java

result = result.reshape(result.size(2), result.size(1));
// Leads to: matrix.cpp:1240: error: (-209:Sizes of input arguments do not match) Requested and source matrices have different count of elements in function 'cv::Mat::reshape'

Would be great if someone could give me a hint how to unwrap the result, so I can process it by using cols / rows as usual.

cansik commented 1 year ago

Just figured out that I can just create a new matrix with a pointer to the first dimension of the combined result:

Mat first = new Mat(result.size(1), result.size(2), CV_32F, result.ptr(0, 0));
saudet commented 1 year ago

Mat is a 2D matrix, that happens to have elements that can take a couple of channels, so it's kind of HWC, but you're not going to get CHW...?

saudet commented 1 year ago

Ah, yes, it does have some preliminary support for multiple dimensions, but functions like reshape() don't work. I suppose passing around the data might be the best option.