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));
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.
Comments