efficiently fill cv::Mat
Dear users,
I'm currently investigating the use of the cv::Mat class as a storage basis for a software dedicated to sequences of images.Basically, I have a binary file containing some raw data that I need to access, process and finally display. Up to now, I was using a simple std::vector but I'm currently thinking of replacing this vector by a cv::Mat object to benefit from all the great tools openCV provides. I'm currently facing a performance issue regarding to time needed to read a file and fill the cv::Mat object.
Here are the method I have written to fill the cv::Mat object :
void ImageReader::readData(cv::Mat &mat)
{
std::ifstream in(m_file, std::ios::in | std::ios::binary);
in.seekg(10);
in.read((char*)(mat.data), 2949120*sizeof(std::uint16_t));
in.close();
}
And here is the one the std::vector
void ImageReader::readDataStd(std::vector<std::uint16_t> &vec)
{
std::ifstream in(m_file, std::ios::in | std::ios::binary);
in.seekg(10);
in.read((char*)(vec.data()), 2949120*sizeof(std::uint16_t));
in.close();
}
The file I read basically contains one small header then followed by the data (1920 * 1536 pixels, each pixel is a uint16 in this example). Although the pieces of code are basically the same, I do not get the same performance on my machine : the method that fills the cv::Mat is almost twice slower than the one filling the std::vector (8ms versus 4.2ms).
Is this result logical? Do I make something wrong? Why do I get such a difference? Is there any tips that can help me to reduce the filling time?
I'm new to C++, so please be kind if the answer is obvious ;-)
Best regards,
Vincent