有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OpenCV,从图像中剪切片段

我想从图片上剪下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) 个答案

  1. # 1 楼答案

    您应该使用Rect{a1},它接受两个Point,左上和右下:

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