当前位置:网站首页>(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
3x3In 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 .
CannyThe 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 , formationCannyThe 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: UseSobelThe 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;
}

边栏推荐
- Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
- Research on Swift
- Mathematical formula (test)
- Talk about the design and implementation logic of payment process
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
- Kubernetes resource object introduction and common commands (4)
- 微服务组件Sentinel控制台调用
- Golang unit test, mock test and benchmark test
- Test your trained model
猜你喜欢

Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
![Luogu: p2685 [tjoi2012] Bridge](/img/f5/f77027288a211ae466781b09ce650f.jpg)
Luogu: p2685 [tjoi2012] Bridge

【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用

Golang unit test, mock test and benchmark test

Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source

TCP congestion control details | 3 design space
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack

QT adjust win screen brightness and sound size

Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028

Automata and automatic line of non-standard design
随机推荐
问题随记 —— 在 edge 上看视频会绿屏
PHP processing - watermark images (text, etc.)
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
国内如何购买Google Colab会员
Golang unit test, mock test and benchmark test
RDS数据库的监测页面在哪看?
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Embedded-c language-7
远程办公工具分享|社区征文
Where is the monitoring page of RDS database?
AcWing 4489. Longest subsequence
Applet setting multi account debugging
Enterprise custom form engine solution (XI) -- form rule engine 1
毕业总结
Mathematical formula (test)
Research on Swift
PR second time
Inheritance of ES6 class
List of financial products in 2022
Stm32h7 Hal library SPI DMA transmission has been in busy solution