FreeImage Lib: convert raw image to cv::Mat
Hi there,
are there any FreeImage users in this cmmunity? I am posting here because the FreeImage users were sadly not able to help me yet... I am trying to convert a FreeImage RAW image (Sony ARW File) data structure with the following specs:
type: FIT_RGB16 bits per pixel: 48
to a cv::Mat data structure. Besides a lot of tests, I tried the following:
//image dimensions
int cols = FreeImage_GetWidth(rawImage);
int rows = FreeImage_GetHeight(rawImage);
FIRGB16* bits = (FIRGB16* )FreeImage_GetScanLine(rawImage, 0);
cv::Mat preview(rows, cols, CV_16UC3, bits, cols * 3);
There is always a segmentation fault. Can you tell me what I am doing wrong? Since a 48bit image is not so special I really don't understand what I am doing wrong.
Any tips are really appreciated! JohannesZ
when does the segfault happen ?
this Mat constructor works with a 'borrowed' pointer to the pixel data. if
FIRGB16* bits
goes out of scope, your Mat will be invalid.try to work with a clone(), to achieve a 'deep' copy:
Hi Berak, thanks for your comment! The seg fault happens right after the execution of the statement, this means I have no chance to execute your suggesion.
A "normal" 8 bit image, eg. 24 bit for RGB, works without any problems:
do you have to give a row-stride ?
try either to omit that, else, imho, the stride is
cols*channels*elemSize
(so yours is too short in the 16bit case.)sorry, I don't understand what you mean exactly with "row stride". Is this some kind of step parameter?
ah, step, ofc. couldn't remember the correct word.
I've never used FreeImage, but I think you can use the FreeImage.GetBits method to get the data pointer and the FreeImage.GetPitch method for the step calculation.
So maybe something like: