1

以下のコードを使用して ROI を設定し、画像をトリミングしました。

cv::Mat testMat = [CaptureViewController cvMatWithImage:self.storeImage];
cv::Rect roi(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
cv :: Mat image_roi;
image_roi = testMat ( roi );

self.CroppedImage = [CaptureViewController imageWithCVMat:image_roi];
UIImageWriteToSavedPhotosAlbum(self.CroppedImage, self,  nil,nil);

しかし、以下のエラーが発生しています:

<Error>: CGContextDrawImage: invalid context 0x0
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)

私はブレークポイントを設定し、ここでテストしましたが、上記のエラーが発生した場所ですimage_roi = testMat ( roi );

しかし、この問題の原因を突き止めることができませんでした。上記のコードのどこかが間違っていますか?

4

2 に答える 2

0

これに答えるには遅すぎることはわかっていますが、同様の問題に直面している他の誰かを助けるかもしれません. エラーはかなり自己記述的であるようです。roi (対象領域) の四角形が cv::Mat の境界からはみ出しているようです。

(左上の x、左上の y、幅、高さ) が testMat の境界内にあることを確認してください。

faces[i].x = faces[i].x >= 0 ? faces[i].x : 0;
faces[i].y = faces[i].y >= 0 ? faces[i].y : 0;
faces[i].width = faces[i].x + faces[i].width > testMat.cols ? testMat.cols - faces[i].x : faces[i].width;
faces[i].height = faces[i].y + faces[i].height > testMat.rows ? testMat.rows - faces[i].y : faces[i].height;
于 2016-03-19T15:46:39.820 に答える
0

オブジェクトの clone() を試みます。

image_roi = testMat(roi).clone();
于 2013-04-04T01:37:42.793 に答える