Makogan
Makogan

Reputation: 9540

OpenCV how to encode raw image information for imencode?

I am in C++.

Assume some mysterious function getData() returns all but only the pixel information of an image.

i.e a char* that points to only the pixel information with no metadata (no width, length, height, nor channels of any form)

Thus we have:

unsigned char *raw_data = getData();

Then we have another function that returns a structure containing the metadata.

eg:

struct Metadata {
    int width;
    int height;
    int channels;
    //other useful fields
}

I now need to prepend the object metadata in the correct way to create a valid image buffer.

So instead of [pixel1, pixel2, pixel3 ...]

I would have, for example [width, height, channels, pixel1, pixel2, pixel3...]

What is the correct order to prepend the metadata and are width, height and channels enough?

Upvotes: 0

Views: 910

Answers (1)

nayab
nayab

Reputation: 2380

You can use Mat constructor to create an image from data and meta data

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP); // documentation here

cv::Mat image = cv::Mat(height, width, CV_8UC3, raw_data);

type argument specifies the number of channels and data format. For example, typical RGB image data is unsigned char and the number of channels is 3 so its type = CV_8UC3

Available OpenCV Mat types are defined cvdef.h

Upvotes: 1

Related Questions