Port code from python to java using OpenCV

Gabriel Muñumel

I'm trying to get a java implementation of a python code. The code is related to opencv. The python code works great, but i'm facing some difficulties to get it work with java, I get a null exception. I don't know if it's related to the reshape functions, or the get/put functions.

Python:

approx = cv2.approxPolyDP(i,0.02*peri,True)
...
approx=rectify(approx)
...
def rectify(h):
    h = h.reshape((4,2))
    hnew = np.zeros((4,2),dtype = np.float32)

    add = h.sum(1)
    hnew[0] = h[np.argmin(add)]
    hnew[2] = h[np.argmax(add)]

    diff = np.diff(h,axis = 1)
    hnew[1] = h[np.argmin(diff)]
    hnew[3] = h[np.argmax(diff)]

    return hnew

Java:

Imgproc.approxPolyDP(newMtx, approx, 0.02 * peri, true);
...
approx = rectify(approx);
...
private Mat rectify(Mat approx) {

    DoubleMatrix ndApproxNew, ndAdd, ndApprox;
    double [] d;
    Mat hnew;

    ndApproxNew = DoubleMatrix.zeros(4, 2);
    hnew = new Mat();

    approx = approx.reshape(0, 4);

    // sum
    d = approx.get(4, 2);
    Log.d(TAG, "daily - heigth: " + approx.height() + " width: " + approx.width());
    ndApprox = new DoubleMatrix(4, 2, d);

    Log.d(TAG, "daily - " + ndApprox.getRow(0)); // <- ERROR NULL
    Log.d(TAG, "daily - " + ndApprox.getRow(1));
    Log.d(TAG, "daily - " + ndApprox.getRow(2));
    Log.d(TAG, "daily - " + ndApprox.getRow(3));
Gabriel Muñumel

Ok, I figured it out (wasn't easy lol).

My problem is not exactly in that part of the code, the problem relies on I didn't pay attention to the types of values returned.

To be more precise, opencv-python method approxPolyDP return:

Nx2 numpy array (Python interface)

While in my case in java is:

MatOfPoint2f approxCurve

I realized that my problem was how to manage with a bunch of points (4 in this case) rather than a reshape function.

I changed my code to this one and everything is working :D :

private MatOfPoint2f rectify(MatOfPoint2f approx) {

    DoubleMatrix ndApproxNew, ndAdd, ndApprox;
    Point[] p;
    double [] d;
    MatOfPoint2f hnew;

    ndApproxNew = DoubleMatrix.zeros(4, 2);
    hnew = new MatOfPoint2f();
    //hnew.put(4, 2, ndZeros.data().asFloat());
    //hnew = np.zeros((4, 2), dtype = np.float32);

    //if (!approx.isContinuous()) approx = approx.clone();
    p = approx.toArray();
    Log.d(TAG, "daily - " + p[0].x + " " + p[0].y);
    Log.d(TAG, "daily - " + p[1].x + " " + p[1].y);
    Log.d(TAG, "daily - " + p[2].x + " " + p[2].y);
    Log.d(TAG, "daily - " + p[3].x + " " + p[3].y);

Cheers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using ANTLR with Python with code migrated from Java

From Dev

Using OpenCV TLD from Python

From Dev

Using OpenCV TLD from Python

From Dev

How can I port this code using collections.deque from Python 2.5 to 3.4?

From Dev

Reading from RS 232 port using python

From Dev

Delay from serial port using Python

From Dev

Delay from serial port using Python

From Dev

Issue in calling Python code from Java (without using jython)

From Dev

Using python opencv to load image from zip

From Dev

Cropping circle from image using opencv python

From Dev

Python creating video from images using opencv

From Dev

Using python opencv to load image from zip

From Dev

Port Python Code to Android

From Dev

Port Python Code to Android

From Dev

How to Save the trackbar values after closing the python code using Opencv?

From Dev

Converting OpenCV code snippet from C++ to Python

From Dev

OpenCV Sample Code in python

From Dev

How to get port number from an HTTP response, using Python?

From Dev

code not reading from serial port

From Dev

Is there a way to port this shell code to python?

From Dev

How to remove the background from static image in Java using OpenCV?

From Dev

How to clear the small black dots from image in OpenCV using Java?

From Dev

Getting Mats from frames in a gif using OpenCV and Java

From Dev

How to remove the background from static image in Java using OpenCV?

From Dev

How to Read Text From Bounding Box using Java With OpenCV

From Dev

Updating code from openCV to openCV2

From Dev

Extract foreground from individual frames using opencv for python

From Dev

Auto-capture an image from a video in OpenCV using python

From Dev

How to read gif from url using opencv (python)

Related Related

HotTag

Archive