CV_8U:占8位的unsigned
CV_8UC(n):占8位的unsigned char
CV_8UC1:占8位的unsigned char 一通道
CV_8UC2:占8位的unsigned char 二通道
CV_8UC3:占8位的unsigned char 三通道
CV_8UC4:占8位的unsigned char 四通道
CV_8S:占8位的signed
CV_8SC(n):占8位的signed char
CV_8SC1:占8位的signed char 一通道
CV_8SC2:占8位的signed char 二通道
CV_8SC3:占8位的signed char 三通道
CV_8SC4:占8位的signed char 四通道
CV_16U:占16位的unsigned
CV_16UC(n):占16位的unsigned char
CV_16UC1:占16位的unsigned char 一通道
CV_16U2:占16位的unsigned char 二通道
CV_16U3:占16位的unsigned char 三通道
CV_16U4:占16位的unsigned char 四通道
CV_16S:占16位的signed
CV_16SC(n):占16位的signed char
CV_16SC1:占16位的signed char 一通道
CV_16SC2:占16位的signed char 二通道
CV_16SC3:占16位的signed char 三通道
CV_16SC4:占16位的signed char 四通道
CV_16F:占16位的float
CV_16FC(n):占16位的float char
CV_16FC1:占16位的float char 一通道
CV_16FC2:占16位的float char 二通道
CV_16FC3:占16位的float char 三通道
CV_16FC4:占16位的float char 四通道
CV_32S:占32位的signed
CV_32SC(n):占32位的signed char
CV_32SC1:占32位的signed char 一通道
CV_32SC2:占32位的signed char 二通道
CV_32SC3:占32位的signed char 三通道
CV_32SC4:占32位的signed char 四通道
CV_32F:占32位的float
CV_32FC(n):占32位的float char
CV_32FC1:占32位的float char 一通道
CV_32FC2:占32位的float char 二通道
CV_32FC3:占32位的float char 三通道
CV_32FC4:占23位的float char 四通道
CV_64F:占64位的float
CV_64FC(n):占64位的float char
CV_64FC1:占64位的float char 一通道
CV_64FC2:占64位的float char 二通道
CV_64FC3:占64位的float char 三通道
CV_64FC4:占64位的float char 四通道
CV_NODISCARD_STD static MatExpr Mat::zeros(int rows, int cols, int type);
CV_NODISCARD_STD static MatExpr Mat::zeros(Size size, int type);
CV_NODISCARD_STD static MatExpr Mat::zeros(int ndims, const int* sz, int type);
//not recommended
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
Size与Mat中的成员函数.size()的返回值,有相同的数据类型,是[宽*高]。
Mat中的成员变量.size,与以上二者不同,是 rows*cols
CV_NODISCARD_STD static MatExpr Mat::ones(int rows, int cols, int type);
CV_NODISCARD_STD static MatExpr Mat::ones(Size size, int type);
CV_NODISCARD_STD static MatExpr Mat::ones(int ndims, const int* sz, int type);
//not recommended
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
CV_NODISCARD_STD static MatExpr Mat::eye(int rows, int cols, int type);
CV_NODISCARD_STD static MatExpr Mat::eye(Size size, int type);
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
MatExpr Mat::t() const;
MatExpr Mat::inv(int method=DECOMP_LU) const;
template inline
Mat_<_Tp>::Mat_(int _rows, int _cols)
: Mat(_rows, _cols, traits::Type<_Tp>::value)
{
}
template inline
Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
: Mat(_rows, _cols, traits::Type<_Tp>::value)
{
*this = value;
}
template inline
Mat_<_Tp>::Mat_(Size _sz)
: Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
{}
template inline
Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
: Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
{
*this = value;
}
以下为使用实例
Mat a=Mat_(2,2)<<(1,2,3,4);
Mat b=Mat_(Size(2,2))<<(1,2,3,4);
注意:给出的数据类型必须是基本数据类型,如int,double。不能是CV_16F等。
Mat::Mat() CV_NOEXCEPT;
Mat::Mat(int rows, int cols, int type);
Mat::Mat(Size size, int type);
Mat::Mat(int rows, int cols, int type, const Scalar& s);
Mat::Mat(Size size, int type, const Scalar& s);
Mat::Mat(const std::vector& sizes, int type);
Mat::Mat(const std::vector& sizes, int type, const Scalar& s);
Mat::Mat(const Mat& m);
void Mat::create(int rows, int cols, int type);
void Mat::create(Size size, int type);
void Mat::create(const std::vector& sizes, int type);
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
Scalar(gray)
Scalar(blue,green,red)
typedef Vec Vec2b;
typedef Vec Vec3b;
typedef Vec Vec4b;
typedef Vec Vec2s;
typedef Vec Vec3s;
typedef Vec Vec4s;
typedef Vec Vec2w;
typedef Vec Vec3w;
typedef Vec Vec4w;
typedef Vec Vec2i;
typedef Vec Vec3i;
typedef Vec Vec4i;
typedef Vec Vec6i;
typedef Vec Vec8i;
typedef Vec Vec2f;
typedef Vec Vec3f;
typedef Vec Vec4f;
typedef Vec Vec6f;
typedef Vec Vec2d;
typedef Vec Vec3d;
typedef Vec Vec4d;
typedef Vec Vec6d;
以下为实例
Mat a(Size(2560,1440),CV_8UC3);
for(int i=0;i(i,j)[0]=0;
a.ptr(i,j)[1]=0;
a.ptr(i,j)[2]=255;
}
}
for(int i=0;i(i,j)[0]=0;
a.at(i,j)[1]=0;
a.at(i,j)[2]=255;
}
}
用ptr访问可以不加Vec类型
用at访问必须加Vec类型
Mat a(Size(2560,1440),CV_8UC3);
for(auto iter=a.begin();iter!=a.end();iter++){
iter[0]=255;
iter[1]=0;
iter[2]=0;
}
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
enum ImreadModes {
IMREAD_UNCHANGED = -1,
//!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
IMREAD_GRAYSCALE = 0,
//!< If set, always convert image to the single channel grayscale image (codec internal conversion).
IMREAD_COLOR = 1,
//!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2,
//!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4,
//!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8,
//!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16,
//!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17,
//!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32,
//!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33,
//!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64,
//!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65,
//!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128
//!< If set, do not rotate the image according to EXIF's orientation flag.
};
CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
winname(window name):窗体名
CV_EXPORTS_W void imshow(const String& winname, InputArray mat);
winname(window name):窗体名
若窗体未创建,会自动进行创建
CV_EXPORTS_W int waitKey(int delay = 0);
控制图片的展示时间,如设置delay=0,则表示一直展示,按SPACE停止展示
如设置delay不为0,则表示停留delay毫秒
CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
const std::vector& params = std::vector());
filename:保存的文件名
CV_WRAP explicit VideoCapture(const String& filename, int apiPreference = CAP_ANY);
CV_WRAP explicit VideoCapture(const String& filename, int apiPreference, const std::vector& params);
CV_WRAP explicit VideoCapture(int index, int apiPreference = CAP_ANY);
CV_WRAP explicit VideoCapture(int index, int apiPreference, const std::vector& params);
影片档案名称(例如video.avi)
图片序列(例如img_%02d.jpg,将读取像这样的样本img_00.jpg, img_01.jpg, img_02.jpg, …)
视频流的网址(例如protocol://host:port/script_name?script_params|auth)。请注意,每个视频流或IP摄像机源均具有其自己的URL方案。请参考源流的文档以了解正确的URL。
要打开的视频捕获设备的ID。要使用默认后端打开默认摄像头,只需传递0。
当apiPreference为CAP_ANY时,使用camera_id + domain_offset(CAP_ *)向后兼容有效。
首选使用的Capture API后端。如果有多个可用的读取器实现,则可以用于实施特定的读取器实现。
设置读取的摄像头编号,默认CAP_ANY=0,自动检测摄像头。多个摄像头时,使用索引0,1,2,…进行编号调用摄像头。 apiPreference = -1时单独出现窗口,选取相应编号摄像头。
CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );
code:转换码
RGB和BGR(opencv默认的彩色图像的颜色空间是BGR)颜色空间的转换
cv::COLOR_BGR2RGB
cv::COLOR_RGB2BGR
cv::COLOR_RGBA2BGRA
cv::COLOR_BGRA2RGBA
向RGB和BGR图像中增添alpha通道
cv::COLOR_RGB2RGBA
cv::COLOR_BGR2BGRA
从RGB和BGR图像中去除alpha通道
cv::COLOR_RGBA2RGB
cv::COLOR_BGRA2BGR
从RBG和BGR颜色空间转换到灰度空间
cv::COLOR_RGB2GRAY
cv::COLOR_BGR2GRAY
cv::COLOR_RGBA2GRAY
cv::COLOR_BGRA2GRAY
从灰度空间转换到RGB和BGR颜色空间
cv::COLOR_GRAY2RGB
cv::COLOR_GRAY2BGR
cv::COLOR_GRAY2RGBA
cv::COLOR_GRAY2BGRA
RGB和BGR颜色空间与BGR565颜色空间之间的转换
cv::COLOR_RGB2BGR565
cv::COLOR_BGR2BGR565
cv::COLOR_BGR5652RGB
cv::COLOR_BGR5652BGR
cv::COLOR_RGBA2BGR565
cv::COLOR_BGRA2BGR565
cv::COLOR_BGR5652RGBA
cv::COLOR_BGR5652BGRA
灰度空间与BGR565之间的转换
cv::COLOR_GRAY2BGR555
cv::COLOR_BGR5552GRAY
RGB和BGR颜色空间与CIE XYZ之间的转换
cv::COLOR_RGB2XYZ
cv::COLOR_BGR2XYZ
cv::COLOR_XYZ2RGB
cv::COLOR_XYZ2BGR
RGB和BGR颜色空间与uma色度(YCrCb空间)之间的转换
cv::COLOR_RGB2YCrCb
cv::COLOR_BGR2YCrCb
cv::COLOR_YCrCb2RGB
cv::COLOR_YCrCb2BGR
RGB和BGR颜色空间与HSV颜色空间之间的相互转换
cv::COLOR_RGB2HSV
cv::COLOR_BGR2HSV
cv::COLOR_HSV2RGB
cv::COLOR_HSV2BGR
RGB和BGR颜色空间与HLS颜色空间之间的相互转换
cv::COLOR_RGB2HLS
cv::COLOR_BGR2HLS
cv::COLOR_HLS2RGB
cv::COLOR_HLS2BGR
RGB和BGR颜色空间与CIE Lab颜色空间之间的相互转换
cv::COLOR_RGB2Lab
cv::COLOR_BGR2Lab
cv::COLOR_Lab2RGB
cv::COLOR_Lab2BGR
RGB和BGR颜色空间与CIE Luv颜色空间之间的相互转换
cv::COLOR_RGB2Luv
cv::COLOR_BGR2Luv
cv::COLOR_Luv2RGB
cv::COLOR_Luv2BGR
Bayer格式(raw data)向RGB或BGR颜色空间的转换
cv::COLOR_BayerBG2RGB
cv::COLOR_BayerGB2RGB
cv::COLOR_BayerRG2RGB
cv::COLOR_BayerGR2RGB
cv::COLOR_BayerBG2BGR
cv::COLOR_BayerGB2BGR
cv::COLOR_BayerRG2BGR
cv::COLOR_BayerGR2BGR
Mat.ptr(i,j)=Mat.ptr(i,j)*a+b
a:控制对比度增益
b:控制亮度增益
Mat xuenai = imread("xuenai.jpg");
resize(xuenai,xuenai,Size(1000,1000));
imshow("xuenai", xuenai);
for(int i=0;i(i, j)[k] = saturate_cast(xuenai.at(i, j)[k] * 1.2 + 30);
}
}
}
imshow("xuenai_convertTo",xuenai);
waitKey();
void Mat::convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;
Mat xuenai = imread("xuenai.jpg");
resize(xuenai,xuenai,Size(1000,1000));
imshow("xuenai", xuenai);
xuenai.convertTo(xuenai,-1,1.2,30);
imshow("xuenai_convertTo",xuenai);
waitKey();
可以看到效果是一样的
CV_EXPORTS_W void addWeighted(InputArray src1, double alpha, InputArray src2,
double beta, double gamma, OutputArray dst, int dtype = -1);
src(source1):输入图片1
alpha:src1的权重
src2(source2):输入图片2
beta:src2的权重
gamma:额外的增量
dst(destination):输出图片
dtype(destination type):输出图片的数据类型,-1表示与输入图片一致
CV_EXPORTS_W void resize( InputArray src, OutputArray dst,
Size dsize, double fx = 0, double fy = 0,
int interpolation = INTER_LINEAR );
src(source):输入图片
dst(destination):输出图片
dsize(destination size):输出图片的尺寸
fx:x方向(width方向)的缩放比例,如果它是0,那么它就会按照(double)dsize.width/src.cols来计算;
fy:y方向(height方向)的缩放比例,如果它是0,那么它就会按照(double)dsize.height/src.rows来计算;
interpolation:插值算法的选择
enum InterpolationFlags{
/** nearest neighbor interpolation */
INTER_NEAREST = 0,
/** bilinear interpolation */
INTER_LINEAR = 1,
/** bicubic interpolation */
INTER_CUBIC = 2,
/** resampling using pixel area relation. It may be a preferred method for image decimation, as
it gives moire'-free results. But when the image is zoomed, it is similar to the INTER_NEAREST
method. */
INTER_AREA = 3,
/** Lanczos interpolation over 8x8 neighborhood */
INTER_LANCZOS4 = 4,
/** Bit exact bilinear interpolation */
INTER_LINEAR_EXACT = 5,
/** Bit exact nearest neighbor interpolation. This will produce same results as
the nearest neighbor method in PIL, scikit-image or Matlab. */
INTER_NEAREST_EXACT = 6,
/** mask for interpolation codes */
INTER_MAX = 7,
/** flag, fills all of the destination image pixels. If some of them correspond to outliers in the
source image, they are set to zero */
WARP_FILL_OUTLIERS = 8,
/** flag, inverse transformation
For example, #linearPolar or #logPolar transforms:
- flag is __not__ set: \f$dst( \rho , \phi ) = src(x,y)\f$
- flag is set: \f$dst(x,y) = src( \rho , \phi )\f$
*/
WARP_INVERSE_MAP = 16
};
使用注意事项:
dsize和fx/fy不能同时为0
指定dsize的值,让fx和fy空置直接使用默认值。
让dsize为0,指定好fx和fy的值,比如fx=fy=0.5,那么就相当于把原图两个方向缩小一倍。
CV_EXPORTS_W void pyrDown( InputArray src, OutputArray dst,
const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
//缩小一倍
src(source):输入图片
dst(destination):输出图片
dstsize(destination size):输出图片的尺寸,默认自动调整
borderType:边界填充方式,默认为黑边。如果没有设置dstsize,则不会出现黑边,因为已经进行了自动调整
CV_EXPORTS_W void pyrUp( InputArray src, OutputArray dst,
const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
//放大一倍
src(source):输入图片
dst(destination):输出图片
dstsize(destination size):输出图片的尺寸,默认自动调整
borderType:边界填充方式,默认为黑边。如果没有设置dstsize,则不会出现黑边,因为已经进行了自动调整
CV_EXPORTS_W double threshold( InputArray src, OutputArray dst,
double thresh, double maxval, int type );
src(source):输入图片
dst(destination):输出图片
thresh(threshold):阈值
maxval(max value):最大值
type:阈值类型
enum ThresholdTypes {
THRESH_BINARY = 0, //!< \f[\texttt{dst} (x,y) = \fork{\texttt{maxval}}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{0}{otherwise}\f]
THRESH_BINARY_INV = 1, //!< \f[\texttt{dst} (x,y) = \fork{0}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{maxval}}{otherwise}\f]
THRESH_TRUNC = 2, //!< \f[\texttt{dst} (x,y) = \fork{\texttt{threshold}}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\f]
THRESH_TOZERO = 3, //!< \f[\texttt{dst} (x,y) = \fork{\texttt{src}(x,y)}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{0}{otherwise}\f]
THRESH_TOZERO_INV = 4, //!< \f[\texttt{dst} (x,y) = \fork{0}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\f]
THRESH_MASK = 7,
THRESH_OTSU = 8, //!< flag, use Otsu algorithm to choose the optimal threshold value
THRESH_TRIANGLE = 16 //!< flag, use Triangle algorithm to choose the optimal threshold value
};
首先指定像素的灰度值的阈值,遍历图像中像素值,如果像素的灰度值大于这个阈值,则将这个像素设置为最大像素值(8位灰度值最大为255);若像素的灰度值小于阈值,则将该像素点像素值赋值为0。公式以及示意图如下:
首先也要指定一个阈值,不同的是在对图像进行阈值化操作时与阈值二值化相反,当像素的灰度值超过这个阈值的时候为该像素点赋值为0;当该像素的灰度值低于该阈值时赋值为最大值。公式及示意图如下:
给定像素值阈值,在图像中像素的灰度值大于该阈值的像素点被设置为该阈值,而小于该阈值的像素值保持不变。公式以及示意图如下:
与截断阈值化相反,像素点的灰度值如果大于该阈值则像素值不变,如果像素点的灰度值小于该阈值,则该像素值设置为0.公式以及示意图如下:
像素值大于阈值的像素赋值为0,而小于该阈值的像素值则保持不变,公式以及示意图如下:
inline
Mat Mat::operator()( const Rect& roi ) const
{
return Mat(*this, roi);
}
以下为实例
Mat xuenai = imread("xuenai.jpg");
resize(xuenai,xuenai,Size(1000,1000));
imshow("xuenai", xuenai);
Mat tuanzi(xuenai,(Rect(0,0,500,1000)));
imshow("tuanzi",tuanzi);
Mat::Mat(const Mat& m, const Rect& roi);
以下为实例
Mat xuenai = imread("xuenai.jpg");
resize(xuenai,xuenai,Size(1000,1000));
imshow("xuenai", xuenai);
Mat tuanzi(xuenai(Rect(0,0,500,1000)));
imshow("tuanzi",tuanzi);
template inline
Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)
: x(_x), y(_y), width(_width), height(_height) {}
template inline
Rect_<_Tp>::Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz)
: x(org.x), y(org.y), width(sz.width), height(sz.height) {}
template inline
Rect_<_Tp>::Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2)
{
x = std::min(pt1.x, pt2.x);
y = std::min(pt1.y, pt2.y);
width = std::max(pt1.x, pt2.x) - x;
height = std::max(pt1.y, pt2.y) - y;
}
CV_EXPORTS_W void flip(InputArray src, OutputArray dst, int flipCode);
src(source):输入图片
dst(destination):输出图片
flipCode:翻转类型
flipcode==0;//上下翻转
flipcod>0;//左右翻转
flipcode<0;//上下加左右翻转,等价于旋转180°
CV_EXPORTS_W void rotate(InputArray src, OutputArray dst, int rotateCode);
enum RotateFlags {
ROTATE_90_CLOCKWISE = 0, //!
src(source):输入图片
dst(destination):输出图片
rotateCode:旋转类型
CV_EXPORTS_W void warpAffine( InputArray src, OutputArray dst,
InputArray M, Size dsize,
int flags = INTER_LINEAR,
int borderMode = BORDER_CONSTANT,
const Scalar& borderValue = Scalar());
src(source):输入图片
dst(destination):输出图片
M:变换矩阵
dsize(destination size):输出图片的尺寸,若不对输出图片的尺寸进行调整,那么很可能会出现黑边
flags:插值算法
borderMode:边界外推法
borderValue:填充边界的值
只需将变换矩阵M设置成如下形式:
int delta_x.delta_y;
Mat M=Mat_(2,3)<<(1,0,delta_x,
0,1,delta_y);
delta_x:x方向上的偏移量
delta_y:y方向上的偏移量
inline
Mat getRotationMatrix2D(Point2f center, double angle, double scale)
{
return Mat(getRotationMatrix2D_(center, angle, scale), true);
}
center:旋转中心点的坐标
angle:逆时针偏角
scale:生成图与原图之比
CV_EXPORTS Mat getAffineTransform( const Point2f src[], const Point2f dst[] );
src[](source[]):输入图片的坐标点集,含三个坐标点
dst[](destination[]):三个坐标点变换的目标位置
CV_EXPORTS_W Mat getPerspectiveTransform(InputArray src, InputArray dst, int solveMethod = DECOMP_LU);
src(source):输入图片
dst(destination):输出图片
CV_EXPORTS Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[], int solveMethod = DECOMP_LU);
src[](source[]):输入图片的坐标点集,含四个坐标点
dst[](destination[]):四个坐标点变换的目标位置