2016-02-27 71 views
0

我已经看过how to get sub image by using OpenCV in java api得到一个子图像,但这并没有帮助使用OpenCV的Java的

我很好奇如何创建,我已经从文件加载的垫图像的子图像。当我运行:

crop = img.submat(405, 450, 280, 335); 

我得到:

OpenCV Error: Assertion failed (m.dims >= 2) in cv::Mat::Mat, file ..\..\..\..\opencv\modules\core\src\matrix.cpp, line 269 
Exception in thread "main" 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_submat_rr(Native Method) 
    at org.opencv.core.Mat.submat(Mat.java:2270) 
    at Parking.WebCommunications.processImage(WebCommunications.java:54) 
    at Parking.WebCommunications.<init>(WebCommunications.java:27) 
    at Parking.App.main(App.java:19) 

我似乎无法找出为什么发生这种情况。当我运行什么似乎是一个类似一块在它正常工作在图像上Python代码...但我需要的java工作...

编辑:

Range xRange = new Range(405, 450); 
    Range yRange = new Range(280, 355); 
    Mat crop; 
    Mat blur = null; 

    System.loadLibrary("opencv_java2411"); 

    //Load image from file 
    Mat img = Highgui.imread("/Users/\"User name\"/git/SE300/JavaWorkspace/ParkingLotApp/src/main/resources/bottomOpen.JPG"); 

    //LOOP: 
     //Crop to the Nth spot: cropN = img[y:y+h, x:x+w] 
    System.out.println(img.rows()); 
    System.out.println(img.cols()); 

     crop = img.submat(405, 450, 280, 335); 
+1

前行'作物= img.submat(405,450,280,335);'尝试打印和调试'img.rows'和'img.cols'以检查图像是否被正确加载。 – ZdaR

+0

@ZdaR我真的这个,它打印出两个都为0。有没有添加照片的特定方式?我添加了上面的代码的其余部分: – Ian

+0

我很愚蠢,不知道绝对路径在哪里,但是当我做到了“src/main/resources/bottomOpen.JPG”它工作....谢谢哈哈 – Ian

回答

0

我manged做在Opencv的Java版本中有一些基本的类。

这是它的代码。

<pre> 
// select the region fist   
rectCrop = new Rect(start_x, start_y, width, height); 

// generate matrix of the interested region, from original_image 
     Mat image_roi = new Mat(original_image, rectCrop); 

// code to write the interested image to disk 
     Highgui.imwrite("/Users/kapil/Research/test_imgs/out/area_of_intereset.jpg", image_roi); 
</pre>