在Matsc中将Mats保存到int数组中

时间:2014-07-01 09:47:45

标签: c++ opencv

我将img分成3个单独的垫子,如下所示:

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

现在我想将这些R,G和Bs值存储在三个不同的数组中。像这样的东西: 例如,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];

}

我知道这会给出错误。那么我该如何正确实现这个功能呢?

2 个答案:

答案 0 :(得分:2)

你快到了那里:

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];

}

答案 1 :(得分:1)

以下是如何为R执行此操作(明显扩展为B&amp; 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]);
}   
相关问题