[英]QImage::pixel and QImage::setPixel coordinate out of range error
我正在開發一個圖像處理程序,雖然我不斷收到這些消息,但這里有一些例子,但我得到了數百個,以至於程序沒有完全執行。
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();
}
如果您更改寬度和高度計數器(就像我在上面的代碼中所做的那樣),您需要確保測量值正確並且您的代碼可以正常復制圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.