当前位置:网站首页>(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;
}
边栏推荐
- A day's work list of an ordinary programmer
- TCP congestion control details | 3 design space
- Kubernetes resource object introduction and common commands (4)
- Golang单元测试、Mock测试以及基准测试
- Detailed explanation of common network attacks
- PS screen printing brush 131, many illustrators have followed suit
- Draw some simple graphics with MFC
- [combinatorics] recursive equation (case where the non-homogeneous part is exponential | example where the non-homogeneous part is exponential)
- Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
- PHP processing - watermark images (text, etc.)
猜你喜欢
MySQL grouping query
Select 3 fcpx plug-ins. Come and see if you like them
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
Vs2013 has blocked the installer, and ie10 needs to be installed
UE4 official charging resources, with a total price of several thousand
List的stream中Long对象与long判等问题记录
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
1147_ Makefile learning_ Target files and dependent files in makefile
Records of long objects and long judgments in the stream of list
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
随机推荐
List的stream中Long对象与long判等问题记录
Servlet specification Part II
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
[combinatorics] generating function (linear property | product property)
1164 Good in C
MinGW compile boost library
Where is the database account used when running SQL tasks in data warehouse tasks configured
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
ArrayList分析3 : 删除元素
Interviewer: why is the value nil not equal to nil?
Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
c# .net 工具生态
PS screen printing brush 131, many illustrators have followed suit
Wechat applet for the first time
Kotlin的协程:上下文
[RT thread] NXP rt10xx device driver framework -- Audio construction and use
Kubernetes resource object introduction and common commands (4)
Introduction to SolidWorks gear design software tool geartrax
Deops入门
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028