当前位置:网站首页>(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;
}
边栏推荐
- STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
- Implementation of Tetris in C language
- 鸿蒙第三次培训
- Graduation summary
- The gbase 8A database does not support the DB2 function value (column_name, 0) cluster syntax
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- Where is the database account used when running SQL tasks in data warehouse tasks configured
- IntelliJ 2021.3 short command line when running applications
- Web-ui automated testing - the most complete element positioning method
- SQL injection database operation foundation
猜你喜欢
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Interviewer: why is the value nil not equal to nil?
How to install PHP on Ubuntu 20.04
MySQL grouping query
Talk about the design and implementation logic of payment process
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
Vs2013 has blocked the installer, and ie10 needs to be installed
[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
Leetcode 669 pruning binary search tree -- recursive method and iterative method
随机推荐
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
Draw some simple graphics with MFC
Applet setting multi account debugging
MySQL grouping query
How to read the source code [debug and observe the source code]
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
i++与++i的区别:通俗易懂的讲述他们的区别
Comparison of kotlin collaboration + retro build network request schemes
1146_ SiCp learning notes_ exponentiation
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
[combinatorics] recursive equation (case where the non-homogeneous part is exponential | example where the non-homogeneous part is exponential)
Five problems of database operation in commodity supermarket system
面试官:值为 nil 为什么不等于 nil ?
Detailed explanation of common network attacks
Select 3 fcpx plug-ins. Come and see if you like them
Stm32h7 Hal library SPI DMA transmission has been in busy solution
Loop through JSON object list
WEB-UI自动化测试-最全元素定位方法
[combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
[combinatorics] recursive equation (the problem of solving recursive equation with multiple roots | the problem is raised)