QImage :: pixel和QImage :: setPixel坐标超出范围错误

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

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,originalImage).red()),(getRgbaPixel(i,originalImage).green()),originalImage).blue()),255));
}
}

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

}
solabc 回答:QImage :: pixel和QImage :: setPixel坐标超出范围错误

如果您尝试旋转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,fileName);
    inImage.save(str);

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

您需要确保尺寸正确,并且如果更改宽度和高度计数器(如我在上面的代码中所做的那样),则代码可以很好地复制图像。

本文链接:https://www.f2er.com/2973742.html

大家都在问