OpenCV:minMaxLoc 没有给出最小/最大值
OpenCV: minMaxLoc gives no min / max value
我正在尝试关注 OpenCV tutorial on image template matching using javacpp-presets。
我想放弃分数太低的比赛,但 minMaxLoc
似乎不是 return minVal
和 maxVal
.
import org.bytedeco.javacpp.DoublePointer;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Point;
import static org.bytedeco.javacpp.opencv_core.CV_32FC1;
import static org.bytedeco.javacpp.opencv_core.NORM_MINMAX;
import static org.bytedeco.javacpp.opencv_core.minMaxLoc;
import static org.bytedeco.javacpp.opencv_core.normalize;
import static org.bytedeco.javacpp.opencv_imgcodecs.IMREAD_COLOR;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
import static org.bytedeco.javacpp.opencv_imgproc.TM_CCOEFF_NORMED;
import static org.bytedeco.javacpp.opencv_imgproc.matchTemplate;
public class MatchTemp {
public static void match(String img, String temp) {
Mat image = imread(img, IMREAD_COLOR);
Mat template = imread(temp, IMREAD_COLOR);
int resultRows = image.rows() - template.rows() + 1;
int resultCols = image.cols() - template.cols() + 1;
Mat result = new Mat(resultRows, resultCols, CV_32FC1);
matchTemplate(image, template, result, TM_CCOEFF_NORMED);
Mat normalizedResult = new Mat();
normalize(result, normalizedResult, 0, 1, NORM_MINMAX, -1, new Mat());
DoublePointer maxVal = new DoublePointer();
DoublePointer minVal = new DoublePointer();
Point minLoc = new Point();
Point maxLoc = new Point();
minMaxLoc(normalizedResult, minVal, maxVal, minLoc, maxLoc, null);
System.out.println("min: "+minLoc.x()+", "+minLoc.y());
System.out.println("max: "+maxLoc.x()+", "+maxLoc.y());
minVal.get(); // this throws exception
maxVal.get(); // this throws exception
}
}
例外情况是:
Exception in thread "main" java.lang.NullPointerException: This pointer address is NULL.
at org.bytedeco.javacpp.DoublePointer.get(Native Method)
at org.bytedeco.javacpp.DoublePointer.get(DoublePointer.java:101)
...
我做错了什么?我怎样才能得到分数?我是否必须手动检查 Mat
的内容?
该错误告诉您它正在尝试访问 Null 引用。阅读 DoublePointer constructor which tells to look at Pointer.Pointer() 的文档,它基本上告诉您以下内容:
Default constructor that does nothing.
这意味着它只是一个类似C++的指针:
double *p;
它没有指向任何 double,但是文档告诉你还有另一个 DoublePointer 的构造函数
public DoublePointer(long size)
表示它分配了一个这样大小的数组。如果大小等于 1,则相当于在 C++ 中说这相当于:
double p[1];
或者基本上
double *p = new double;
我从未使用过 JavaCpp,所以如果我遗漏了什么,请发表评论。
我正在尝试关注 OpenCV tutorial on image template matching using javacpp-presets。
我想放弃分数太低的比赛,但 minMaxLoc
似乎不是 return minVal
和 maxVal
.
import org.bytedeco.javacpp.DoublePointer;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Point;
import static org.bytedeco.javacpp.opencv_core.CV_32FC1;
import static org.bytedeco.javacpp.opencv_core.NORM_MINMAX;
import static org.bytedeco.javacpp.opencv_core.minMaxLoc;
import static org.bytedeco.javacpp.opencv_core.normalize;
import static org.bytedeco.javacpp.opencv_imgcodecs.IMREAD_COLOR;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
import static org.bytedeco.javacpp.opencv_imgproc.TM_CCOEFF_NORMED;
import static org.bytedeco.javacpp.opencv_imgproc.matchTemplate;
public class MatchTemp {
public static void match(String img, String temp) {
Mat image = imread(img, IMREAD_COLOR);
Mat template = imread(temp, IMREAD_COLOR);
int resultRows = image.rows() - template.rows() + 1;
int resultCols = image.cols() - template.cols() + 1;
Mat result = new Mat(resultRows, resultCols, CV_32FC1);
matchTemplate(image, template, result, TM_CCOEFF_NORMED);
Mat normalizedResult = new Mat();
normalize(result, normalizedResult, 0, 1, NORM_MINMAX, -1, new Mat());
DoublePointer maxVal = new DoublePointer();
DoublePointer minVal = new DoublePointer();
Point minLoc = new Point();
Point maxLoc = new Point();
minMaxLoc(normalizedResult, minVal, maxVal, minLoc, maxLoc, null);
System.out.println("min: "+minLoc.x()+", "+minLoc.y());
System.out.println("max: "+maxLoc.x()+", "+maxLoc.y());
minVal.get(); // this throws exception
maxVal.get(); // this throws exception
}
}
例外情况是:
Exception in thread "main" java.lang.NullPointerException: This pointer address is NULL.
at org.bytedeco.javacpp.DoublePointer.get(Native Method)
at org.bytedeco.javacpp.DoublePointer.get(DoublePointer.java:101)
...
我做错了什么?我怎样才能得到分数?我是否必须手动检查 Mat
的内容?
该错误告诉您它正在尝试访问 Null 引用。阅读 DoublePointer constructor which tells to look at Pointer.Pointer() 的文档,它基本上告诉您以下内容:
Default constructor that does nothing.
这意味着它只是一个类似C++的指针:
double *p;
它没有指向任何 double,但是文档告诉你还有另一个 DoublePointer 的构造函数
public DoublePointer(long size)
表示它分配了一个这样大小的数组。如果大小等于 1,则相当于在 C++ 中说这相当于:
double p[1];
或者基本上
double *p = new double;
我从未使用过 JavaCpp,所以如果我遗漏了什么,请发表评论。