简体   繁体   English

尽管我正在防止QImage出现错误

[英]QImage is getting an error eventhough I'm preventing it

I'm playing around with QImage and QGraphics view. 我在玩QImage和QGraphics视图。 I was trying to calculate the euclidean distance between two images, I know it's slow but it doesn't matter, and i'm getting this error 我正在尝试计算两个图像之间的欧几里得距离,我知道它很慢,但是没关系,并且出现此错误

ASSERT failure in QVector<T>::at: "index out of range", file c:\work\build\qt5_workdir\w\s\qtbase\include\qtcore\../../src/corelib/tools/qvector.h, line 377

whenever I got through these lines 每当我通过这些线

for(int row = 0; row < 128 ; row++){
    for(int col = 0; col < 128; col++){
        if(this->Imagem->valid(row, col)){
            qDebug() << "1";
            this->Imagem->pixel(row, col);
        }
        else
            qDebug() << "2";
    }
}

It always outputs "1" on terminal and crashes. 它始终在终端上输出“ 1”并崩溃。 I'm declaring the image with 我在声明图像

this->Imagem = new QImage(128, 128, QImage::Format_Indexed8);
this->Imagem->fill(QColor(Qt::black).rgb());

and i'm even checking if the points are within the boundaries of the image and it clearly is. 我什至在检查这些点是否在图像的边界内,并且显然是。

Format_Indexed8 uses the manually defined color table where each index represents a color. Format_Indexed8使用手动定义的颜色表,其中每个索引代表一种颜色。 You have to set the color table for your image before manipulating its pixels: 您必须先设置图像的颜色表,然后才能处理其像素:

QVector<QRgb> color_table;
for (int i = 0; i < 256; ++i) {
    color_table.push_back(qRgb(i, i, i)); // Fill the color table with B&W shades
}
Imagem->setColorTable(color_table);

Or you can manually set each index for the current color table: 或者,您可以手动设置当前色表的每个索引:

Imagem->setColorCount(4); // How many colors will be used for this image
Imagem->setColor(0, qRgb(255, 0, 0));   // Set index #0 to red
Imagem->setColor(1, qRgb(0, 0, 255));   // Set index #1 to blue
Imagem->setColor(2, qRgb(0, 0, 0));     // Set index #2 to black
Imagem->setColor(3, qRgb(255, 255, 0)); // Set index #3 to yellow
Imagem->fill(1); // Fill the image with color at index #1 (blue)

As you can see, Format_Indexed8 pixel values represent not RGB colors but the index values (which in turn represent the colors you set in the color table). 正如你所看到的,Format_Indexed8像素值代表不是 RGB颜色,但指数值(这反过来又代表你在颜色表中设置的颜色)。

If you don't want to deal with color tables, you can simply use another format such as Format_RGB32 . 如果您不想处理颜色表,则可以简单地使用其他格式,例如Format_RGB32

Note that QImage::valid expects (col, row) , not (row, col) (ie the first parameter is X , the second Y ); 注意, QImage::valid期望(col, row) ,而不是(row, col) (即第一个参数是X ,第二个Y ); this shouldn't however make a difference for a 128x128 image. 不过,对于128x128的图片,这应该不会有所不同。

May be the object you are accessing has already been destroyed (for example because your handling of ownership is buggy), but it's hard to tell without seeing more code. 可能是您正在访问的对象已被销毁(例如,由于您对所有权的处理有问题),但是很难看到更多的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C++ curl 代码中设置授权承载标头? 我得到的授权不足,即使它在命令行上工作 - How do I set authorization bearer header in C++ curl code? I'm getting insufficient authorization, eventhough it works at the command line 我收到警告,阻止了我运行程序,我不确定如何修复它 - I'm getting a warning that is preventing me from running my program and I'm not sure how to fix it 我收到错误消息:Missing; *之前,但我没有错过 - I'm getting error: Missing ; before *, but I'm not missing it 我在 for 循环和 if 语句中遇到错误: - i'm getting an error in for loop and if statements: 我在这里遇到链接错误,为什么? - I'm getting a linking error here, why is that? 我收到“错误:字符串未在此范围内声明” - I'm getting an "Error: string was not declared in this scope" 无法理解我收到的错误消息 - Not understanding the error message I'm getting 我收到调试断言失败的错误? - I'm getting an error that the debug assertion failed? 不明白为什么我收到错误消息 - Not understanding why I'm getting error message 将项目编译为静态lib(未定义引用)时,可以在代码中使用QImage吗? - Can I use QImage in my code while I'm compiling my project as static lib (undefined reference)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM