OpenCV,从图像中切出碎片

OpenCV, cut pieces from image

我想从图像中剪切 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.

的第二个循环中崩溃

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

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