我试图获得python代码的java实现。该代码与opencv有关。 python代码工作得很好,但是我遇到了一些使用java工作的困难,我得到了一个 null 异常。我不知道它是否与重塑函数或 get / put 函数有关。
的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
爪哇:
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));
答案 0 :(得分:0)
答案 1 :(得分:0)
好的,我想通了(不容易大笑)。
我的问题并不完全在代码的那一部分,问题依赖于我没有注意返回的值的类型。
更确切地说,opencv-python方法approxPolyDP返回:
Nx2 numpy array (Python interface)
虽然我在java的案例中是:
MatOfPoint2f approxCurve
我意识到我的问题是如何管理一堆点(在这种情况下为4)而不是reshape
函数。
我将代码更改为此代码,一切正常: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);
干杯。