Ask Your Question

Type issues

asked 2020-03-23 10:16:06 -0500

NL37 gravatar image


Actually, I'm trying to sum images.

That is just a simple example, you're gonna understand my issue :

Mat src ;
Mat datasrc1 ;
src = imread("/home/jetson/Downloads/wd-wallhaven-r2g7rm.jpg", 0);
int size_reshape = src.cols * src.rows ;
datasrc1 = src.reshape(0,1) ;
unsigned char * A ;
unsigned char * B ;
float * E ;
A =;
E = new float char[size_reshape];
for (int i = 0 ; i<size_reshape ; i++){
    E[i] = (float)A[i] ;
cv::Mat Result(src.rows, src.cols, CV_16FC1, E) ;
imwrite("/home/jetson/Desktop/blabla.jpeg", Result) ;

The image that I write is unreadable. I don't want to do a simple conversion, that is a sample of my work. I need to sum several image in 16bit images, and divide. As I don't have to loose informations, I need to use float Mat.

Thank you for your help,

edit retag flag offensive close merge delete



A float is 32-bits. JPEG most likely cannot handle floating point images.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-03-23 10:25:02 -0500 )edit

Yes but 16_FC1 is in half precision. Am I wrong ?

NL37 gravatar imageNL37 ( 2020-03-23 10:55:13 -0500 )edit

You are pointing a cv::Mat header at 32-bit data and inpterpreting it as 16-bit. How is that going to produce sensible output? And btw, your code above will not compile. E = new float char[size_reshape];?

Der Luftmensch gravatar imageDer Luftmensch ( 2020-03-23 13:23:17 -0500 )edit

Am I wrong ?

wrong about anything being float (of any depih) here for sure, this is NOT matlab. if you use imread() with ".jpg" - you get a CV_8U image

and no, you cannot imwrite() float data as jpg here

berak gravatar imageberak ( 2020-03-23 16:37:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-03-23 11:14:05 -0500

juanmamdp gravatar image

The first thing you have to keep in mind is that you have to separate the "image" from your "data". You will use the "image" to display it or save it to disk and usually don't need much precision. The "data" you need to process, depending on the problem, does need more precision.

Another thing to take into account is how much accuracy you need, if it is not too much you could multiply your data by 1000, and if you work with integers it would be like using values with three decimals.

Finally, if you need to use floats you must do the conversion yourself so that the result is correct. This will depend on the scale you are using (for example: pix/255)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-03-23 10:16:06 -0500

Seen: 100 times

Last updated: Mar 23 '20