1. 程式人生 > >opencv Mat 賦值失敗

opencv Mat 賦值失敗

我找到的原因,是因為我的類成員變數裡,之前的那個越界了,導致我目前這個mat 沒辦法被賦值,因為其refcount為0,那麼下面這段程式碼中的判斷就會引起中斷,refcount為0的意思就是代表這個變數已經被銷燬了。

inline void Mat::release()
{
    if( refcount && CV_XADD(refcount, -1) == 1 )
        deallocate();
    data = datastart = dataend = datalimit = 0;
    size.p[0] = 0;
    refcount = 0;
}

或者根據官方文件 ,呼叫這條函式的原因是
If the current array shape and the type match the new ones, return immediately. Otherwise, de-reference the previous data by calling Mat::release().
賦值前後的矩陣形狀不同,也是因為之前的成員變數越界導致目前的這個mat形狀不同了導致的。
另外除錯的時候用監聽變數可以查到具體變數在哪裡被改變了,這樣對初始化的這種錯誤排查是一個比較好的辦法。