当前位置:网站首页>(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;
}
边栏推荐
- September, 19, "cam principle and application" online assignment [Full Score answer]
- Leetcode 669 pruning binary search tree -- recursive method and iterative method
- Draw some simple graphics with MFC
- 聊聊支付流程的設計與實現邏輯
- [combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
- Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
- TCP congestion control details | 3 design space
- Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
- [RT thread] NXP rt10xx device driver framework -- Audio construction and use
- Leetcode Valentine's Day Special - looking for a single dog
猜你喜欢
Play with fancy special effects. This AE super kit is for you
Swm32 series Tutorial 4 port mapping and serial port application
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
1164 Good in C
[RT thread] NXP rt10xx device driver framework -- Audio construction and use
Draw some simple graphics with MFC
STM32实现74HC595控制
Embedded-c language-7
QT adjust win screen brightness and sound size
鸿蒙第三次培训
随机推荐
QT adjust win screen brightness and sound size
[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
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
Play with fancy special effects. This AE super kit is for you
Golang unit test, mock test and benchmark test
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
Introduction to SolidWorks gear design software tool geartrax
鸿蒙第四次培训
Basic grammar of interview (Part 2)
A day's work list of an ordinary programmer
PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
Deops入门
The difference between get and post
Where is the monitoring page of RDS database?
Graduation summary
Design e-commerce spike
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
PHP returns 500 errors but no error log - PHP return 500 error but no error log
Tensorboard quick start (pytoch uses tensorboard)
自动渗透测试工具核心功能简述