Ask Your Question

what contains cv::Mat data

asked 2020-05-26 08:44:11 -0500

NL37 gravatar image

updated 2020-05-26 21:04:44 -0500

supra56 gravatar image


Actually, I handle CV_8UC1 and CV_32FC1. For example, I have :

cv::Mat img32bit = cv::imread(path, 2) ;

The type of img32bit is CV_32FC1. The type of is uchar*, and not float*. What is exaclty ? I ask because I transfer 32 bit images through socket. The client runs on windows, and we can only send/receive uchar* with windows.

It works well, I can transfer 32-bit images without lose informations, but I need to understand how is encoded.

Thank you for your help !

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-05-27 03:59:33 -0500

kbarni gravatar image

updated 2020-05-27 04:06:29 -0500

The is a generic pointer to the image data. Its content depends on the image type. Just cast it to the desired type to use it. For example if it's float (CV_32FC1):

int imW = img32bit.cols;
float *ptrF = (float*);
float pix1015 = *(ptrF+10*imW+15); //get the value of pixel (10,15)
pix1015 =<float>(10,15); //same thing with at() function

For 8 bit RGB images:

Vec3b *ptrRGB = (Vec3b*);
*(ptrRGB+15*imW+20) = Vec3b(0,255,255); //set pixel (15,20) to yellow
edit flag offensive delete link more



Thank you for your answer. It is more clear. Just one question more. When we send image through TCP socket, we send, which is uchar*. To rebuild the image, we just have to do Mat img(height, width, CV_32FC1, sockData);.

Can you explain what the data contain exactly ? I understand that is a generic pointer, but how it works to rebuild the image only with sockData, which is uchar through socket ?

NL37 gravatar imageNL37 ( 2020-06-02 00:53:51 -0500 )edit

Yes, you can rebuild an image like that. contains the image data, only in C++ you must give a type (uchar*) as there is no whatever the image type is* declaration in C++.

But you can cast a pointer type to another type: float *p1; uchar *p2; p1=(float*)p2;

kbarni gravatar imagekbarni ( 2020-06-02 02:13:27 -0500 )edit

Question Tools

1 follower


Asked: 2020-05-26 08:44:11 -0500

Seen: 194 times

Last updated: May 27 '20