gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:03:06 27 4
gpt4 key购买 nike

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

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");

}

最佳答案

如果你尝试旋转 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();
}

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

关于c++ - QImage::pixel 和 QImage::setPixel 坐标超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59186495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com