运行此代码时遇到此错误。看起来文件在数组中(我认为),但它仍然给出了错误。我怎么解决这个问题?
OpenCV Error: Assertion failed (m.dims >= 2) in cv::Mat::Mat, file ..\..\..\..\opencv\modules\core\src\matrix.cpp, line 269
CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\matrix.cpp:269: error: (-215) m.dims >= 2 in function cv::Mat::Mat]
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:676)
at last.Last1.main(Last1.java:39)
代码。
public class Last1 {
public static void main (String[] args){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
try{
File folder = new File("C:\\Documents and Settings\\James2\\Desktop\\JE\\testplatenum\\images");
File[] listOfFiles = folder.listFiles();
for (int a = 0; a < listOfFiles.length; a++){
for (File file : listOfFiles) {
if (file.isFile()) {
System.out.println(file.getName());
String img[] = {file.getPath()};
Mat img_grayROI = Highgui.imread(img[a], Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Rect roi = new Rect(300,250,300,200);
Mat cropped = new Mat(img_grayROI, roi);
Imgproc.threshold(cropped, cropped, -1, 255, Imgproc.THRESH_BINARY+Imgproc.THRESH_OTSU);
Imgproc.dilate(cropped, cropped, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
Mat result = cropped;
Highgui.imwrite("1)Crop_"+a+".jpg",result);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}