我简化了代码,它是这样的:
Mat mat = imread("xxx.jpg"); //Successfully read the image, confirmed by cvShowImage.
if (mat.empty())
{
qDebug() << "Couldn't load image";
return;
}
Mat cpy = mat.clone();
cvtColor(mat,cpy,CV_BGR2RGB);
QImage image(cpy.data, cpy.cols, cpy.rows, cpy.step, QImage::Format_RGB888);
try {
pm = QPixmap::fromImage(image); //crash line
} catch(std::exception const &ex){
qDebug()<<ex.what();
}
然而程序崩溃没有任何调试日志..我尝试了很多图像,结果是一样的。我试图找到“堆栈跟踪”,它似乎给了segfault ..
答案 0 :(得分:0)
好的..只需将转换代码更改为
QImage image(cpy.data, cpy.cols, cpy.rows, cpy.step, QImage::Format_RGB888);
image = image.rgbSwapped();
解决了它。实际上我不知道为什么要修复它。也许是因为我应该使用COLOR_BGR2RGB而不是CV_BGR2RGB ..
答案 1 :(得分:0)
QPixmap.fromImage中发生了类似的崩溃。通过将图像调整为与4个字节对齐的大小来找到解决方法
aligned = cv2.resize(img, (img.shape[1]//4*4, img.shape[0]//4*4), fx=0, fy=0, interpolation=cv2.INTER_NEAREST)
rgb = cv2.cvtColor(aligned, cv2.COLOR_BGR2RGB)
qimage = QImage(rgb.data, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)