Wednesday 14 October 2020

How to save opencv Mat data and read in python?

#include <opencv2/hdf.hpp>
using namespace std;

static void write_multiple_channels(cv::Mat data)
{
    std::string filename = "data.h5";
    std::string parent_name = "/data";
    std::string dataset_name = parent_name + "/two_channels";
    // prepare data
    for (size_t i = 0; i < data.total()*data.channels(); i++)
        ((float*) data.data)[i] = (float)i;
    cv::Ptr<cv::hdf::HDF5> h5io = cv::hdf::open(filename);
    // first we need to create the parent group
    if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
    // create the dataset if it not exists
    if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name);
    // the following is the same with the above function write_root_group_single_channel()
    h5io->dswrite(data, dataset_name);
    cv::Mat expected;
    h5io->dsread(expected, dataset_name);
    double diff = norm(data - expected);
    CV_Assert(abs(diff) < 1e-10);
    h5io->close();
}

===

import h5py    
import numpy as np    
h5data = h5py.File("data.h5",'r')  
temp = h5data.get('/data/two_channels')
temp = np.array(temp)

import pdb; pdb.set_trace()

 

No comments:

Post a Comment