1. 输入/输出
Mat img = imread(filename)
Mat img = imread(filename, IMREAD_GRAYSCALE);
imwrite(filename, img);
Mat imread(const string& filename, int flags=1 )
- flag>0,该函数返回 3 通道图像,如果磁盘上的图像文件是单通道的灰
度图像,则会被强制转为 3 通道;- flag=0,该函数返回单通道图像,如果磁盘的图像文件是多通道图像,则
会被强制转为单通道;- flag<0,则函数不对图像进行通道转换。
2. 基础操作
获取像素值
-
单通道:
Scalar intensity = img.at<uchar>(x, y)
,表示x行,y列。从0开始。
Scalar intensity = img.at<uchar>(Point(x, y))
,表示y列,x行。
img.at<uchar>(y, x) = 128
,改变像素值 -
多通道:
Vec3b intensity = img.at<Vec3b>(y, x);
uchar blue = intensity.val[0]; //intensity[0]
uchar green = intensity.val[1];
uchar red = intensity.val[2];
- std::vector:
vector<Point2f> points;
//... fill the array
Mat pointsMat = Mat(points);
Point2f point = pointsMat.at<Point2f>(i, 0);
内存调整和引用计数
std::vector<Point3f> points;
// .. fill the array
Mat pointsMat = Mat(points).reshape(1); //without copying data
基础操作
make a black image from an existing greyscale image img
:img = Scalar(0);
选择感兴趣的区域:Rect r(10, 10, 100, 100); Mat smallImg = img(r);
色彩类型变换:
Mat img = imread("image.jpg"); // loading a 8UC3 image
Mat grey;
cvtColor(img, grey, COLOR_BGR2GRAY);
数据类型变换:src.convertTo(dst, CV_32F)
图像展示
Mat img = imread("image.jpg");
Mat grey;
cvtColor(img, grey, COLOR_BGR2GRAY);
Mat sobelx;
Sobel(grey, sobelx, CV_32F, 1, 0);
double minVal, maxVal;
minMaxLoc(sobelx, &minVal, &maxVal); //find minimum and maximum intensities
Mat draw;
sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", draw);
waitKey(0);
sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal))
类型变换,同时范围最大化。一个斜率,一个截距。
网友评论