博客
关于我
opencv常见函数使用
阅读量:730 次
发布时间:2019-03-21

本文共 2873 字,大约阅读时间需要 9 分钟。

OpenCV常见函数使用说明

OpenCV是一个强大的图像处理库,拥有丰富的功能和高效的API。本文将介绍OpenCV中常用的函数及其使用方法,包括轮廓检测、图像遍历、字符串操作等。

1. findcontours/drawcontours函数

findcontours和drawcontours是处理图像轮廓的核心函数,常用于边缘检测和物体识别。以下是两者的典型使用方法:

Mat img = image.clone(); // 复制输入图像vector contours; // 定义一个点向量来存储轮廓findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); // 椭圆检测,使用外部检索模式,未约简轮廓Mat image_drawfindcontours = Mat::zeros(img.rows, img.cols, CV_8UC1); // 创建同样大小的黑色图像drawContours(image_drawfindcontours, contours, -1, Scalar::all(255), 2); // 绘制轮廓,颜色为白色,线宽为2。

findContours的第二个参数可以是单通道图像矩阵,也可以是已经处理过的二值图像。常用的轮廓检索模式包括:

  • RETR_LIST: 提取所有轮廓,不建立父子关系。
  • RETR_EXTERNAL: 只提取外部轮廓,忽略子轮廓。
  • RETR_CCOMP: 提取所有轮廓并建立两级结构。
  • RETR_TREE: 提取所有轮廓并建立完整的树结构。

CHAIN_APPROX_NONE**: 保持所有边界点,不压缩轮廓;CHAIN_APPROX_SIMPLE**: 压缩轮廓,去除冗余点。

常用功能

1、遍历图像像素点(灰度图、RGB)

Mat img = imread("input.jpg"); // 读取输入图像int num_rows = img.rows; // 获取行数int num_cols = img.cols * img.channels(); // 获取列数和通道数// 遍历灰度图像素点for (int j = 0; j < num_rows; j++) {    uchar* pt = img.ptr(uchar)(j);    for (int i = 0; i < num_cols; i++) {        // 修改像素值        if (pt[i] != 251) pt[i] = 0;    }}

2、字符串查找与替换

常见的字符串操作包括查找和替换 Scalars 可以在字符串中查找文本或替换文本。以下是关键方法:

  • 查找方法:

    • size_t pos = str.find(s1);:从前向后查找子串。如果不找到返回string::npos。
    • size_t pos = str.rfind(s1);:从后向前查找子串。
    • size_t pos = str.find_first_of(s1);:查找任意字符的位置。
    • size_t pos = str.find_last_of(s1);:反向查找任意字符的位置。
  • 替换方法:

    • str.replace(pos, n, s1);:替换从pos开始的n个字符为s1。
  • 截取字符串方法:

    • str.substr(pos, n);:从pos开始截取n个字符。
    • str.substr(pos);:从pos开始截取到末尾。

3、遍历

使用OpenCV遍历图像像素点或文件列表。例如,遍历图像像素点可以看上面所示,以下是遍历文件的示例:

narrow_lane narrow_lane; // 定义变量glob(input_path, filenames, true); // glob函数返回匹配路径的列表for (int i = 0; i < filenames.size(); i++) {    // 输出文件名    cout << filenames[i] << endl;        // 提取文件名    size_t str_begin1 = filenames[i].find_last_of('\\');    size_t str_begin2 = filenames[i].find_last_of('.');    std::string filename_base = filenames[i].substr(str_begin1 + 1, str_begin2 - str_begin1 -1);}

4、Map

Map 是一个强大的结构,常用于键值对存储。例如,可以用Map来存储图像类别和文件名的对应关系。

map img_class; // 定义一个String到Int的Mapimg_class[{ "1654242", 1 }, { "01920", 2 }]; // 添加键值对

5、main函数参数

main函数通常处理输入和输出参数。例如:

int main(int argc, char** argv) {    if (argc != 5) {        cout << "argv error" << endl;        return 1;    }        string input_path;    string out_path;        for (int i = 0; i < argc; i++) {        if (!strcmp(argv[i], "-i")) {            input_path = argv[++i];        }        else if (!strcmp(argv[i], "-o")) {            out_path = argv[++i];        }    }}

2. Mat创建图像

使用Mat创建图像,可以指定颜色空间和像素类型。以下是创建 vividimage 的示例:

Mat img_out(img_in.rows, img_in.cols, CV_8UC3, Scalar(0, 0, 0)); // 创建一个黑色图像

CV_8UC3 表示8位深度的三通道图像,而 Scalar(0, 0, 0) 表示黑色

编译指令(附录)

在Ubuntu中编译OpenCV应用程序的命令:

g++ -std=c++11 your_file.cpp -I/usr/local/include/opencv -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgcodecs -o your_file

其中,-I指定头文件路径,-L指定库文件路径,-lopencv_ série uncovered 所有需要的库文件。

转载地址:http://jsqgz.baihongyu.com/

你可能感兴趣的文章
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>