我有一组从右侧拍摄的图像,如图像1。我需要围绕垂直轴旋转它,并改变它像图像2。
这个仿射变换对image1到image2的转换有用吗?我用了仿射变换,但没有得到想要的结果。我该怎么办?我用这部分代码进行图像变换。
cv::Mat shearedImage;
cv::Point2f srcPoints[3] = {cv::Point2f(0, 0), cv::Point2f(resizedImage.cols, 0), cv::Point2f(0, resizedImage.rows)};
cv::Point2f dstPoints[3] = {cv::Point2f(0, 0), cv::Point2f(resizedImage.cols, 0), cv::Point2f(0, resizedImage.rows)};
cv::Mat transformMatrix = cv::getAffineTransform(srcPoints, dstPoints);
cv::warpAffine(resizedImage, shearedImage, transformMatrix, resizedImage.size());
下面是Qt中的完整代码
#include "mainwindow.h"
#include <QApplication>
#include <opencv2/opencv.hpp>
#include <QDebug>
#include <QLabel>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
// Load the original image
cv::Mat originalImage = cv::imread("Pic1.jpg");
// Crop the image
cv::Rect roi(200, 300, 1350, 850);
cv::Mat croppedImage = originalImage(roi);
// Resize the cropped image
cv::Mat resizedImage;
cv::resize(croppedImage, resizedImage, cv::Size(), 0.4, 0.4);
// Shear the resized image
cv::Mat shearedImage;
cv::Point2f srcPoints[3] = {cv::Point2f(0, 0), cv::Point2f(resizedImage.cols, 0), cv::Point2f(0, resizedImage.rows)};
cv::Point2f dstPoints[3] = {cv::Point2f(0, 0), cv::Point2f(resizedImage.cols, 0), cv::Point2f(0, resizedImage.rows)};
cv::Mat transformMatrix = cv::getAffineTransform(srcPoints, dstPoints);
cv::warpAffine(resizedImage, shearedImage, transformMatrix, resizedImage.size());
// Display the shear image
cv::namedWindow("Transformed Image");
cv::imshow("Transformed Image", shearedImage);
MainWindow w;
w.show();
return a.exec();
}
1条答案
按热度按时间tjrkku2a1#
我搜索了更多,发现了更多的细节,基于this Question,我应该使用
cv::warpPerspective
而不是仿射变换。这里是输出和代码: