Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在iPhone中裁剪图像时出现Opencv错误_C++_Iphone_Objective C_Image Processing_Opencv - Fatal编程技术网

C++ 在iPhone中裁剪图像时出现Opencv错误

C++ 在iPhone中裁剪图像时出现Opencv错误,c++,iphone,objective-c,image-processing,opencv,C++,Iphone,Objective C,Image Processing,Opencv,我使用下面的代码设置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 = [CaptureViewCon

我使用下面的代码设置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).clone();

我知道现在回答这个问题已经太迟了,但也许这会帮助其他面临类似问题的人。这个错误似乎是相当自我描述的。 它表示您感兴趣的roi区域矩形似乎超出了cv::Mat的范围

确保左上角x、左上角y、宽度和高度在测试垫的范围内:

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;