[c++ Opencv] Some related operations of Mat in Opencv

The Mat class in C++ is one of the core data structures in the OpenCV library and is used to represent two-dimensional and three-dimensional data such as images and matrices. The main feature of the Mat class is that it can easily access pixels, supports various matrix operations, and can implement various image processing algorithms. Here are some common Mat operations:

1. Create Mat objects: Mat objects can be created through constructors, static methods or assignment operators, for example:

cv::Mat mat1; //创建空的Mat对象
cv::Mat mat2(100, 200, CV_8UC3); //创建大小为100x200,类型为CV_8UC3的Mat对象
cv::Mat mat3 = cv::Mat::zeros(200, 300, CV_8UC1); //创建大小为200x300,类型为CV_8UC1,像素值为0的Mat对象
cv::Mat mat4 = cv::imread("test.jpg"); //从文件中读取图像,返回一个Mat对象

2. Access pixels: You can access pixels in the Mat object through the at() method or pointer, for example:

int value1 = mat1.at<int>(row, col); //获取Mat对象中(row, col)处的像素值
uchar value2 = mat2.ptr<uchar>(row)[col * 3]; //获取Mat对象中(row, col)处的B通道的像素值(类型为CV_8UC3)

3. Modify pixels: You can modify pixels in the Mat object through the at() method or pointer method, for example:

mat1.at<int>(row, col) = value1; //将Mat对象中(row, col)处的像素值设为value1
mat2.ptr<uchar>(row)[col * 3] = value2; //将Mat对象中(row, col)处的B通道的像素值(类型为CV_8UC3)设为value2

4. Access the properties of the Mat object: You can access the size and type of the Mat object through attributes such as rows, cols and type, for example:

int rows = mat1.rows; //获取Mat对象的行数
int cols = mat2.cols; //获取Mat对象的列数
int type = mat3.type(); //获取Mat对象的像素类型

5. Matrix operations: You can use Mat objects to perform various matrix operations, such as:

cv::Mat mat5 = mat2 * 2; //将Mat对象中所有像素值乘以2
cv::Mat mat6 = mat2 + mat3; //将两个Mat对象中的像素值相加
cv::Mat mat7 = mat3.t(); //将Mat对象转置

6. Channel separation and merging: You can separate a multi-channel Mat object into a single-channel Mat object, or merge multiple single-channel Mat objects into a multi-channel Mat object, for example:

std::vector<cv::Mat> channels;
cv::split(mat2, channels); //将Mat对象的三个通道分离成三个Mat对象
cv::merge(channels, mat2); //将三个Mat对象合并成一个Mat对象

7. Image scaling: The Mat object can be scaled through the resize() method, for example:

cv::Mat dst;
cv::resize(src, dst, cv::Size(200, 100)); //将src Mat对象缩放为大小为200x100的dst Mat对象

8. Image translation: The Mat object can be translated through the warpAffine() method, for example:

cv::Mat dst;
cv::Mat M = (cv::Mat_<float>(2,3) << 1, 0, 50, 0, 1, 100); //定义平移矩阵
cv::warpAffine(src, dst, M, src.size()); //进行平移操作,输出结果保存在dst Mat对象中

9. Image rotation: The Mat object can be rotated through the warpAffine() method, for example:

cv::Mat dst;
cv::Point2f center(src.cols/2.0, src.rows/2.0); //定义旋转中心
cv::Mat M = cv::getRotationMatrix2D(center, 45, 1.0); //定义旋转矩阵
cv::warpAffine(src, dst, M, src.size()); //进行旋转操作,输出结果保存在dst Mat对象中

10. Image flipping: The Mat object can be flipped through the flip() method, for example:

cv::Mat dst;
cv::flip(src, dst, 1); //将src Mat对象水平翻转,输出结果保存在dst Mat对象中

11. Image expansion and erosion: The Mat object can be expanded and corroded through the dilate() and erode() methods, for example:

cv::Mat dst1, dst2;
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3,3)); //定义膨胀和腐蚀时的结构元素
cv::dilate(src, dst1, kernel); //对src Mat对象进行膨胀操作,输出结果保存在dst1 Mat对象中
cv::erode(src, dst2, kernel); //对src Mat对象进行腐蚀操作,输出结果保存在dst2 Mat对象中

12. Histogram equalization: Histogram equalization can be performed on the Mat object through the equalizeHist() method to improve image contrast, for example:

cv::Mat dst;
cv::equalizeHist(src, dst); //对src Mat对象进行直方图均衡化操作,输出结果保存在dst Mat对象中

13. Gaussian filtering: Gaussian filtering can be performed on the Mat object through the GaussianBlur() method, which is used to denoise and smooth images, for example:

cv::Mat dst;
cv::GaussianBlur(src, dst, cv::Size(3,3), 0); //对src Mat对象进行高斯滤波操作,输出结果保存在dst Mat对象中

14. Canny edge detection: Canny edge detection can be performed on the Mat object through the Canny() method, for example:

cv::Mat dst;
cv::Canny(src, dst, 50, 150); //对src Mat对象进行Canny边缘检测操作,输出结果保存在dst Mat对象中

15. Hough transformation: Hough transformation can be performed on the Mat object through the HoughLines() and HoughCircles() methods, which is used to detect shapes such as straight lines and circles, for example:

std::vector<cv::Vec2f> lines;
cv::HoughLines(dst, lines, 1, CV_PI/180, 100); //对dst Mat对象进行Hough直线检测操作,输出结果保存在lines中
std::vector<cv::Vec3f> circles;
cv::HoughCircles(dst, circles, CV_HOUGH_GRADIENT, 1, src.rows/8, 200, 100, 0, 0); //对dst Mat对象进行Hough圆检测操作,输出结果保存在circles中

16. Watershed algorithm: The watershed algorithm can be performed on the Mat object through the distanceTransform(), threshold() and watershed() methods for image segmentation, for example:

cv::Mat dst, binary, distance;
cv::distanceTransform(src, distance, cv::DIST_L2, 3); //对src Mat对象进行距离变换,输出结果保存在distance Mat对象中
cv::threshold(distance, binary, 0.7 * cv::norm(distance), 255, cv::THRESH_BINARY); //对distance Mat对象进行二值化,输出结果保存在binary Mat对象中
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3,3));
cv::morphologyEx(binary, binary, cv::MORPH_OPEN, kernel); //对binary Mat对象进行形态学开运算
cv::Mat markers(binary.size(), CV_8U, cv::Scalar(0));
markers = binary + 1;
cv::watershed(src, markers); //对src Mat对象进行分水岭算法,输出结果保存在markers Mat对象中

Guess you like

Origin blog.csdn.net/qq_44747572/article/details/129354688