【问题标题】:QImage::pixel and QImage::setPixel coordinate out of range errorQImage::pixel 和 QImage::setPixel 坐标超出范围错误
【发布时间】:2020-03-29 20:58:52
【问题描述】:

我正在开发一个图像处理程序,虽然程序我不断收到这些消息,但这里有一些示例,但我收到了数百个示例,以至于程序无法完全执行。

QImage::setPixel: coordinate (1043968,0) out of range
QImage::pixel: coordinate (1043968,0) out of range

我查看了其他问题,但似乎在我的代码中找不到错误,不幸的是我有大约 8 个函数,但我认为我将其缩小到可能存在问题的一个。这是一个假设旋转给定图像的功能,我认为这可能是根据我审查过的其他问题导致错误的原因,但我看不到它。如果我能得到任何帮助,我将不胜感激,或者如果这个功能很好,我如何在不发 8 个不同帖子的情况下询问其他人,谢谢!

    void makeRotate(QImage originalImage){
QImage inImage = originalImage;    // Copies the original image into a new QImage object.


int width = originalImage.width();
int height = originalImage.height();

//a double for loop

//first loop through the HEIGHT OF inImage  (width of newImage)
for (int i = 0; i < height; i++){
//loop through the WIDTH OF inImage  (height of newImage)
for (int j = 0; j < width; j++){
    //set the pixel
    inImage.setPixel(i , j,(new QColor (getRgbaPixel(i, j, originalImage).red()), (getRgbaPixel(i, j, originalImage).green()), (getRgbaPixel(i, j, originalImage).blue()), 255));
}
}

inImage.save("../Images/rotate.png");

}

【问题讨论】:

  • 这段代码不应该编译。 setPixel 需要一个 QColor 值,而不是一个 QColor 指针。 new QColor 是一个指针。

标签: c++ qt clion qimage


【解决方案1】:

如果你尝试旋转 90 度,这是代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QtDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                    "/home",
                                                    tr("Images (*.png *.xpm *.jpg *.jpeg *.png)"));
    QImage image = QImage(fileName);
    makeRotate(image);
}

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

void MainWindow::makeRotate(QImage originalImage)
{
    QImage inImage = originalImage;    // Copies the original image into a new QImage object.


    int width = originalImage.width();
    int height = originalImage.height();

    QTransform rotate;
    rotate.rotate(90);
    inImage = inImage.transformed(rotate);


    //a double for loop

    //first loop through the HEIGHT OF inImage  (width of newImage)
    for (int i = 0; i < width; i++)
    {
        //loop through the WIDTH OF inImage  (height of newImage)
        for (int j = 0; j < height; j++)
        {
            //set the pixel
            QRgb rgb = originalImage.pixel(i, j);
            inImage.setPixel(j, i, (rgb));
        }
    }
    QString str = QFileDialog::getSaveFileName(this, tr("Open File"), fileName);
    inImage.save(str);

    qDebug() << inImage.size();
}

如果您更改宽度和高度计数器(就像我在上面的代码中所做的那样),您需要确保测量值正确,并且您的代码可以正常复制图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多