保存和加载向量< Mat> Qt& OpenCV的 [英] saving and loading vector<Mat> Qt & OpenCV

查看:178
本文介绍了保存和加载向量< Mat> Qt& OpenCV的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Qt& amp;中进行人脸识别使用不支持更新的FisherFaces识别器的openCV,因此我必须保存面部数据库以在进行任何更改后重新训练识别器.
这是我保存的代码:

I'm working on face recognition in Qt & openCV using the FisherFaces recognizer which doesn't support updating so i have to save the faces database to retrain the recognizer after any changes.
Here is my code for saving :

save(const std::vector* MatVect){
       QFile file("students_dataset.dat");
       file.open(QIODevice::WriteOnly);
       QDataStream out(&file);
       QVector qimgvect;
       for (size_t i = 0; i < MatVect->size(); ++i) 
       {
                cv::Mat matt = MatVect->at(i);
                QImage img((uchar*)matt.data, matt.cols, matt.rows, matt.step, QImage::Format_Indexed8);
                qimgvect.push_back(img);
        }
       out << qimgvect ;
       file.flush();
       file.close();
    } 

这是用于加载:

and this is for loading :

load(std::vector* MatVect)
    {
    QFile file("students_dataset.dat");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);
    QVector qimgvect;
    in >> qimgvect;
    for (size_t i = 0; i < qimgvect.size(); ++i) 
      {
        QImage img = qimgvect.at(i);
        cv::Mat matt = cv::Mat(70, 70, CV_8U, img.bits(), img.bytesPerLine());
        MatVect->push_back(matt);
      }
    file.close();
    return;
    } 

问题是我从文件中读取的不是我保存的

the problem is that what I read back from the file is not what i saved

那么这段代码到底有什么问题(希望不是全部)?
有没有更好/更容易的方法来保存向量?

感谢 Marek_R ,转换部分已修复,但现在使用QDataStream保存和加载是一个问题:

那是什么原因导致这些线路?

我已经尝试过了:
QimgVect公开并修饰为QDataStream部分: MatVect-> QimgVect QimgVect-> MatVect ,它确实可以正常工作,但是在添加QDataStream之后: MatVect-> QimgVect-> QDataStream QDataStream-> QimgVect-> MatVect 我得到上面显示的结果(垂直白线).
编辑
从qdatastream读取后将图像从 RGB32 转换为 Indexed8 得到以下结果:

so what's exactly wrong in this code (hope not all of it ) ?
is there a better/easier way of saving the vector ?
EDIT :
thanks to Marek_R the conversion part is fixed, but saving and loading with QDataStream is the problem now :

so what's causing those lines ?
EDIT :
I have tried this :
made QimgVect public and elemenated QDataStream part : MatVect-> QimgVect than QimgVect->MatVect and it did work fine, but after adding QDataStream : MatVect-> QimgVect->QDataStream and QDataStream->QimgVect->MatVect i get get the result shown above( vertical white lines ).
EDIT
converting the image from RGB32 to Indexed8 after reading from qdatastream gives the following result :

推荐答案

SO这是一个解决方案: 用于通过QDataStream保存灰度级Qimage并将其转换为Format_ARGB32然后保存,以进行加载将其转换回Format_Indexed8: 保存代码

SO Here is a solution : for saving grayscale Qimage through QDataStream convert it to Format_ARGB32 then save it, for loading convert it back to Format_Indexed8 : saving code

QFile file("students_dataset.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
//converting to ARGB32
foreach (QImage img, qimgvect) {
    img = img.convertToFormat(QImage::Format_ARGB32);
 }
// saving to QDataStream
out << qimgvect ;
file.flush();
file.close();

用于加载

QFile file("students_dataset.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QVector<QImage> qimgvect;
// loading images vector from QDatastream
in >> qimgvect;
// converting to grayscale
QVector<QRgb> grayscale;
for (int i = 0; i < 256; ++i) grayscale.append(qRgb(i, i, i));
for (int i, i < qimgvect.size(),i++) {  
    QImage img = qimgvect.at(i).convertToFormat(QImage::Format_Indexed8,grayscale);        
    qimgvect.push_back(img);
}

file.close();

我认为,即使QImage具有其他格式,也会遇到相同的问题,因为假定默认格式为Format_ARGB32

I think even QImage with other Formats will face the same problem since it is assumed that the default format is Format_ARGB32

这篇关于保存和加载向量&lt; Mat&gt; Qt&amp; OpenCV的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆