用 OpenCVSharp 4.5 跑一遍 OpenCV 官方教程
原 OpenCV 官方教程链接:似乎网站宕机了
using System;
using OpenCvSharp;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class tutorial55 : ITutorial
{
private void basicPanoramaStitching(string img1Path, string img2Path)
{
Mat img1 = new Mat(img1Path);
Mat img2 = new Mat(img2Path);
//! [camera-pose-from-Blender-at-location-1]
Mat c1Mo = new Mat(4, 4, MatType.CV_64F, new Double[]{ 0.9659258723258972, 0.2588190734386444, 0.0, 1.5529145002365112,
0.08852133899927139, -0.3303661346435547, -0.9396926164627075, -0.10281121730804443,
-0.24321036040782928, 0.9076734185218811, -0.342020183801651, 6.130080699920654,
0, 0, 0, 1 });
//! [camera-pose-from-Blender-at-location-1]
//! [camera-pose-from-Blender-at-location-2]
Mat c2Mo = new Mat(4, 4, MatType.CV_64F, new double[]{ 0.9659258723258972, -0.2588190734386444, 0.0, -1.5529145002365112,
-0.08852133899927139, -0.3303661346435547, -0.9396926164627075, -0.10281121730804443,
0.24321036040782928, 0.9076734185218811, -0.342020183801651, 6.130080699920654,
0, 0, 0, 1 });
//! [camera-pose-from-Blender-at-location-2]
//! [camera-intrinsics-from-Blender]
Mat cameraMatrix = new Mat(3, 3, MatType.CV_64F, new double[]{700.0, 0.0, 320.0,
0.0, 700.0, 240.0,
0, 0, 1 });
//! [camera-intrinsics-from-Blender]
//! [extract-rotation]
Mat R1 = c1Mo[new Range(0, 3), new Range(0, 3)];
Mat R2 = c2Mo[new Range(0, 3), new Range(0, 3)];
//! [extract-rotation]
//! [compute-rotation-displacement]
//c1Mo * oMc2
Mat R_2to1 = R1 * R2.T();
//! [compute-rotation-displacement]
//! [compute-homography]
Mat H = cameraMatrix * R_2to1 * cameraMatrix.Inv();
H /= H.At<double>(2, 2);
Console.WriteLine("H:\n", H);
//! [compute-homography]
//! [stitch]
Mat img_stitch = new Mat();
Cv2.WarpPerspective(img2, img_stitch, H, new Size(img2.Cols * 2, img2.Rows));
Mat half = img_stitch[new Rect(0, 0, img1.Cols, img1.Rows)];
img1.CopyTo(half);
//! [stitch]
Mat img_compare = new Mat();
Mat img_space = Mat.Zeros(new Size(50, img1.Rows), MatType.CV_8UC3);
Cv2.HConcat(img1, img_space, img_compare);
Cv2.HConcat(img_compare, img2, img_compare);
Cv2.ImShow("Compare images", img_compare);
Cv2.ImShow("Panorama stitching", img_stitch);
Cv2.WaitKey();
}
public void Run()
{
string image1 = @"I:\csharp\images\Blender_Suzanne1.jpg";
string image2 = @"I:\csharp\images\Blender_Suzanne2.jpg";
basicPanoramaStitching(image1, image2);
}
}
}
版权声明:本文为jimtien原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。