2011-12-22 84 views

回答

3

一個簡單而乾淨的方法是使用create()方法。你可以把它多次,只要你想,它會重新分配時paramters發送到創建和當前圖像參數不匹配的圖像緩存:

Mat frame; 

for(int i=0;i<n;i++) 
{ 
    ... 
    // if width[i], height[i] or type[i] are != to those on the i-1 
    // or the frame is empty(first loop) 
    // it allocates new memory 
    frame.create(width[i], height[i], type[i]); 
    ... 
    // do some processing 
} 
+4

不會創建()破壞原始圖像?我以爲你必須使用調整大小 – 2012-12-21 05:17:32

+0

你是對的Mat ::創建去引用以前的數據http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-create而Mat :: resize保留它就像STL矢量類http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-resize – gantzer89 2013-11-24 18:48:15

+0

由於我懶得解釋,resize()不起作用的STL容器的功能,並在這種情況下create()做這項工作。如果新尺寸與原始尺寸不匹配,它只會重新分配。 – Sam 2013-11-25 08:43:20

6

如果您想要調整圖像大小,請選擇​​!

創建一個新的Mat dst與維和數據輸入你想要的,那麼:

cv::resize(src, dst, dst.size(), 0, 0, cv::INTER_CUBIC); 

有除了cv::INTER_CUBIC其他插值方法,檢查文檔。

+0

感謝卡爾,但我的問題是,我想動態地定義cv :: Mat對象的類型和維度。 resize()可能是一個解決方案,但它不完全是我正在尋找的。 – Rasoul 2011-12-22 16:28:52

+0

那麼,我已經使用了'resize()'來達到這個目的(就我所能理解的情況而言),這比我可以算的更多。每當你需要改變'src' Mat的尺寸時,你所要做的就是用你想要的尺寸和類型聲明一個新的Mat,並執行'resize()',以便將數據擴展到新的尺寸。如果你願意,你仍然可以執行'src = dst.clone();'將redimensioned數據複製到原始的Mat。 – karlphillip 2011-12-22 16:38:59

3

你只是想用你計算的Size變量來定義它嗎?

// dynamically compute size... 
Size dynSize(0, 0); 
dynSize.width = magicWidth(); 
dynSize.height = magicHeight(); 

int dynType = CV_8UC1; 
// determine the type you want... 

Mat dynMat(dynSize, dynType); 
+0

謝謝!我想我找到了解決方案,聲明cv :: Mat圖像;並通過調用create()作爲image.create(magicWidth(),magicHeight(),dynType)來動態改變其屬性。 – Rasoul 2011-12-22 17:15:08

+0

是的,''create()''應該在聲明'Mat'對象後立即和構造函數做同樣的事情。 – mevatron 2011-12-22 17:36:36