OpenCV,从图像切割件

时间:2015-10-06 10:46:00

标签: java android opencv mat

我想从图片中剪下16块。我使用OpenCV和方法submat

    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的第二个周期崩溃。

1 个答案:

答案 0 :(得分:0)

您应该使用接受两个Rect,左上角和右下角的Point constructor

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