2014-01-21 27 views
1

我是OpenCV圖像處理新手。我的任務很簡單。我必須得到圖像的1/4(下1/4)。圖像的大小是320 x 240.我在Mat對象中使用了ROI Rect來獲取它。Mat OpenCV Rect投資回報率:訪問衝突

Mat img_roi; 
img_roi= image(cv::Rect(0,180,320,240)); 

我收到以下錯誤:

First-chance exception at 0x7669c41f in first.exe: Microsoft C++ exception: cv::Exception >at memory location 0x0041ec30.. Unhandled exception at 0x7669c41f in first.exe: Microsoft C++ exception: cv::Exception at >memory location 0x0041ec30..

如果我指定在上面的代碼Rect(0,0,320,60),我得到的結果(這是上半部分)和代碼適用於其他一些值,例如Rect (0,0,320,240),即完整圖像。但與較低的季度價值(0,180,320,240)不同。

我也應該告訴我使用Iplimage來得到結果。只有Mat對象產生問題。

如果有人能指導我解決這個問題,我會非常感激。

回答

2

圖像的下半部分是Rect(0,180,320,60)。它不是Rect(0,180, 320, 240)也不是Rect(0,0,320,60)

+0

非常感謝你Michael Burdinov。有效。重要的是,我明白我的錯誤。我想知道,它是如何與Iplimage合作的? –

+0

不客氣。至於爲什麼它與IplImage一起工作,我認爲你得到的異常是由OpenCV本身拋出的,即Mat構造函數在完整性檢查時失敗。我不確定IplImage是否執行了相同的完整性檢查。訪問未分配的內存總是很糟糕,但並不總是會產生異常。 –

+0

非常感謝您的解釋。 –

2

cv::Rect的參數是:

cvRect(int x, int y, int width, int height);

這裏的X和Y代表的左上角,而寬度和高度代表你想在你的圖像的行和列數。

你的情況:行數= 320的cols = 240

因此得到左下角使用:

Rect(0, image.rows/2, image.cols/2, image.rows/2 )

,如果你想擁有右下 image then use:

Rect(image.cols/2, image.rows/2, image.cols/2, image.rows/2 )