下面是對如何通過簡單的乘法和如何從中提取一個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;
}
在此示例中,首先將圖像旋轉並翻譯到左側,稍後再翻譯到右側。如果兩個轉換依次進行,重要的圖像區域將會丟失。相反,如果他們通過同形乘法相結合,就好像完整的操作在一個步驟中完成,而不會在中間步驟中丟失圖像部分。
輸入:如果圖像是第一變換
與H1,後用H 2:
如果圖像被變換直接與H1 * H2的組合:
此單應性組合的一個典型應用是首先將圖像中心轉換爲原點,然後旋轉,然後再轉換回原始位置。這具有如同圖像圍繞其重心旋轉的效果。
你可以簡單地多重播放單應性。仿射變換必須擴展到3x3,通過添加第3行來實現:0 0 1,那麼您可以簡單地乘以Hcombined = H1 * H2;但要小心,你以正確的順序繁殖。 – Micka
好的,謝謝。我如何簡單地使用opencv添加第三行到r?然後我不確定明白H1是什麼,H2是什麼。 H1是第三行添加的旋轉矩陣? H2是翻譯矩陣? – lock
我會發布代碼示例 – Micka