[英]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.