Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 即使I';我在阻止它_C++_Qt_Qgraphicsview_Qimage - Fatal编程技术网

C++ 即使I';我在阻止它

C++ 即使I';我在阻止它,c++,qt,qgraphicsview,qimage,C++,Qt,Qgraphicsview,Qimage,我正在玩QImage和QGraphics view。我试图计算两幅图像之间的欧几里德距离,我知道这很慢,但这并不重要,我得到了这个错误 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 我甚至在检查这些点是否在图像的边界内

我正在玩QImage和QGraphics view。我试图计算两幅图像之间的欧几里德距离,我知道这很慢,但这并不重要,我得到了这个错误

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

我甚至在检查这些点是否在图像的边界内,而且很明显是这样

注意
QImage::valid
期望
(列,列)
,而不是
(列,列)
(即第一个参数是X,第二个参数是Y);但是,这对128x128图像不会产生任何影响


可能是您正在访问的对象已被销毁(例如,因为您对所有权的处理有缺陷),但如果没有看到更多代码,就很难判断。Format_Indexed8使用手动定义的颜色表,其中每个索引表示一种颜色。在操作图像像素之前,必须为图像设置颜色表:

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);
正如您所看到的,Format_Indexed8像素值表示的不是RGB颜色,而是索引值(依次表示在颜色表中设置的颜色)


如果您不想处理颜色表,您可以简单地使用另一种格式,例如格式\u RGB32

它在
this->Imagem->pixel(行,列)崩溃从计算行和列崩溃时的值开始。当您在调试器中运行它时,这和Imagem是否也有效?这个循环中的Imagem->size()是什么?我只需要更改我的全部代码。doneI在
this->Imagem->fill(QColor(Qt::black).rgb())上用黑色像素填充了整个图像,但没有多少内容
格式索引8
必须用索引值而不是颜色值填充。这就是为什么需要颜色表,其中每个索引表示一种颜色。例如,
Imagem->fill(0)
的意思不是“用黑色填充图像”,而是“用0索引填充图像,并从颜色表中获取0索引处的颜色”。试试答案中的代码。
this->Imagem = new QImage(128, 128, QImage::Format_Indexed8);
this->Imagem->fill(QColor(Qt::black).rgb());
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);
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)