Reputation: 11753
I understand how to create a opencv Mat object supposed the pointer to the image pixels as well as some other auxiliary information is given. For example:
int width = 100;
int height = 200;
unsigned char *pPixel = new unsigned char [width*height];
cv::Mat myMat(height,width, CV_8UC1,pPixel);
delete []nPixel;
My next question is: suppose myMat already exists as an empty cv::Mat object, and in this case how can we assign image information to the empty myMat object? This can happen in the following function:
void create_mat(int width, int height, int band_num, unsigned char *pPixel, cv::Mat &obj)
{
// adjust obj based on width, height and pPixel
}
EDIT:Possible solutions:
void create_mat(int width, int height, int band_num, unsigned char *pPixel, cv::Mat &obj)
{
obj.data = pPixel;
obj.rows = height;
obj.cols = width;
// how to assign image data type?
}
Upvotes: 0
Views: 1083
Reputation: 6420
Use the same constructor:
void create_mat(int width, int height, int band_num, unsigned char *pPixel, cv::Mat &obj)
{
obj = cv::Mat(height, width, CV_8UC1, pPixel);
}
Upvotes: 0
Reputation: 7919
To assign data to an already initialized Mat object ,you can use std::copy
:
std::copy(pPixel, pPixel + width*height, &obj.data[0]);
and if that matrix is already initialized with:
cv::Mat myMat(height,width, CV_8UC1);
You do not need any further modification to set data type.
Upvotes: 1
Reputation: 824
I'm not sure what band_num
means, bu you could use
void create_mat(int width, int height, int band_num, unsigned char *pPixel, cv::Mat &obj)
{
obj.create(width, height, type); // e.g. type: CV_8UC1
obj.data = pPixel;
}
Upvotes: 1