2015-10-06 88 views
1

我想从图像切割16件。我正在使用OpenCV和方法submatOpenCV,从图像切割件

List<Mat> listOfPieces = new ArrayList<Mat>(); 

    Mat mat = new Mat(); 
    Utils.bitmapToMat(bitmap1, mat); 

    int x = mat.cols()/4; 
    int y = mat.rows()/4; 

    for(int i=0; i<4; i++){ 
     for(int j=0; j<4; j++){    
      Rect roi = new Rect(i*x ,j*y, (i+1)*x, (j+1)*y); 
      Mat submat = mat.submat(roi); 
      listOfPieces.add(submat); 
     } 
    } 

我收到此错误:

10-06 12:42:19.842: E/cv::error()(18420): OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat(const cv::Mat&, const Rect&), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp, line 323 

我检查了我宝贵的roi,如果不大于垫的尺寸更大。此代码在for的第二个循环中崩溃。

+0

看看[这里](http://stackoverflow.com/questions/32870430/how-到分频的-的OpenCV-MAT-在矩形子区域/ 32870595#32870595)。它是C++,但可以轻松移植。 – Miki

+0

您应该使用接受左上角和右下角的'Rect'构造函数'' – Miki

+0

谢谢,这有帮助。 –

回答

0

您应该使用Rectconstructor接受两个Point,左上和右下:

Rect roi = new Rect(new Point(i*x ,j*y), new Point((i+1)*x, (j+1)*y));