我使用的是Ubuntu 12.04和opencv 2
我写了下面的代码:
IplImage* img =0; img = cvLoadImage("nature.jpg"); if(img != 0) { Mat Img_mat(img); std::vector<Mat> RGB; split(Img_mat, RGB); int data = (RGB[0]).at<int>(i,j)); /*Where i, j are inside the bounds of the matrix size .. i have checked this*/ }
问题是我在数据variables中获得负值和非常大的值。 我想我在某个地方犯了一些错误。 你能指出一下吗? 我一直在阅读文档(我还没有完成它..这是相当大的。)但从我读过的,这应该工作。 但它不是。 这里怎么了?
Img_mat
是一个3通道图像。 每个通道由数据类型的像素值uchar
组成。 因此,通过split(Img_mat, BGR)
, Img_mat
被分割成3个蓝色,绿色和红色的平面,这些平面被集中存储在向量BGR
。 所以BGR[0]
是第一个(蓝色)与uchar
数据类型像素的平面…因此它将是
int dataB = (int)BGR[0].at<uchar>(i,j); int dataG = (int)BGR[1].at<uchar>(i,j);
等等…
@bsdnoobz我想你想写
Vec3b data = Img_mat.at<Vec3b>(i,j); data[0] = // blue pixel data[1] = // green pixel
等等…
你必须指定cv::Mat::at(i,j)
的正确类型。 你正在像素访问像素,而它应该是一个向量的uchar
。 你的代码应该是这样的:
IplImage* img = 0; img = cvLoadImage("nature.jpg"); if(img != 0) { Mat Img_mat(img); std::vector<Mat> BGR; split(Img_mat, BGR); Vec3b data = BGR[0].at<Vec3b>(i,j); // data[0] -> blue // data[1] -> green // data[2] -> red }
你为什么先加载一个IplImage? 您正在混合C和C ++接口。 用imread直接加载cv :: Mat将会更直接。
这样您也可以指定类型并在呼叫中使用相应的类型。