Ask Your Question

Revision history [back]

Here is my version of your functions and test code. I get very similar results for the Mat and vector reading using fstream. I do get faster times for the Mat reading using stdio functions fseek, fread

output:

ReadFileMatStdio time = 0.00337 s

ReadFileMatStream time = 0.00546 s

ReadFileVector time = 0.00556 s

string WindowName = "OutputWindow";


void WriteImage(
    const Size& size,
    const int pixelDepthBits,
    const int headerSizeBytes,
    const string& filename
) {
    int pixelType = CV_MAKETYPE(pixelDepthBits/8, 1);
    Mat img(size, pixelType, Scalar(0));
    // draw something to see
    arrowedLine(img, Point(size.width / 4, size.height / 4), Point(size.width / 4 * 3, size.height / 4 * 3), Scalar(0xffff), 5);

    vector<char> header(headerSizeBytes, char(0xFF));

    FILE* fp = fopen(filename.c_str(), "wb");
    fwrite(header.data(), sizeof(char), headerSizeBytes, fp);
    fwrite(img.data, sizeof(char), pixelDepthBits / 8 * img.size().area(), fp);
    fclose(fp);

    //namedWindow(WindowName);
    //imshow(WindowName, img);
}

void ReadFileMatStdio(
    Mat& result,
    const Size& size,
    const int pixelDepthBits,
    const int headerSizeBytes,
    const string& filename
) {
    FILE* fp = fopen(filename.c_str(), "rb");
    fseek(fp, headerSizeBytes, 0);
    fread(result.data, sizeof(char), pixelDepthBits / 8 * result.size().area(), fp);
    fclose(fp);
}

void ReadFileMatStream(
    Mat& result,
    const Size& size,
    const int pixelDepthBits,
    const int headerSizeBytes,
    const string& filename
) {
    std::ifstream in(filename, std::ios::in | std::ios::binary);
    in.seekg(headerSizeBytes);
    in.read((char*)(result.data), size.area() * pixelDepthBits / 8);
    in.close();
}

void ReadFileVector(
    std::vector<std::uint16_t>& vec,
    const Size& size,
    const int pixelDepthBits,
    const int headerSizeBytes,
    const string& filename
    ) {
    std::ifstream in(filename, std::ios::in | std::ios::binary);
    in.seekg(headerSizeBytes);
    in.read((char*)(vec.data()), size.area() * pixelDepthBits/8);
    in.close();
}

int main() {

    Size size(1920, 1536);
    const int pixelDepthBits = 16;
    const int headerSizeBytes = 10;
    const string& filename = "test.bin";

    int pixelType = CV_MAKETYPE(pixelDepthBits / 8, 1);
    Mat img(size, pixelType);

    int64 beg, end;
    double timeSec;

    //WriteImage(size, pixelDepthBits, headerSizeBytes, filename);


    beg = getTickCount();
    ReadFileMatStdio(img, size, pixelDepthBits, headerSizeBytes, filename);
    end = getTickCount();
    timeSec = (end - beg) / getTickFrequency();
    printf("ReadFileMatStdio time = %.5f s\n\n", timeSec);
    /* */

    namedWindow(WindowName);
    imshow(WindowName, img);
    while (waitKey(0) < 0) {
    }

    beg = getTickCount();
    ReadFileMatStream(img, size, pixelDepthBits, headerSizeBytes, filename);
    end = getTickCount();
    timeSec = (end - beg) / getTickFrequency();
    printf("ReadFileMatStream time = %.5f s\n\n", timeSec);

    namedWindow(WindowName);
    imshow(WindowName, img);
    while (waitKey(0) < 0) {
    }

    vector<uint16_t> vec(size.area(), 0);
    beg = getTickCount();
    ReadFileVector(vec, size, pixelDepthBits, headerSizeBytes, filename);
    end = getTickCount();
    timeSec = (end - beg) / getTickFrequency();
    printf("ReadFileVector time = %.5f s\n\n", timeSec);
    img = Mat(size, pixelType, vec.data());
    /* */

    namedWindow(WindowName);
    imshow(WindowName, img);
    while (waitKey(0) < 0) {
    }


    return 0;