QT / OPENCV-调整图像的大小不适合特定尺寸

发布于 2025-01-27 04:00:17 字数 3634 浏览 3 评论 0原文

我在QT/OpenCV中制作了一个图像编辑器,您可以从文件资源管理器和灰度/自适应阈值/调整大小。

错误1:当我使用我的ImageProcessor :: Resize(int,int)方法调整加载的图像大小为(例如)600x600像素时,它可以正常工作。但是,当我将其更改为546x750像素时,图像会带有一个怪异的灰度。

错误2:当我想调整灰度/阈值图像大小时,它总是会得到一个与错误1相似的怪异灰度。

代码:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "resizer.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::Display(cv::Mat inputImage)
{
        QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_RGB888);
        scene->addPixmap(QPixmap::fromImage(image));
        ui->graphicsView->setScene(scene);
        ui->graphicsView->show();
}


void MainWindow::on_actionOpen_triggered()
{
    QString file = QFileDialog::getOpenFileName(this, "Open", "", "Images (*.jpg *.png)");
    std::string filename = file.toStdString();
    inputImage = cv::imread(filename);
    Display(inputImage);

    imgProc = new ImageProcessor(inputImage);
}


void MainWindow::on_pushButton_clicked() // Grayscale
{
    scene->clear();

    imgProc->mode = 1;
    inputImage = imgProc->Grayscale();

    QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_Grayscale8);
    scene->addPixmap(QPixmap::fromImage(image));
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();
}


void MainWindow::on_pushButton_2_clicked() // ADT
{
    scene->clear();

    imgProc->mode = 2;
    inputImage = imgProc->AdaptiveThreshold();

    QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_Grayscale8);
    scene->addPixmap(QPixmap::fromImage(image));
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();
}


void MainWindow::on_pushButton_3_clicked() // Resize
{
    scene->clear();

    Resizer resizer;
    resizer.exec();

    int newWidth = resizer.GetWidth();
    int newHeight = resizer.GetHeight();

    inputImage = imgProc->Resize(newWidth, newHeight);

    if(imgProc->mode == 1 || imgProc->mode == 2)
    {
        QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_Grayscale8);
        scene->addPixmap(QPixmap::fromImage(image));
        ui->graphicsView->setScene(scene);
        ui->graphicsView->show();
    }
    else
    {
        QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_RGB888);
        scene->addPixmap(QPixmap::fromImage(image));
        ui->graphicsView->setScene(scene);
        ui->graphicsView->show();
    }
}


imageprocessor.cpp

#include "imageprocessor.h"

ImageProcessor::ImageProcessor(cv::Mat inputImage)
{
    this->inputImage = inputImage;
}

cv::Mat ImageProcessor::Resize(int width, int height)
{
    cv::Mat resized;
    cv::resize(inputImage, resized, cv::Size(width, height), cv::INTER_LINEAR);
    return resized;
}

cv::Mat ImageProcessor::Grayscale()
{
    cv::Mat grayscaled;
    cv::cvtColor(inputImage, grayscaled, cv::COLOR_RGB2GRAY);
    return grayscaled;
}

cv::Mat ImageProcessor::AdaptiveThreshold()
{
    cv::Mat binarized, grayscaled;
    cv::cvtColor(inputImage, grayscaled, cv::COLOR_RGB2GRAY);
    cv::adaptiveThreshold(grayscaled, binarized, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 15, 11);
    return binarized;
}

I made an Image Editor in Qt / OpenCV where you can load the Image from the File explorer and grayscale/adaptive threshold/resize it afterwards.

Bug 1: When I resize the Loaded Image to (for example) 600x600 Pixels using my ImageProcessor::Resize(int, int) method, it works fine. But when I change it to like 546x750 Pixels, the Image has a weird grayscale.

Bug 2: When I want to resize my Grayscaled/Thresholded Image, it always gets a weird grayscale similiar to Bug 1.

Codes:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "resizer.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::Display(cv::Mat inputImage)
{
        QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_RGB888);
        scene->addPixmap(QPixmap::fromImage(image));
        ui->graphicsView->setScene(scene);
        ui->graphicsView->show();
}


void MainWindow::on_actionOpen_triggered()
{
    QString file = QFileDialog::getOpenFileName(this, "Open", "", "Images (*.jpg *.png)");
    std::string filename = file.toStdString();
    inputImage = cv::imread(filename);
    Display(inputImage);

    imgProc = new ImageProcessor(inputImage);
}


void MainWindow::on_pushButton_clicked() // Grayscale
{
    scene->clear();

    imgProc->mode = 1;
    inputImage = imgProc->Grayscale();

    QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_Grayscale8);
    scene->addPixmap(QPixmap::fromImage(image));
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();
}


void MainWindow::on_pushButton_2_clicked() // ADT
{
    scene->clear();

    imgProc->mode = 2;
    inputImage = imgProc->AdaptiveThreshold();

    QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_Grayscale8);
    scene->addPixmap(QPixmap::fromImage(image));
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();
}


void MainWindow::on_pushButton_3_clicked() // Resize
{
    scene->clear();

    Resizer resizer;
    resizer.exec();

    int newWidth = resizer.GetWidth();
    int newHeight = resizer.GetHeight();

    inputImage = imgProc->Resize(newWidth, newHeight);

    if(imgProc->mode == 1 || imgProc->mode == 2)
    {
        QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_Grayscale8);
        scene->addPixmap(QPixmap::fromImage(image));
        ui->graphicsView->setScene(scene);
        ui->graphicsView->show();
    }
    else
    {
        QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_RGB888);
        scene->addPixmap(QPixmap::fromImage(image));
        ui->graphicsView->setScene(scene);
        ui->graphicsView->show();
    }
}


imageprocessor.cpp

#include "imageprocessor.h"

ImageProcessor::ImageProcessor(cv::Mat inputImage)
{
    this->inputImage = inputImage;
}

cv::Mat ImageProcessor::Resize(int width, int height)
{
    cv::Mat resized;
    cv::resize(inputImage, resized, cv::Size(width, height), cv::INTER_LINEAR);
    return resized;
}

cv::Mat ImageProcessor::Grayscale()
{
    cv::Mat grayscaled;
    cv::cvtColor(inputImage, grayscaled, cv::COLOR_RGB2GRAY);
    return grayscaled;
}

cv::Mat ImageProcessor::AdaptiveThreshold()
{
    cv::Mat binarized, grayscaled;
    cv::cvtColor(inputImage, grayscaled, cv::COLOR_RGB2GRAY);
    cv::adaptiveThreshold(grayscaled, binarized, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 15, 11);
    return binarized;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2025-02-03 04:00:17

qimage :: format_rgb888是您定义的格式类型,意味着:

使用24位RGB格式(8-8-8)存储图像。

如果您的图像具有3个频道,则您的方式是正确的,除了添加以下内容:

QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_RGB888).rgbSwapped();

您需要在末尾添加rgbswapped(),因为QT以RGB顺序读取它,因为OpenCV给出了BGR。

如果要将灰度映像发送到GUI,则需要使用qimage :: format_grayscale8格式类型,这意味着:

使用8位灰度格式存储图像。

在这里是格式的清晰的共同介绍。

注意:使用OpenCV函数如何调整图像大小?共享resizer.h,我将相应地更新答案。

QImage::Format_RGB888 is the format type you defined means that:

The image is stored using a 24-bit RGB format (8-8-8).

If your image has 3 channels then your way is correct to continue, except adding this:

QImage image = QImage(inputImage.data, inputImage.cols, inputImage.rows, QImage::Format_RGB888).rgbSwapped();

You need to add at the end rgbSwapped() because Qt reads it in RGB order as OpenCV gives BGR.

If you want to send a gray scale image to GUI then you need to use QImage::Format_Grayscale8 format type, which means:

The image is stored using an 8-bit grayscale format.

Here is the clear cocumentation for the formats.

Note: How do you resize your image, by using OpenCV function ? Share resizer.h , I will update the answer accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文