Ask Your Question
0

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

Hi,

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 img32bit.data is uchar*, and not float*. What is img32bit.data 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 img32bit.data is encoded.

Thank you for your help !

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

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

kbarni gravatar image

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

The img32bit.data 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*)img32bit.data;
float pix1015 = *(ptrF+10*imW+15); //get the value of pixel (10,15)
pix1015 = img32bit.at<float>(10,15); //same thing with at() function

For 8 bit RGB images:

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

Comments

Hello,

Thank you for your answer. It is more clear. Just one question more. When we send image through TCP socket, we send img32bit.data, 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. Mat.data 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
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 46 times

Last updated: May 27