当前位置:网站首页>【OpenCV】-边缘检测汇总示例
【OpenCV】-边缘检测汇总示例
2022-07-31 12:12:00 【我菜就爱学】
汇总之前: 这一节还有一个scharr滤波器(也是算子)没有介绍,先介绍一下。。。
1 计算图像差分:Scharr()函数
使用Scharr滤波器运算符计算x或y方向的图像差分,包含的参数与Sobel算子基本一样,除了没有ksize核的大小
void Scharr(
IntputArray src, //源图
OUtputArray dst, //目标图
int ddepth, //图像深度
int dx, //x方向上的差分阶数
int dy, //y方向上的差分阶数
double scale=1,//缩放因子
double delta=0,//delta值
intborderType=BORDER_DEFAULT//边界模式
);
第一个参数:输入图像,填Mat类型即可
第二个参数:目标图像,函数的输出参数,需要和源图片有一样的尺寸和类型
第三个参数:输出图像的深度,支持如下组合:
- 若src.depth()=CV_8U,取ddepth=-1/CV_16S/CV_32F/CV_64F
- 若src.depth()=CV_16U/CV_16S,取ddepth=-1/CV_32F/CV_64F
- 若src.depth()=CV_32F,取ddepth=-1/CV_32F/CV_64F
- 若src.depth()=CV_64F,取ddepth=-1/CV_64F
第四个参数:x方向上的差分阶数
第五个参数:y方向上的差分阶数
第六个参数:double类型的scale,计算导数值时可选的缩放因子,默认值是1,表示默认情况下是没有应用缩放的。
第七个参数:double类型的delta,表示存入目标图
第八个参数:边界模式。有默认值:BORDER_DEFAULT
5.2 示例程序
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y, dst;
Mat src = imread("E:\\Pec\\fushiyuan.jpg");
imshow("【原始图】", src);
//求x方向上的梯度
Scharr(src, grad_x, CV_16S, 1, 0, 1, 0, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);
imshow("【x方向的效果图】", abs_grad_x);
//求y方向上的梯度
Scharr(src, grad_y, CV_16S, 0, 1, 1, 0, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);
imshow("【y方向的效果图】", abs_grad_y);
//合并梯度
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dst);
imshow("【合并梯度效果图】", dst);
waitKey(0);
return 0;
}
边缘检测汇总示例
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
//================================
// 全局变量声明
//===============================
Mat g_srcImage, g_srcGrayImage, g_dstImage;
//Canny边缘检测相关变量
Mat g_cannyDetectedEdges;
int g_cannyLowThreshold = 1;//TackBar位置参数
//Sobel边缘检测相关变量
Mat g_sobelGradient_X, g_sobelGradient_Y;
Mat g_sobelAbsGradient_X, g_sobelAbsGradient_Y;
int g_sobelKernelSize = 1;//TrackBar位置参数
//Scharr滤波器相关变量
Mat g_scharrGradient_X, g_scharrGradient_Y;
Mat g_scharrAbsGradient_X, g_scharrAbsGradient_Y;
//================================
// 全局函数声明
//===============================
static void on_Canny(int, void *);//Canny边缘检测窗口滚动条的回调函数
static void on_Sobel(int, void *);
void Scharr();//封装了scharr边缘检测相关代码函数
int main()
{
//载入源图
g_srcImage = imread("E:\\Pec\\钢铁侠.jpg");
if (!g_srcImage.data)
{
printf("读取图片错误");
return false;
}
namedWindow("【原始图】");
imshow("【原始图】", g_srcImage);
//创建与src同类型核大小的矩阵(dst)
g_dstImage.create(g_srcImage.size(), g_srcImage.type());
//将原始图像转化为灰度图
cvtColor(g_srcImage,g_srcGrayImage, COLOR_BGR2GRAY);
//创建显示窗口
namedWindow("【Canny效果图】", WINDOW_AUTOSIZE);
namedWindow("【Sobel效果图】", WINDOW_AUTOSIZE);
//创建trackbar
createTrackbar("参数值:", "【Canny效果图】", &g_cannyLowThreshold, 120, on_Canny);
createTrackbar("参数值:", "【Sobel效果图】", &g_sobelKernelSize, 3, on_Sobel);
//调用回调函数
on_Canny(0, 0);
on_Sobel(0, 0);
//调用封装了Scharr边缘检测代码的函数
Scharr();
waitKey(0);
}
//================================
// Canny边缘检测窗口滚动条的回调函数
//===============================
void on_Canny(int, void*)
{
//先使用3x3内核来降噪
blur(g_srcGrayImage, g_cannyDetectedEdges, Size(3, 3));
//运行Cannny算子
Canny(g_cannyDetectedEdges, g_cannyDetectedEdges, g_cannyLowThreshold, g_cannyLowThreshold * 3, 3);
//先将g_dstImage内的所有元素设置为0
g_dstImage = Scalar::all(0);
//使用Canny算子输出的边缘图g_cannyDetectedEdges作为掩码,来将源图拷贝到目标图
g_srcImage.copyTo(g_dstImage, g_cannyDetectedEdges);
//显示效果图
imshow("【Canny效果图】", g_dstImage);
}
//================================
// Sobel边缘检测窗口滚动条的回调函数
//===============================
void on_Sobel(int, void*)
{
//求X方向梯度
Sobel(g_srcImage, g_sobelGradient_X, CV_16S, 1, 0, (2 * g_sobelKernelSize + 1), 1, 1, BORDER_DEFAULT);
convertScaleAbs(g_sobelGradient_X, g_sobelAbsGradient_X);
//求Y方向梯度
Sobel(g_srcImage, g_sobelGradient_Y, CV_16S, 0, 1, (2 * g_sobelKernelSize + 1), 1, 1, BORDER_DEFAULT);
convertScaleAbs(g_sobelGradient_Y, g_sobelAbsGradient_Y);
//合并梯度
addWeighted(g_sobelAbsGradient_X, 0.5, g_sobelAbsGradient_Y, 0.5, 0, g_dstImage);
imshow("【Sobel效果图】", g_dstImage);
}
//================================
// 封装Scharr边缘检测
//===============================
void Scharr()
{
Scharr(g_srcImage, g_scharrGradient_X, CV_16S, 1, 0, 1, 0, BORDER_DEFAULT);
convertScaleAbs(g_scharrGradient_X, g_scharrAbsGradient_X);
Scharr(g_srcImage, g_scharrGradient_Y, CV_16S, 0, 1, 1, 0, BORDER_DEFAULT);
convertScaleAbs(g_scharrGradient_Y, g_scharrAbsGradient_Y);
addWeighted(g_scharrAbsGradient_X, 0.5, g_scharrAbsGradient_Y, 0.5, 0, g_dstImage);
imshow("【Scharr效果图】", g_dstImage);
}
效果图展示:
边栏推荐
- [Shader] Shader official example [easy to understand]
- DCM middleware family welcomes a new member
- Docker搭建Mysql主从复制
- 【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
- The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
- VBA输出日志到工作簿demo
- 认知—运动康复医疗机器人应用设计
- Initial JDBC programming
- JVS低代码能力简介及功能清单
- 消息队列面试题(2022最新整理)
猜你喜欢
生信周刊第38期
busybox之reboot命令流程分析
字符函数和字符串函数
A Week of Wonderful Content Sharing (Issue 14)
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
Distributed id solution
The 2nd activity of the TOGAF10 Standard Reading Club continues wonderfully, and the highlights will be reviewed!
LeetCode - 025. 链表中的两数相加
TOGAF10标准读书会第2场活动精彩继续,高光时刻回顾!
MySQL日志中“binlog”的三种格式玩起来真爽
随机推荐
Quickly learn database management
Use Excel to read data exposed by SAP ABAP CDS View through ODBC
DCM 中间件家族迎来新成员
使用 Excel 读取 SAP ABAP CDS View 通过 ODBC 暴露出来的数据
Qt鼠标穿透
jmeter性能测试步骤入门(性能测试工具jmeter)
WPF中报错:“未将对象引用设置到对象的实例。”
Character Functions and String Functions
How to correctly write the binary stream of the file returned by the server to the local file and save it as a file
MySQL百万数据优化总结 一
[Shader] Shader official example [easy to understand]
「R」使用ggpolar绘制生存关联网络图
CWE4.8 -- 2022年危害最大的25种软件安全问题
Basic use of dosbox [easy to understand]
连续变量离散化教程
Use IN List Population in Your JDBC Application to Avoid Cursor Cache Contention Issues
JVS开发套件产品定位
Docker build Mysql master-slave replication
一周精彩内容分享(第14期)
apisix-Getting Started