写出边缘跟踪的c++代码(不使用OpenCV与MATLAB)

时间: 2024-01-26 08:05:01 浏览: 18
边缘跟踪是一种经典的计算机视觉算法,可以用于检测图像中物体的轮廓。下面是一个简单的C++实现,使用Sobel算子进行边缘检测,再利用非极大值抑制和双阈值处理得到二值化的边缘图像,最后进行边缘跟踪。 ``` #include <iostream> #include <cmath> #include <vector> using namespace std; // Sobel算子 int sobel_x[3][3] = { {-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1} }; int sobel_y[3][3] = { {-1, -2, -1}, {0, 0, 0}, {1, 2, 1} }; // 非极大值抑制 void non_maximum_suppression(vector<vector<int>>& grad, vector<vector<double>>& theta) { int rows = grad.size(); int cols = grad[0].size(); for (int i = 1; i < rows - 1; i++) { for (int j = 1; j < cols - 1; j++) { double angle = theta[i][j] * 180 / M_PI; if ((angle >= -22.5 && angle < 22.5) || (angle >= 157.5 && angle <= -157.5)) { if (grad[i][j] < grad[i][j - 1] || grad[i][j] < grad[i][j + 1]) { grad[i][j] = 0; } } else if ((angle >= 22.5 && angle < 67.5) || (angle >= -157.5 && angle < -112.5)) { if (grad[i][j] < grad[i - 1][j - 1] || grad[i][j] < grad[i + 1][j + 1]) { grad[i][j] = 0; } } else if ((angle >= 67.5 && angle < 112.5) || (angle >= -112.5 && angle < -67.5)) { if (grad[i][j] < grad[i - 1][j] || grad[i][j] < grad[i + 1][j]) { grad[i][j] = 0; } } else if ((angle >= 112.5 && angle <= 157.5) || (angle >= -67.5 && angle <= -22.5)) { if (grad[i][j] < grad[i - 1][j + 1] || grad[i][j] < grad[i + 1][j - 1]) { grad[i][j] = 0; } } } } } // 双阈值处理 void double_thresholding(vector<vector<int>>& grad, int T1, int T2) { int rows = grad.size(); int cols = grad[0].size(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (grad[i][j] < T1) { grad[i][j] = 0; } else if (grad[i][j] > T2) { grad[i][j] = 255; } else { grad[i][j] = 128; } } } } // 边缘跟踪 void edge_tracking(vector<vector<int>>& grad, vector<vector<int>>& edges) { int rows = grad.size(); int cols = grad[0].size(); int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; for (int i = 1; i < rows - 1; i++) { for (int j = 1; j < cols - 1; j++) { if (grad[i][j] == 255) { edges[i][j] = grad[i][j]; for (int k = 0; k < 8; k++) { int x = i + dx[k]; int y = j + dy[k]; if (grad[x][y] == 128) { edges[x][y] = 255; } } } } } } // 边缘检测 void detect_edges(vector<vector<int>>& image, vector<vector<int>>& edges, int T1, int T2) { int rows = image.size(); int cols = image[0].size(); vector<vector<int>> grad(rows, vector<int>(cols)); vector<vector<double>> theta(rows, vector<double>(cols)); // Sobel算子 for (int i = 1; i < rows - 1; i++) { for (int j = 1; j < cols - 1; j++) { int gx = 0, gy = 0; for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { gx += image[i + k][j + l] * sobel_x[k + 1][l + 1]; gy += image[i + k][j + l] * sobel_y[k + 1][l + 1]; } } grad[i][j] = sqrt(gx * gx + gy * gy); theta[i][j] = atan2(gy, gx); } } // 非极大值抑制 non_maximum_suppression(grad, theta); // 双阈值处理 double_thresholding(grad, T1, T2); // 边缘跟踪 edge_tracking(grad, edges); } int main() { int T1 = 50; // 低阈值 int T2 = 100; // 高阈值 vector<vector<int>> image = { {0, 0, 0, 0, 0, 0, 0, 0}, {0, 100, 100, 100, 100, 100, 100, 0}, {0, 100, 0, 0, 0, 0, 100, 0}, {0, 100, 0, 100, 100, 0, 100, 0}, {0, 100, 0, 100, 100, 0, 100, 0}, {0, 100, 0, 0, 0, 0, 100, 0}, {0, 100, 100, 100, 100, 100, 100, 0}, {0, 0, 0, 0, 0, 0, 0, 0} }; int rows = image.size(); int cols = image[0].size(); vector<vector<int>> edges(rows, vector<int>(cols)); detect_edges(image, edges, T1, T2); // 输出结果 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << edges[i][j] << " "; } cout << endl; } return 0; } ```

相关推荐

最新推荐

recommend-type

opencv3/C++ 使用Tracker实现简单目标跟踪

今天小编就为大家分享一篇opencv3/C++ 使用Tracker实现简单目标跟踪,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

opencv3/C++图像边缘提取方式

今天小编就为大家分享一篇opencv3/C++图像边缘提取方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python 使用Opencv实现目标检测与识别的示例代码

主要介绍了Python 使用Opencv实现目标检测与识别的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

python+opencv边缘提取与各函数参数解析

其实能写python的就能写opencv,但是工具很总要,代码提示也很重要,你可能会用submit vs等工具,submit编码个人觉得不够智能,vs的话过完年我学的方向不一致,所以没用 推荐 pycharm ,在项目setting中的项目解释器...
recommend-type

OpenCV cv.Mat与.txt文件数据的读写操作

主要介绍了OpenCV cv.Mat 与 .txt 文件数据的读写操作,现在分享给大家,也给大家做个参考
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。