Converting Mat to one dimensional float array for EM?

asked 2020-02-11 11:59:26 -0600

Tarcisioflima gravatar image

I'm trying to convert a Python code to Java. However, I'm unable to find a way to create the sample to train the Expectation Maximization as it should be a one-channel matrix with 2 values as below:

  • row 0: S, V
  • row 1: S, V
  • row 2: S, V
  • row 3: S, V

In Python, I was able to do it as follow:

def convert_to_samples(image, height, width):
    samples = []
    for y in range(0, height):
        for x in range(0, width):
            samples.append(image[y, x])
    samples = np.float32(np.vstack(samples))
    return samples

I have tried as following without success:

public double[][] convert_to_samples(Mat image) {
    double[][] samples = new double[image.height()][];
        for(int i = 0; i < image.height(); i++) {
            for(int j = 0; j < image.width(); j++) {
                samples[i] = image.get(i, j);
            }
        }
    return sortRowWise(samples);
}

private static double[][] sortRowWise(double[][] m) {
    for (double[] values : m) Arrays.sort(values);
    return m;
}

Could someone help me transform the Mat?

edit retag flag offensive close merge delete