gpt4 book ai didi

java - 使用 OpenCV 将代码从 python 移植到 java

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:24 24 4
gpt4 key购买 nike

我正在尝试获取 python 代码的 java 实现。该代码与opencv相关。 python 代码工作得很好,但我在让它与 java 一起工作时遇到了一些困难,我得到了 null 异常。我不知道它是否与reshape函数或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

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

最佳答案

Mat::reshape 的第一个参数让您指定 Mat 对象的 channel 数。

当您 reshape approx 变量时,您将向其传递零 channel 。您的问题可能出在这一行:

approx = approx.reshape(0, 4);

关于java - 使用 OpenCV 将代码从 python 移植到 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30162325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com