首页 > 学院 > 开发设计 > 正文

操作像素------OpenCV2.4.11

2019-11-08 02:18:25
字体:
来源:转载
供稿:网友

操作像素------OpenCV2.4.11

1、Mat类

Mat是一个n维矩阵类,声明在<opencv2/core/core.hpp>中1711-2034行。

class CV_EXPORTS Mat{public:    //! default constructor    Mat();    //! constructs 2D matrix of the specified size and type    // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)    // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)    Mat(int rows, int cols, int type);    Mat(Size size, int type);    //! constucts 2D matrix and fills it with the specified value _s.    Mat(int rows, int cols, int type, const Scalar& s);    Mat(Size size, int type, const Scalar& s);	........    //! destructor - calls release()    ~Mat();    //! assignment Operators    Mat& operator = (const Mat& m);
      ............
     //! the same as above, with the pointer dereferencing    template<typename _Tp> _Tp& at(int i0=0);    template<typename _Tp> const _Tp& at(int i0=0) const;    template<typename _Tp> _Tp& at(int i0, int i1);    template<typename _Tp> const _Tp& at(int i0, int i1) const;    template<typename _Tp> _Tp& at(int i0, int i1, int i2);    template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;    template<typename _Tp> _Tp& at(const int* idx);    template<typename _Tp> const _Tp& at(const int* idx) const;    template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);    template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;    //! special versions for 2D arrays (especially convenient for referencing image pixels)    template<typename _Tp> _Tp& at(Point pt);    template<typename _Tp> const _Tp& at(Point pt) const;
    ............    int flags;    //! the matrix dimensionality, >= 2    int dims;    //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions    int rows, cols;    //! pointer to the data    uchar* data;    ..............};

2、Scalar类

template<typename _Tp> class Scalar_ : public Vec<_Tp, 4>{public:    //! various constructors    Scalar_();    Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);    Scalar_(const CvScalar& s);    Scalar_(_Tp v0);    //! returns a scalar with all elements set to v0    static Scalar_<_Tp> all(_Tp v0);    //! conversion to the old-style CvScalar    operator CvScalar() const;    //! conversion to another data type    template<typename T2> operator Scalar_<T2>() const;    //! per-element PRoduct    Scalar_<_Tp> mul(const Scalar_<_Tp>& t, double scale=1 ) const;    // returns (v0, -v1, -v2, -v3)    Scalar_<_Tp> conj() const;    // returns true iff v1 == v2 == v3 == 0    bool isReal() const;};typedef Scalar_<double> Scalar;
template<typename _Tp, int cn> class Vec : public Matx<_Tp, cn, 1>{public:    typedef _Tp value_type;    enum { depth = DataDepth<_Tp>::value, channels = cn, type = CV_MAKETYPE(depth, channels) };    //! default constructor    Vec();      .........
    template<typename _T2> Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp);};
template<typename _Tp, int m, int n> class Matx{public:    typedef _Tp value_type;    typedef Matx<_Tp, (m < n ? m : n), 1> diag_type;    typedef Matx<_Tp, m, n> mat_type;    enum { depth = DataDepth<_Tp>::value, rows = m, cols = n, channels = rows*cols,           type = CV_MAKETYPE(depth, channels) };    //! default constructor    Matx();      .........
    //! extract the matrix row    Matx<_Tp, 1, n> row(int i) const;    //! extract the matrix column    Matx<_Tp, m, 1> col(int i) const;    _Tp val[m*n]; //< matrix elements};

3、获取像素的亮度值

(1)获取单通道灰度图(类型 8UC1)的(x, y)位置处的像素值:

	Scalar intensity = img.at<uchar>(x, y);

intensity.val[0] 中保存从0到255的值

(2)获取3通道图像的(x, y)位置处的像素值:

	Vec3b intensity = img.at<Vec3b>(x, y);	uchar blue = intensity.val[0];	uchar green = intensity.val[1];	uchar red = intensity.val[2];

(3)处理浮点图像(例如通对一个3通道图像进行Sobel运算得到的浮点图像):

	Vec3f intensity = img.at<Vec3f>(x, y);	float blue = intensity.val[0];	float green = intensity.val[1];	float red = intensity.val[2];

(4)像素值的修改:

	img.at<uchar>(x, y) = 128;

示例


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表