2016-12-29 154 views
1

我想向圖像中的單個通道添加高斯噪聲。這是我寫的代碼:在opencv代碼中的錯誤:C++

double Mean = 0.0; 
    double StdDev = 10.0; 
    Mat gauss_noise = Mat(img_a.size(), CV_16SC1); 
    randn(gauss_noise, Scalar::all(Mean), Scalar::all(StdDev)); 

    Mat img_a_noise_planes[3]; 
    split(img_a, img_a_noise_planes); 
    addWeighted(gauss_noise, 1.0, img_a_noise_planes[1], 1.0, 1.0, img_a_noise_planes[1]); 

    merge(img_a_noise_planes, 3, img_a); 

我有在代碼中addWeighted()行的錯誤。
錯誤:

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 /home/cortana/Libraries/opencv/modules/core/src/arithm.cpp, line 683 
terminate called after throwing an instance of 'cv::Exception' 
    what(): /home/cortana/Libraries/opencv/modules/core/src/arithm.cpp:683: error: (-5) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function arithm_op 

Aborted (core dumped) 

我在做什麼錯在這裏?

編輯: img_a_noise_planes[1]的顏色通道價值是1。所以我改變了我的addweighted()功能:

addWeighted(gauss_noise, 1.0, img_a_noise_planes[1], 1.0, 1.0, img_a_noise_planes[1], 1);

現在的錯誤是:

OpenCV Error: Assertion failed (mv[i].size == mv[0].size && mv[i].depth() == depth) in merge, file /home/cortana/Libraries/opencv/modules/core/src/convert.cpp, line 222 
terminate called after throwing an instance of 'cv::Exception' 
    what(): /home/cortana/Libraries/opencv/modules/core/src/convert.cpp:222: error: (-215) mv[i].size == mv[0].size && mv[i].depth() == depth in function merge 

Aborted (core dumped) 

在合併功能,每個img_a_noise_planeschannel=1,我已經把3在那裏爲img_a有3個通道,併合並而成。

+0

'img_a'的深度是多少?除非它是'CV_16SC3',否則你需要在'addWeighted'中指定輸出深度。 – beaker

回答

1

我猜想高斯Mattype是這裏的問題。 如果您不確定img_a類型,那麼您可以移動gauss_noise創建並使其依賴於拆分通道的類型。例如:

+0

我改變了我的'gauss_noise'定義,它給出了與編輯相同的錯誤 –

+0

我刪除了我爲增加的功能所做的更改,現在它可以工作。謝謝! –

1

根據錯誤消息,它看起來像img_a_noise_planes從未初始化或設置爲輸入的大小。

+0

這可能並非如此,因爲拆分應該做所需的分配 – Dusteh