Core moudle(基础模块)
1. Operations with images
输入输出:
Mat img = imread(filename); //载入一个图片
Mat img = imread(filename, IMREAD_GRAYSCALE); //以灰度形式载入图像
imwrite(filename, img); //把图像写出
OpenCV中还从缓冲区读写数据的函数:cv::imdecode
,cv::imencode
.
获取图像中的某个点的像素值;
//单通道访问方法
Scalar intensity = img.at<uchar>(y, x); //包含一个0-255的像素值,默认先行后列
Scalar intensity = img.at<uchar>(Point(x, y)); //也可以用Point()访问
//三通道访问方式
Vec3b intensity = img.at<Vec3b>(y, x); //先获取三个通道的数据
uchar blue = intensity.val[0]; //然后依次访问
uchar green = intensity.val[1];
uchar red = intensity.val[2];
//用2D或3D点来构造矩阵,并访问
vector<Point2f> points;
//... fill the array
Mat pointsMat = Mat(points); //用2D点初始化了一列
Point2f point = pointsMat.at<Point2f>(i, 0);
存储方式的管理
//内存管理和引用计数
std::vector<Point3f> points;
// .. fill the array
Mat pointsMat = Mat(points).reshape(1); //矩阵从32FC3类型的一列变换成32FC1类型的三列
//开辟新的内存以存储图像,使用cv::Mat::copyTo,cv::Mat::clone
Mat img = imread("image.jpg");
Mat img1 = img.clone();
//只声明矩阵是不分配内存的,但是可以当做输出矩阵来存储
Mat img = imread("image.jpg");
Mat sobelx;
Sobel(img, sobelx, CV_32F, 1, 0);
操作图像的数据类型
img = Scalar(0); //将图像中像素设置成黑色
Rect r(10, 10, 100, 100); //矩形框用来选择图像范围
Mat smallImg = img(r); //根据r来选择图像区域
Mat img = imread("image.jpg"); //读取的img是3通道
Mat grey;
cvtColor(img, grey, COLOR_BGR2GRAY); //彩色图转换成灰度图,grey是输出矩阵
src.convertTo(dst, CV_32F); //转换图像的数据类型
可视化图像
Mat img = imread("image.jpg");
namedWindow("image", WINDOW_AUTOSIZE); //设置窗口名字。
imshow("image", img); //可视化图像
waitKey(); //等待