2016-01-25 73 views
1

我一直在努力與opencv項目中的一些斷言。OpenCV C++ Assertion

首先,我將墊對象,以確保他們的類型:

gray_im.convertTo(gray_im, CV_8U); 
diff_im.convertTo(diff_im, CV_8U); 

然後我做了減法。這條線路是在那裏我得到斷言:

diff_im = gray_im - prev_im; 

這裏是斷言:

OpenCV Error: Bad argument (When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified) in arithm_op, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/arithm.cpp, line 1313 

當我打印信息約我使用減法圖像;

diff_im. type: 5 rows: 600 cols 800 
gray_im. type: 5 rows: 600 cols 800 
prev_im. type: 0 rows: 600 cols 800 

我認爲我明確指定輸出數組(如果我是正確的,這裏diff_im是輸出數組,對吧?),通過將其轉換爲CV_8U。另外當我在運行時打印diff_im的類型信息時,它說它是「5」,這意味着我明確指定了「diff_im」的類型。

我錯了嗎?有什麼建議麼?

OpenCV版本:2.4.8 在此先感謝。

回答

1

gray_im的類型爲5和prev_im的類型爲0。你可以忘記正確初始化prev_im,即:

prev_im = cv::Mat::zeros(gray_im.size(), gray_im.type()); 

更新#1:

好,dst = src1 - src2相當於subtract(dst, src1, dst)。參數dstOutputArray,僅用於輸出功能參數。 dst的類型不能由operator-僅定義subtract()通過其參數dtype提供了可能性,請參見here

如果dtype沒有給出並且兩個輸入參數是數組然後src1.type()必須等於src2.type(),見here

所以,你應該如下覆蓋operator-

cv::Mat src1(600, 800, CV_8U); 
cv::Mat src2(600, 800, CV_32F); 
cv::Mat dst; 

//dst = src1 - src2; // --> this will give the same assert 
cv::subtract(src1, src2, dst, cv::noArray(), CV_32F); // type of dst is CV_32F 
+0

但是斷言說**當添加的輸入數組/減/乘/除功能有不同的類型,輸出數組類型必須明確指定* *,這意味着(我認爲)**我可以處理不同類型的輸入,但在這種情況下,您必須指定我將寫入的類型。如果我是正確的,在這種情況下,輸出是diff_im,並且它的類型是明確指定的(在本例中爲5)。對? –

+0

請在上面找到我的更新#1 – Kornel

+0

好的,我會試一試。將在今天結束時讓你知道結果。另外,是否有任何理由不經意地改變圖像類型? –