Cv2.GetRotationMatrix2D 实例
时间: 2023-05-27 19:01:45 浏览: 76
下面是使用cv2.GetRotationMatrix2D旋转图像的示例:
``` python
import cv2
# 加载图像
image = cv2.imread("image.jpg")
# 获取图像的中心点和旋转角度
(height, width) = image.shape[:2]
center = (width / 2, height / 2)
angle = 45
# 旋转图像
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(image, matrix, (width, height))
# 显示原始图像和旋转后的图像
cv2.imshow("Original Image", image)
cv2.imshow("Rotated Image", rotated_image)
cv2.waitKey(0)
```
在此示例中,我们首先加载一张图像。然后,使用cv2.GetRotationMatrix2D函数获取图像的中心点和旋转角度,其中旋转角度设置为45度。接下来,我们将使用cv2.warpAffine函数来旋转图像。最后,我们将显示原始图像和旋转后的图像。
这是使用cv2.GetRotationMatrix2D旋转图像时的一个基本示例。
相关问题
opencvsharp Cv2.GetRotationMatrix2D().Transform()实例
以下是使用OpencvSharp中Cv2.GetRotationMatrix2D().Transform()方法的示例:
```
using OpenCvSharp;
namespace OpencvSharpDemo
{
class Program
{
static void Main(string[] args)
{
Mat src = new Mat("test.jpg", ImreadModes.Color);
Mat dst = new Mat();
Point2f center = new Point2f(src.Width / 2f, src.Height / 2f);
Mat rotMat = Cv2.GetRotationMatrix2D(center, 45, 1); // 顺时针旋转45度,放缩因子为1
Cv2.WarpAffine(src, dst, rotMat, src.Size()); // 对图像进行仿射变换
Cv2.ImShow("src", src); // 显示原图
Cv2.ImShow("dst", dst); // 显示旋转后的图像
Cv2.WaitKey(0); // 等待按键
}
}
}
```
该示例加载名为"test.jpg"的图像,以其中心为旋转中心,将图像顺时针旋转45度,然后将旋转后的图像显示在屏幕上。其中Cv2.GetRotationMatrix2D()方法用于创建旋转的仿射变换矩阵,Cv2.WarpAffine()方法用于将图像应用仿射变换。
void Extract1DEdge::GetProfieMat() { if (m_mInputMat.empty()) { return; } if (m_mInputMat.channels() > 1) { cvtColor(m_mInputMat, m_mInputMat, COLOR_BGR2GRAY); } Mat RotateMat = getRotationMatrix2D(m_pdCenter, -m_dAngle, 1); warpAffine(m_mInputMat, m_mInputMat, RotateMat, m_mInputMat.size(), WARP_INVERSE_MAP); Mat newCenter = RotateMat * (Mat_<double>(3, 1) << m_pdCenter.x, m_pdCenter.y, 1); double x = newCenter.at<double>(0, 0); double y = newCenter.at<double>(1, 0); Mat M = (Mat_<double>(2, 3) << 1, 0, x - m_dLength * 0.5, 0, 1, y - m_dHeight * 0.5); warpAffine(m_mInputMat, m_mInputMat, M, Size2d(m_dLength, m_dHeight), WARP_INVERSE_MAP); }如何保持效果不变的情况下加速这段代码给个例程
可以考虑使用OpenCV提供的并行计算模块,如OpenCV的ParallelLoopBody类和ParallelFor函数,来加速这段代码。具体做法如下:
1. 头文件引入:
```cpp
#include <opencv2/core/parallel.hpp>
```
2. 利用ParallelLoopBody类重写GetProfieMat函数中需要并行处理的for循环,如下:
```cpp
class Extract1DEdge_Parallel : public cv::ParallelLoopBody
{
public:
Extract1DEdge_Parallel(Mat& inputMat, Point2d center, double angle, double length, double height)
: m_mInputMat(inputMat), m_pdCenter(center), m_dAngle(angle), m_dLength(length), m_dHeight(height)
{}
virtual void operator()(const cv::Range& range) const
{
for (int i = range.start; i < range.end; i++)
{
// 在这里写需要并行处理的代码
// 例如,对矩阵进行处理等
}
}
private:
Mat& m_mInputMat;
Point2d m_pdCenter;
double m_dAngle;
double m_dLength;
double m_dHeight;
};
```
3. 在GetProfieMat函数中调用ParallelFor函数,并传入ParallelLoopBody类的实例,如下:
```cpp
void Extract1DEdge::GetProfieMat()
{
if (m_mInputMat.empty())
{
return;
}
if (m_mInputMat.channels() > 1)
{
cvtColor(m_mInputMat, m_mInputMat, COLOR_BGR2GRAY);
}
Mat RotateMat = getRotationMatrix2D(m_pdCenter, -m_dAngle, 1);
warpAffine(m_mInputMat, m_mInputMat, RotateMat, m_mInputMat.size(), WARP_INVERSE_MAP);
Mat newCenter = RotateMat * (Mat_<double>(3, 1) << m_pdCenter.x, m_pdCenter.y, 1);
double x = newCenter.at<double>(0, 0);
double y = newCenter.at<double>(1, 0);
Mat M = (Mat_<double>(2, 3) << 1, 0, x - m_dLength * 0.5, 0, 1, y - m_dHeight * 0.5);
warpAffine(m_mInputMat, m_mInputMat, M, Size2d(m_dLength, m_dHeight), WARP_INVERSE_MAP);
// 调用ParallelFor函数
Extract1DEdge_Parallel parallelBody(m_mInputMat, m_pdCenter, m_dAngle, m_dLength, m_dHeight);
cv::parallel_for_(cv::Range(0, m_mInputMat.rows), parallelBody);
}
```
注意:并行计算的效果取决于计算任务的复杂性和硬件环境等因素,需要根据实际情况调整并行范围和线程数等参数,以达到最佳性能。