user2799508
user2799508

Reputation: 848

Saving Mats into int arrays in opencv

I split my img into 3 separate Mats like this:

std::vector<Mat> planes(3);
cv::split(img, planes);
cv::Mat R = planes[2];
cv::Mat G = planes[1];
cv::Mat B = planes[0];

Now I want to store these R, G and Bs values in three different arrays. Somthing like this: for example for R.

std::vector<Mat> planes(3);
cv::split(img, planes);
cv::Mat R = planes[2];
int r[20]; 

for (i=0 ; i<20 ; i++)

{

r[i]= R[i];

}

I know this will give error. So how do I correctly implement this feature ?

Upvotes: 1

Views: 1190

Answers (2)

Bull
Bull

Reputation: 11951

Here is how you can do this for R (with obvious extension to B & G)

std::vector<Mat> planes(3);
cv::split(img, planes);
cv::Mat R;

// change the type from uchar to int
planes[2].convertTo(R, CV_32SC1);

// get a pointer to the first row
int* r = R.ptr<int>(0);

// iterate of all data  (R has to be continuous
// with no row padding to do it like this)
for (i = 0 ; i < R.rows * R.cols; ++i)
{    // you have to write the following :-)
     your_code(r[i]);
}   

Upvotes: 1

remi
remi

Reputation: 3988

You were almost there:

std::vector<Mat> planes(3);
cv::split(img, planes);
cv::Mat R = planes[2];
int r[20]; 
unsigned char *Rbuff = R.data;
for (i=0 ; i<20 ; i++)

{

r[i]= (int)Rbuff[i];

}

Upvotes: 2

Related Questions