2

I have already looked at how to get sub image by using OpenCV in java api, but this did not help

I am curious how to create a sub image of a Mat image that I have loaded in from a file. When I run:

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

I get :

  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)

I cannot seem to figure out why this is happening. When I run what seems to be a similar piece of code in python on the image it works properly...but I need java to work...

EDIT:

    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);
Community
  • 1
  • 1
Ian
  • 21
  • 1
  • 4
  • 1
    Before the line `crop = img.submat(405, 450, 280, 335);` try to print and debug the `img.rows` and `img.cols` to check if the image was properly loaded. – ZdaR Feb 27 '16 at 06:33
  • @ZdaR I true this and it printed out 0 for both. Is there a specific way to add photos in? I added the rest of the code above: – Ian Feb 27 '16 at 20:39
  • Im stupid, not sure where the absolute path was taking it but when I made it "src/main/resources/bottomOpen.JPG" it worked....thanks haha – Ian Feb 27 '16 at 20:43

1 Answers1

1

I manged to do it with some basic classes in Java version of Opencv.

Here is the code for it.

// 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);
Kapil Bhosale
  • 153
  • 2
  • 8