当前位置:网站首页>(9) Opencv Canny edge detection
(9) Opencv Canny edge detection
2022-07-03 17:45:00 【Hengyoucheng】
1. Basic principle
Reference from 《 Digital image processing 》 Chapter ten
And OpenCV Tutorial Canny Edge Detector
1.1 Overview of edge detection
Edge detection is a common method to segment an image according to gray mutation . Edge models can be classified according to their gray Profile , Usually divided into step model , Slope model and roof edge model . Step models are often found in computer-generated images , Such as solid modeling and animation . Most of the actual images are slope edge models . When using two-step degree to obtain image edge , The second derivative will produce a local maximum positive value and a local minimum negative value , Reflected in the image as a bilinear effect .
1.2Canny edge detection
1986 Year by year John F. Canny
stay A computational approach to edge detection. It is proposed that ,Canny
The algorithm is mainly divided into the following steps :
- Use Gaussian kernel to smooth the input image
- Calculate gradient amplitude image and angle image
- Apply non maximum suppression to gradient amplitude images
- Use double threshold processing and connectivity analysis to detect and connect edges
Discuss separately ,
Gaussian kernel smoothing image denoising
Calculate the gradient amplitude diagram and gradient direction diagram , Make
f(x,y)
Represents the input image , The gradient amplitude and direction are :
M ( x , y ) = ∣ ∣ ▽ f ( x , y ) ∣ ∣ = g x 2 ( x , y ) + g y 2 ( x , y ) M(x,y)=||\triangledown f(x,y)||=\sqrt{g_x^2(x,y)+g_y^2(x,y)} M(x,y)=∣∣▽f(x,y)∣∣=gx2(x,y)+gy2(x,y)
α ( x , y ) = a r c t a n [ g y ( x , y ) g x ( x , y ) ] \alpha(x,y)=arctan[\frac{g_y(x,y)}{g_x(x,y)}] α(x,y)=arctan[gx(x,y)gy(x,y)],
among , g x ( x , y ) = ∂ f ( x , y ) ∂ x g_x(x,y)=\frac{\partial f(x,y)}{\partial x} gx(x,y)=∂x∂f(x,y), g y ( x , y ) = ∂ f ( x , y ) ∂ y g_y(x,y)=\frac{\partial f(x,y)}{\partial y} gy(x,y)=∂y∂f(x,y), You can use ( 7、 ... and ) Operators commonly used in image processing Laplacian\Sobel\Roberts\Prewitt\Kirsch To find g x ( x , y ) g_x(x,y) gx(x,y) and g y ( x , y ) g_y(x,y) gy(x,y).Apply non maximum suppression to gradient amplitude images , Gradient image ∣ ∣ ▽ f ( x , y ) ∣ ∣ ||\triangledown f(x,y)|| ∣∣▽f(x,y)∣∣ It usually contains some wide ridges near the local maximum , You can use non maximum suppression to refine these wide ridges . In a
3x3
In the area , Can define 4 Edge normals ( Gradient vector ) Of 4 A direction , level , vertical ,+45 degree , -45 degree , The angle range in each direction is shown in the figure below ,
Use d 1 , d 2 , d 3 , d 4 d_1,d_2,d_3,d_4 d1,d2,d3,d4 Represents the foregoing 3x3
Regional 4 Basic edge directions , To α \alpha α Any point in ( x , y ) (x,y) (x,y) Centred 3x3
Area , The corresponding non maximum suppression scheme is :
- 1) seek α ( x , y ) \alpha(x,y) α(x,y) The direction of d k d_k dk
- 2) Make K Express ∣ ∣ ▽ f ∣ ∣ ||\triangledown f|| ∣∣▽f∣∣ stay ( x , y ) (x,y) (x,y) Place the value of the . if K Less than d k d_k dk Point in the direction (x,y) At one or two adjacent points of ∣ ∣ ▽ f ∣ ∣ ||\triangledown f|| ∣∣▽f∣∣ value , Then order g N ( x , y ) = 0 g_N(x,y)=0 gN(x,y)=0; otherwise , Make g N ( x , y ) = K g_N(x,y)=K gN(x,y)=K
Yes (x,y) Repeat this process for all values of , Get a picture with f ( x , y ) f(x,y) f(x,y) Non maximum suppression image with the same size g N ( x , y ) g_N(x,y) gN(x,y), Images g N ( x , y ) g_N(x,y) gN(x,y) Only the refined edges are included in .
- Use double threshold processing and connectivity analysis to detect and connect edges , obtain g N ( x , y ) g_N(x,y) gN(x,y) After that, threshold processing is carried out to obtain the edge .
Canny
The algorithm uses lag threshold processing , Set two thresholds , Low threshold T L T_L TL And high threshold T H T_H TH. The hysteresis threshold operation can be regarded as creating two additional images :
g N H ( x , y ) = g N ( x , y ) ≥ T H g_{NH}(x,y)=g_N(x,y)\ge T_H gNH(x,y)=gN(x,y)≥TH
g N L ( x , y ) = g N ( x , y ) ≥ T L g_{NL}(x,y)=g_N(x,y)\ge T_L gNL(x,y)=gN(x,y)≥TL
And g N L ( x , y ) g_{NL}(x,y) gNL(x,y) comparison g N H ( x , y ) g_{NH}(x,y) gNH(x,y) Non zero values are less , g N H ( x , y ) g_{NH}(x,y) gNH(x,y) All non-zero pixels in g N L ( x , y ) g_{NL}(x,y) gNL(x,y) in , adopt
g N L ( x , y ) = g N L ( x , y ) − g N H ( x , y ) g_{NL}(x,y) = g_{NL}(x,y) - g_{NH}(x,y) gNL(x,y)=gNL(x,y)−gNH(x,y) from g N L ( x , y ) g_{NL}(x,y) gNL(x,y) Delete from g N H ( x , y ) g_{NH}(x,y) gNH(x,y) Non zero pixels of . After the above reduction g N L ( x , y ) g_{NL}(x,y) gNL(x,y) and g N H ( x , y ) g_{NH}(x,y) gNH(x,y) respectively “ weak ” Edge pixels and “ strong ” Edge pixels . g N H ( x , y ) g_{NH}(x,y) gNH(x,y) The edge pixels in are assumed to be valid edge pixels , But it usually has cracks , It is usually necessary to form longer edges by the following treatment :- 1) stay g N H ( x , y ) g_{NH}(x,y) gNH(x,y) Locate the next edge pixel that is not accessed p
- 2) take g N L ( x , y ) g_{NL}(x,y) gNL(x,y) China and p 8 All weak pixels in the connected domain are marked as effective edge pixels
- 3) if g N H ( x , y ) g_{NH}(x,y) gNH(x,y) All non-zero pixels in have been accessed , Go to 4, Otherwise return to 1
- 4) take g N L ( x , y ) g_{NL}(x,y) gNL(x,y) All pixels in that are not marked as valid edge pixels are set to 0
Go through the above steps , take g N L ( x , y ) g_{NL}(x,y) gNL(x,y) All non-zero pixels in are appended to g N H ( x , y ) g_{NH}(x,y) gNH(x,y) On , formationCanny
The final image output by the operator .
2.OpenCV API
void cv::Canny (
InputArray image,
OutputArray edges,
double threshold1,
double threshold2,
int apertureSize = 3,
bool L2gradient = false
)
image
: Single channel grayscaleedges
: Store images of edge pixelsthreshold1
: Super parameter of hysteresis threshold processingthreshold2
: Super parameter of hysteresis threshold processingapertureSize
: UseSobel
The convolution kernel size of the operatorL2gradient
: Whether to use L 2 L_2 L2 norm
3. Example
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src, src_gray;
Mat dst, detected_edges;
int low_threshold = 0;
const int max_low_threshold = 100;
const int ratio = 3;
const int ks = 3;
const char *win_name = "Canny Edge Map";
static void CannyThreshold(int, void*)
{
blur(src_gray, detected_edges, Size(3,3));
Canny(detected_edges, detected_edges, low_threshold, low_threshold*ratio, ks);
dst = Scalar::all(0);
src.copyTo(dst, detected_edges);
imshow(win_name, dst);
imwrite("seg_res.png", dst);
}
int main(int argc, char **argv)
{
CommandLineParser parser( argc, argv, "{@input | fruits.jpg | input image}" );
src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR ); // Load an image
if( src.empty() )
{
std::cout << "Could not open or find the image!\n" << std::endl;
std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl;
return -1;
}
dst.create( src.size(), src.type() );
cvtColor( src, src_gray, COLOR_BGR2GRAY );
namedWindow( win_name, WINDOW_AUTOSIZE);
createTrackbar( "Min Threshold:", win_name, &low_threshold, max_low_threshold, CannyThreshold );
CannyThreshold(0, 0);
waitKey(0);
return 0;
}
边栏推荐
- WEB-UI自动化测试-最全元素定位方法
- PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
- Graduation summary
- POM in idea XML graying solution
- vs2013已阻止安装程序,需安装IE10
- Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
- c# .net 工具生态
- 【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
- Comparison of kotlin collaboration + retro build network request schemes
- Five problems of database operation in commodity supermarket system
猜你喜欢
1164 Good in C
Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
SWM32系列教程4-端口映射及串口应用
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
Talk about the design and implementation logic of payment process
Embedded-c language-7
[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
随机推荐
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Where is the monitoring page of RDS database?
Getting started with deops
【JokerのZYNQ7020】DDS_ Compiler。
Where is the database account used when running SQL tasks in data warehouse tasks configured
TensorBoard快速入门(Pytorch使用TensorBoard)
AcWing 3438. Number system conversion
Kotlin的协程:上下文
Kubernetes resource object introduction and common commands (4)
Type conversion, variable
1164 Good in C
Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
STM32实现74HC595控制
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
Y is always discrete and can't understand, how to solve it? Answer: read it several times
Leetcode 669 pruning binary search tree -- recursive method and iterative method
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
UE4 official charging resources, with a total price of several thousand