How to write cv::Mat to CArchive? (in the MFC function Serialize(CArchive& ar))

273 Views Asked by At

I'm trying to write matImage

cv::Mat matImage;
matImage.create(480, 640, CV_8U);

to a bmp-file (for example "test.bmp") through Serialize(CArchive& ar) in the MFC application.

This code doesn't work

void CApplDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        // TODO: add storing code here
        CFile* file = ar.GetFile();
        CString strFileName = file->GetFileName();

        CStringA strFileNameA(strFileName);

        cv::imwrite(strFileNameA.GetBuffer(), matImage);
    }
}

How to do this correctly?

UPDATE

This code is not good enough, but it works

void CApplDoc::Serialize(CArchive& ar)
{
    CFile* file = ar.GetFile();
    CString strFileName = file->GetFileName();
    CStringA strFileNameA(strFileName);

    if (ar.IsStoring())
    {
        std::vector<uchar> buff;
        cv::imencode(".bmp", matImage, buff);
        ar.Write(&buff[0], buff.size());
        buff.empty();
    }
    else
    {
        UINT size = (UINT)file->GetLength();
        std::vector<uchar> buff;
        buff.resize(size);
        ar.Read(&buff[0], size);
        matImage = cv::imdecode(buff, CV_LOAD_IMAGE_GRAYSCALE);
        buff.empty();
    }
}
0

There are 0 best solutions below