opencv访问图像

来源:互联网 发布:用友软件客户端安装 编辑:程序博客网 时间:2024/06/03 02:50

opencv访问图像有几个常见的方法:
1 通过at方法:

Mat image(Size(height,width),CV_8UC1);image.at<uchar>(y,x)

2 通过at使用point方法:

image.at<uchar>(cv::Point2d(x,y))

3 如果提前知道image的类型,比如知道是为uchar:

//当为单通道时Mat_<uchar> image(height,width);image(y,x);//当为多通道uchar时:Mat_<Vec3b> image(height,width,Vec3b(0,255,0));image(y,x)[channel];

当提前知道图像类型的时候,建议使用Mat_,因为访问起来更方便,代码更少。

4 通过ptr指针获取图像的行指针:

    //gain_map是一维CV_32FC1    const float* gain_row = gain_map.ptr<float>(y);     //image是CV_8UC3    Point3_<uchar>* row = image.ptr<Point3_<uchar> >(y);    for (int x = 0; x < image.cols; ++x)    {      row[x].x = saturate_cast<uchar>(row[x].x * gain_row[x]);      row[x].y = saturate_cast<uchar>(row[x].y * gain_row[x]);      row[x].z = saturate_cast<uchar>(row[x].z * gain_row[x]);    }

另外给opencv的定义:

//2维点(x,y)typedef Point_<int> Point2i;typedef Point2i Point;typedef Point_<float> Point2f;typedef Point_<double> Point2d;//3维点(x,y,z),点云数据typedef Point3_<int> Point3i;typedef Point3_<float> Point3f;typedef Point3_<double> Point3d;//多通道,uchar是图像数据类型,后面的数据代表通道数typedef Vec<uchar, 2> Vec2b;typedef Vec<uchar, 3> Vec3b;typedef Vec<uchar, 4> Vec4b;typedef Vec<short, 2> Vec2s;typedef Vec<short, 3> Vec3s;typedef Vec<short, 4> Vec4s;typedef Vec<int, 2> Vec2i;typedef Vec<int, 3> Vec3i;typedef Vec<int, 4> Vec4i;typedef Vec<float, 2> Vec2f;typedef Vec<float, 3> Vec3f;typedef Vec<float, 4> Vec4f;typedef Vec<float, 6> Vec6f;typedef Vec<double, 2> Vec2d;typedef Vec<double, 3> Vec3d;typedef Vec<double, 4> Vec4d;typedef Vec<double, 6> Vec6d;
0 0