2014-07-01 62 views
1

INT阵列我分裂我的IMG到3个独立的垫这样的:保存垫入的OpenCV

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和烧烤值。财产以后这样的: 例如用于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]; 

} 

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

回答

1

这里是你如何可以对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]); 
} 
+0

这会给我第一行的像素值(红色),对吧? – user2799508

+0

是的,但据我可以从cv :: split()内部看到,第二行将从没有填充的第一个图像继续。你可以使用'R.isContinuous()'来检查这是否为真。因此'r'不仅仅是一个指向第一行的指针,它是一个指向整个图像数据的指针。一般来说,我不会像这样公开图像的结构,没有很好的理由。 – Bull

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

} 
+0

使用'的memcpy()',而不是deferencing每个像素更安全,因为它可以避免某些体系结构中的“错误对齐”错误。 –

+1

@KeillRandor,对于这个答案,像素数据是uchars,他们怎么会错位?对于我的答案,像素数据是ints,OpenCV在16个字节边界上分配缓冲区,所以它们也不会错位。使用'memcpy()'进行类型转换也有点困难。在哪个架构上这不起作用? – Bull