我想从图像中剪切 16 block 。我正在使用 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 的第二个循环中崩溃。
最佳答案
您应该使用矩形
constructor接受两个点
,左上角和右下角:
Rect roi = new Rect(new Point(i*x ,j*y), new Point((i+1)*x, (j+1)*y));
关于java - OpenCV,从图像中剪切 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968019/