OpenCV和Qt C++中的图像转换

w3nuxt5m  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(117)

我有一组从右侧拍摄的图像,如图像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();
}
tjrkku2a

tjrkku2a1#

我搜索了更多,发现了更多的细节,基于this Question,我应该使用cv::warpPerspective而不是仿射变换。


这里是输出和代码:

#include "mainwindow.h"
    #include <QApplication>
    #include <opencv2/opencv.hpp>
    #include <QDebug>
    #include <QLabel>
    
    cv::Point2f srcPoints[4];
    int pointCounter = 0;
    
    void mouseCallback(int event, int x, int y, int flags, void* userdata)
    {
        if (event == cv::EVENT_LBUTTONDOWN && pointCounter < 4)
        {
            srcPoints[pointCounter] = cv::Point2f(x, y);
            pointCounter++;
            qDebug() << "Selected point: (" << x << ", " << y << ")";
        }
    }
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
    
        // Load the original image
        cv::Mat Image = cv::imread("image1.jpg");
        if (Image.empty())
        {
            qDebug() << "Failed to load image";
            return -1;
        }
    
        // Resize the cropped image
        cv::Mat originalImage;
        cv::resize(Image, originalImage, cv::Size(), 0.9, 0.9);
    
        // Create a window to display the image
        cv::namedWindow("Original Image");
        cv::imshow("Original Image", originalImage);
    
        // Set mouse callback
        cv::setMouseCallback("Original Image", mouseCallback);
    // Display instructions to the user
//    cv::putText(originalImage, "Select points from left to right and clockwise", cv::Point(10, 30),
//                cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 2);
    cv::imshow("Original Image", originalImage);
    
        // Wait until 4 points are selected
        while (pointCounter < 4)
        {
            char key = cv::waitKey(10);
            if (key == 27)  // Esc key
            {
                qDebug() << "Selection aborted";
                return 0;
            }
        }
        cv::destroyWindow("Original Image");
    
        // Generate the perspective transform matrix
        cv::Point2f dstPoints[4] = {cv::Point2f(0, 0), cv::Point2f(originalImage.cols, 0), cv::Point2f(originalImage.cols, originalImage.rows), cv::Point2f(0, originalImage.rows)};
        cv::Mat perspectiveMatrix = cv::getPerspectiveTransform(srcPoints, dstPoints);
    
        // Apply the perspective transform
        cv::Mat transformedImage;
        cv::warpPerspective(originalImage, transformedImage, perspectiveMatrix,  originalImage.size());
    
        // Display the transformed image
        cv::namedWindow("Transformed Image");
        cv::imshow("Transformed Image", transformedImage);
    MainWindow w;
        w.show();
        return a.exec();
    }

相关问题