2015-12-11 102 views
0

我想製作一個程序,它從兩張相同的圖片找到齊次座標,但是對象已經被旋轉或翻譯,然後取第一張圖片並覆蓋第二張圖片圖片。Substracting不同大小的兩個矩陣,OpenCV錯誤

問題是,我得到一個矩陣錯誤,因爲我有不同的矩陣大小。

我已經找到了解決方案,我需要應用旋轉和平移獲得第二的第一張照片(該值,我需要去申請第二矩陣)

這是我的代碼:

Size size(image2.cols, image2.rows); 

    Mat dest = cv::Mat::zeros(image2.rows, image2.cols, CV_32FC2); 

    warpAffine(image2, dest, t, size); 

    namedWindow("manipulated img", CV_WINDOW_NORMAL); 
    imshow("manipulated img", dest); 

    Mat output; 

    cout << image1.size() << endl; 
    cout << dest.size() << endl; 

    bitwise_and(dest, image1, output); 

    //subtract(dest, image1, output); 
    namedWindow("output img", CV_WINDOW_NORMAL); 
    imshow("output img", output); 

我得到以下錯誤:

> [3722 x 2937] 
> [3722 x 2932] 
> OpenCV Error: Sizes of input arguments do 
> not match (The operation is neither 'a rray op array' (where arrays 
> have the same size and type), nor 'array op scalar' , nor 'scalar op 
> array') in cv::binary_op, file C:\builds\master_PackSlave-win32 
> -vc12-shared\opencv\modules\core\src\arithm.cpp, line 1573 

我想我知道問題是什麼,但我不知道該解決方案是什麼。

矩陣具有不同的尺寸

image1.size() = [3722 x 2937] 
dest.size() = [3722 x 2932] 

我能做些什麼,來解決這個問題?

+0

你想如何在不同大小的矩陣之間進行位移?這在理論上是不可能的 –

+0

哦,我忘了提及,我也試圖減去每個圖像。有什麼方法可以「添加」像素嗎? – larsen

回答

0
  1. 創建一個同樣大小的圖像1
  2. 的新矩陣(newDest)
  3. 設置newDest爲零
  4. 複製DEST矩陣的newDest到所需感興趣區域(可以設置左,頂部爲0,0,如果你想在新的矩陣是在右上角)

像這樣:

src.copyTo(dst(Rect(left, top, src.cols, src.rows))); // left and top are the x,y position of where this Mat is going 

類似李NK: Copy an cv::Mat inside a ROI of another one

1

我懷疑你只需要做出destimage1的大小,而不是image2。有一個很好的機會,warpAffine將妥善處理。

+0

所以只需更改\t Mat dest = cv :: Mat :: zeros(image1.rows,image1.cols,CV_32FC2); ?:-) – larsen