2016-04-28 63 views
0

我對旋轉和平移圖像的一段代碼:聯合旋轉和平移OpenCV中,在一通

Point2f pt(0, in.rows); 
double angle = atan(trans.c/trans.b) * 180/M_PI; 
Mat r = getRotationMatrix2D(pt, -angle, 1.0); 
warpAffine(in, out, r, in.size(), interpolation); /* rotation */ 

Mat t = (Mat_<double>(2, 3) << 1, 0, trans.a, 0, 1, -trans.d); 
warpAffine(out, out, t, in.size(), interpolation); /* translation */ 

的問題是,我在兩次這樣做。因此,例如,如果我的角度爲90度,則第一個「out」變量將爲空,因爲所有數據都超出範圍。有一種方法可以一次完成嗎?爲了避免丟失我的數據和黑色圖像。

我認爲最好的辦法是將r和t結合在一個矩陣中,但我有點失落。

此致

+0

你可以簡單地多重播放單應性。仿射變換必須擴展到3x3,通過添加第3行來實現:0 0 1,那麼您可以簡單地乘以Hcombined = H1 * H2;但要小心,你以正確的順序繁殖。 – Micka

+0

好的,謝謝。我如何簡單地使用opencv添加第三行到r?然後我不確定明白H1是什麼,H2是什麼。 H1是第三行添加的旋轉矩陣? H2是翻譯矩陣? – lock

+0

我會發布代碼示例 – Micka

回答

0

下面是對如何通過簡單的乘法和如何從中提取一個3x3單應的仿射變換的單應性2結合的例子。

int main(int argc, char* argv[]) 
{ 
    cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png"); 

    // create to 3x3 identity homography matrices 
    cv::Mat homography1 = cv::Mat::eye(3, 3, CV_64FC1); 
    cv::Mat homography2 = cv::Mat::eye(3, 3, CV_64FC1); 

    double alpha1 = -13; // degrees 
    double t1_x = -86; // pixel 
    double t1_y = -86; // pixel 

    double alpha2 = 21; // degrees 
    double t2_x = 86; // pixel 
    double t2_y = 86; // pixel 

    // hope there is no error in the signs: 
    // combine homography1 
    homography1.at<double>(0, 0) = cos(CV_PI*alpha1/180); 
    homography1.at<double>(0, 1) = -sin(CV_PI*alpha1/180); 
    homography1.at<double>(1, 0) = sin(CV_PI*alpha1/180); 
    homography1.at<double>(1, 1) = cos(CV_PI*alpha1/180); 
    homography1.at<double>(0, 2) = t1_x; 
    homography1.at<double>(1, 2) = t1_y; 

    // compose homography2 
    homography2.at<double>(0, 0) = cos(CV_PI*alpha2/180); 
    homography2.at<double>(0, 1) = -sin(CV_PI*alpha2/180); 
    homography2.at<double>(1, 0) = sin(CV_PI*alpha2/180); 
    homography2.at<double>(1, 1) = cos(CV_PI*alpha2/180); 
    homography2.at<double>(0, 2) = t2_x; 
    homography2.at<double>(1, 2) = t2_y; 

    cv::Mat affine1 = homography1(cv::Rect(0, 0, 3, 2)); 
    cv::Mat affine2 = homography2(cv::Rect(0, 0, 3, 2)); 

    cv::Mat dst1; 
    cv::Mat dst2; 

    cv::warpAffine(input, dst1, affine1, input.size()); 
    cv::warpAffine(input, dst2, affine2, input.size()); 


    cv::Mat combined_homog = homography1*homography2; 
    cv::Mat combined_affine = combined_homog(cv::Rect(0, 0, 3, 2)); 

    cv::Mat dst_combined; 

    cv::warpAffine(input, dst_combined, combined_affine, input.size()); 

    cv::imshow("input", input); 
    cv::imshow("dst1", dst1); 
    cv::imshow("dst2", dst2); 

    cv::imshow("combined", dst_combined); 

    cv::waitKey(0); 
    return 0; 
} 

在此示例中,首先將圖像旋轉並翻譯到左側,稍後再翻譯到右側。如果兩個轉換依次進行,重要的圖像區域將會丟失。相反,如果他們通過同形乘法相結合,就好像完整的操作在一個步驟中完成,而不會在中間步驟中丟失圖像部分。

輸入:如果圖像是第一變換

enter image description here

與H1,後用H 2:

enter image description here

如果圖像被變換直接與H1 * H2的組合:

enter image description here

此單應性組合的一個典型應用是首先將圖像中心轉換爲原點,然後旋轉,然後再轉換回原始位置。這具有如同圖像圍繞其重心旋轉的效果。

+0

噢謝謝,這應該對我有很大的幫助,我必須儘快測試它。我還有兩個問題。 alpha的單位是什麼?對於90度的-13,我很抱歉它一定是呃。我不知道你是否看到過,但我的翻譯數據是以(0,in.rows)(即左下角)作爲旋轉中心。它應該改變我想的矩陣。 – lock

+0

或者我可以先從(0,in.rows) – lock

+0

開始移位,對不起,單位是度。它是-13度,而不是90度。首先我嘗試了90度,但我想稍微改變一下,並沒有改變評論。抱歉。 – Micka