#region 图像旋转
static void Main(string[] args)
{
//Mat src = new Mat("lenna.png", ImreadModes.AnyColor | ImreadModes.AnyDepth);
//Cv2.ImShow("lenna.png", src);
//Mat M = Cv2.GetRotationMatrix2D(new Point2f(src.Width/2,src.Width/2),30,1.0);
//Mat output = new Mat();
//Cv2.WarpAffine(src, output, M, src.Size(),InterpolationFlags.Linear,BorderTypes.Constant,new Scalar (255,255,0));
//Cv2.ImShow("output", output);
//Cv2.WaitKey();
Mat src = new Mat("lenna.png", ImreadModes.AnyColor | ImreadModes.AnyDepth);
Cv2.ImShow("lenna.png", src);
Mat M = Cv2.GetRotationMatrix2D(new Point2f(src.Width / 2, src.Height / 2), 30, 1.0);
Mat output = new Mat();
double cos = M.Get<double>(0, 0);
double sin = M.Get<double>(0, 1);
int nw = (int)(cos * src.Width + sin * src.Height);
int nh = (int)(sin * src.Width + cos * src.Height);
M.Set<double>(0, 2, M.Get<double>(0, 2) + (nw/2 - src.Width/2));
M.Set<double>(1, 2, M.Get<double>(1, 2) + (nh / 2 - src.Height / 2));
Cv2.WarpAffine(src, output, M, new Size(nw, nh), InterpolationFlags.Linear, BorderTypes.Constant, new Scalar(255, 255, 0));
Cv2.ImShow("output", output);
Cv2.WaitKey();
}
#endregion
OpenCVSharp 笔记21 图像旋转
最新推荐文章于 2024-10-12 22:27:28 发布
这段代码演示了如何使用OpenCV的GetRotationMatrix2D和WarpAffine函数来旋转图像。首先读取图像'lenna.png',然后计算旋转矩阵,考虑到旋转中心和图像尺寸的变化,确保旋转后的图像完全包含在输出矩阵中。最后,显示旋转前后的图像并等待用户按键。
摘要由CSDN通过智能技术生成