在openv中将Mats保存为int数组

Saving Mats into int arrays in opencv

本文关键字:int 数组 保存 Mats openv 中将      更新时间:2023-10-16

我把我的图片分成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和b的值存储在三个不同的数组中。像这样:例如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];
}

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

你就快到了:

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

这是你如何在R中做到这一点(明显扩展到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]);
}