2013-02-04 13 views
1

我使用下面的代碼來設置ROI並裁剪圖像。Opencv在iPhone中裁剪圖像的錯誤

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);

但我無法追查這個問題的原因。在上面的代碼中,我錯了嗎?

回答

0

嘗試clone()該對象。

image_roi = testMat(roi).clone(); 
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;