This feels like a really easy question, but I have not been able to find the answer to it.
I have a function which reads a (binary) file and feeds the content into an openCV image. Currently the file is always of the "unsigned char" datatype but I would like to expand the support to other datatypes. Preferable as an argument to the function.
I'm not very experienced with C++ but after googling around this feels like something which should be done with templates, but I am really unsure how to implement it.
cv::Mat ReadImage(const char * filename, int dataTypeSize, int imageWidth)
{
auto read_image = fopen(filename, "rb");
if (read_image == nullptr)
{
printf("Image Not Found\n");
return cv::Mat();
}
fseek(read_image, 0, SEEK_END);
int fileLen = ftell(read_image);
fseek(read_image, 0, SEEK_SET);
auto pre_image = static_cast<unsigned char *>(malloc(fileLen));
auto data = fread(pre_image, 1, fileLen, read_image);
// Printed and verify the values
//printf("File Size %d\n", fileLen);
//printf("Read bytes %zd\n", data);
auto width = imageWidth;
auto height = fileLen / dataTypeSize / imageWidth;
fclose(read_image);
vector<unsigned char> buffer(pre_image, pre_image + data);
auto img = cv::Mat(height, width, CV_64F, pre_image);
//printf("Image rows %d\n", img.rows);
//printf("Image cols %d\n", img.cols);
return img;
}