current position:Home>Python opencv Canny edge detection knowledge supplement
Python opencv Canny edge detection knowledge supplement
2022-01-30 07:22:49 【Dream eraser】
Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities
This article has participated in 「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .
Python OpenCV 365 Day study plan , Go into the field of image with the eraser . This blog is the third in this series 47 piece .
Learning ahead
stay Canny Edge extraction related knowledge learning , Image processing part 32 blog In this blog , We're right Canny
Basic learning of edge detection , Today's article is mainly used to supplement it , Of course, knowledge is not difficult ,1 You can learn in an hour .
Canny Edge detection process
Refer to the most published processes on the Internet
- Gaussian blur denoising ;
- Calculate the gradient amplitude and direction of the image , What is commonly used here is Sobel operator ;
- Non maximum suppression , The simple understanding is “ Thin edge ”;
- Double threshold detection , Also called lag threshold .
Follow the above steps to translate , You can write one Canny A simple case
import cv2 as cv
import numpy as np
src = cv.imread("./t7.jpg", 0)
# Gaussian blur
gaussian = cv.GaussianBlur(src, (3, 3), 0)
ret, thresh = cv.threshold(gaussian, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
# Calculate the gradient amplitude and direction of the image , What is commonly used here is Sobel operator ;
sobel_x = cv.Sobel(thresh, cv.CV_16SC1, 1, 0)
sobel_y = cv.Sobel(thresh, cv.CV_16SC1, 0, 1)
sobel_x = cv.convertScaleAbs(sobel_x)
sobel_y = cv.convertScaleAbs(sobel_y)
sobel_xy = cv.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
canny1 = cv.Canny(sobel_xy, 50, 150)
image = np.hstack((thresh, canny1))
cv.imshow('img', image)
cv.waitKey(0)
cv.destroyAllWindows()
Copy code
There are some things to explain in the code , Double threshold detection adopts high and low threshold detection , High threshold usage
maxVal
、 Low threshold use minVal
, There are the following conclusions .
- Gradient value > maxVal , Boundary preservation ;
- minVal < Gradient value < maxVal when , If the pixel gradient value is on the boundary , Retain , otherwise , Abandon ;
- Gradient value < minVal, Abandon .
The vernacular of the above three points , Very easy to remember , If you want more boundaries , The small minVal
value , Otherwise, turn it up .
In the actual operation, I also try to find a problem , If binarization is performed before edge extraction , The resulting image is less affected by the above parameters , For example, the following code , Double threshold setting different values , The results obtained are basically the same .
import cv2 as cv
import numpy as np
src = cv.imread("./t77.jpg", 0)
# Gaussian blur
gaussian = cv.GaussianBlur(src, (3, 3), 0)
ret, thresh = cv.threshold(gaussian, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
# Calculate the gradient amplitude and direction of the image , What is commonly used here is Sobel operator ;
sobel_x = cv.Sobel(thresh, cv.CV_16SC1, 1, 0)
sobel_y = cv.Sobel(thresh, cv.CV_16SC1, 0, 1)
sobel_x = cv.convertScaleAbs(sobel_x)
sobel_y = cv.convertScaleAbs(sobel_y)
sobel_xy = cv.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
canny1 = cv.Canny(sobel_xy, 10, 30)
canny2 = cv.Canny(sobel_xy, 30, 90)
canny3 = cv.Canny(sobel_xy, 50, 150)
image = np.hstack((canny1, canny2,canny3))
cv.imshow('img', image)
cv.waitKey(0)
cv.destroyAllWindows()
Copy code
If you remove binarization , There is a clear difference in the edge of the image , Now I understand why I was just explaining Canny Edge extraction steps , There is no binarization step , It seems superfluous .
# Gaussian blur
gaussian = cv.GaussianBlur(src, (3, 3), 0)
# ret, thresh = cv.threshold(gaussian, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
# Calculate the gradient amplitude and direction of the image , What is commonly used here is Sobel operator ;
Copy code
There's a little problem , It was found during the test , The edges obtained by using the following two pieces of code are inconsistent , As follows , You can test separately . The difference is in the way 1 adopt Sobel Operators calculate x Direction and y Directional gradient , After using the merged image Canny edge detection , The resulting edge is a double line , Method 2 Better effect, ideal .
# Method 1
import cv2 as cv
import numpy as np
src = cv.imread("./t77.jpg", 0)
# Gaussian blur
gaussian = cv.GaussianBlur(src, (3, 3), 0)
# Calculate the gradient amplitude and direction of the image , What is commonly used here is Sobel operator ;
sobel_x = cv.Sobel(gaussian, cv.CV_16SC1, 1, 0)
sobel_y = cv.Sobel(gaussian, cv.CV_16SC1, 0, 1)
sobel_x = cv.convertScaleAbs(sobel_x)
sobel_y = cv.convertScaleAbs(sobel_y)
sobel_xy = cv.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
canny1 = cv.Canny(sobel_xy, 50, 150)
# canny2 = cv.Canny(sobel_x,sobel_y, 50, 150)
image = np.hstack((gaussian, canny1))
cv.imshow('img', image)
cv.waitKey(0)
cv.destroyAllWindows()
### Method 2
import cv2 as cv
import numpy as np
src = cv.imread("./t77.jpg", 0)
# Gaussian blur
gaussian = cv.GaussianBlur(src, (3, 3), 0)
# Calculate the gradient amplitude and direction of the image , What is commonly used here is Sobel operator ;
sobel_x = cv.Sobel(gaussian, cv.CV_16SC1, 1, 0)
sobel_y = cv.Sobel(gaussian, cv.CV_16SC1, 0, 1)
canny2 = cv.Canny(sobel_x, sobel_y, 50, 150)
image = np.hstack((gaussian, canny2))
cv.imshow('img', image)
cv.waitKey(0)
cv.destroyAllWindows()
Copy code
Unfortunately Non maximum suppression The learning of relevant knowledge , It needs to be postponed , The current knowledge reserve is insufficient , It's hard to learn , We'll see you later .
Eraser bars
I hope today's 1 You get something in an hour , I'll see you on our next blog ~
copyright notice
author[Dream eraser],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300722474982.html
The sidebar is recommended
- [recalling the 1970s] using Python to repair the wonderful memories of parents' generation, black-and-white photos become color photos
- You used to know Python advanced
- Pyinstaller package Python project
- 2021 IEEE programming language rankings: Python tops the list!
- Implementation of Python automatic test control
- Python advanced: [Baidu translation reverse] graphic and video teaching!!!
- Do you know the fuzzy semantics in Python syntax?
- [Python from introduction to mastery] (XXVII) learn more about pilot!
- Playing excel office automation with Python
- Some applications of heapq module of Python module
guess what you like
-
Python and go languages are so popular, which is more suitable for you?
-
Python practical skills task segmentation
-
Python simulated Login, numpy module, python simulated epidemic spread
-
Python opencv contour discovery function based on image edge extraction
-
Application of Hoff circle detection in Python opencv
-
Python reptile test ox knife (I)
-
Day 1: learn the Django framework of Python development
-
django -- minio_ S3 file storage service
-
[algorithm learning] 02.03 Delete intermediate nodes (Java / C / C + + / Python / go)
-
Similarities and differences of five pandas combinatorial functions
Random recommended
- Learning in Python + opencv -- extracting corners
- Python beginner's eighth day ()
- Necessary knowledge of Python: take you to learn regular expressions from zero
- Get your girlfriend's chat records with Python and solve the paranoia with one move
- My new book "Python 3 web crawler development practice (Second Edition)" has been recommended by the father of Python!
- From zero to familiarity, it will take you to master the use of Python len() function
- Python type hint type annotation guide
- leetcode 108. Convert Sorted Array to Binary Search Tree(python)
- For the geometric transformation of Python OpenCV image, let's first talk about the extraordinary resize function
- leetcode 701. Insert into a Binary Search Tree (python)
- For another 3 days, I sorted out 80 Python datetime examples, which must be collected!
- Python crawler actual combat | using multithreading to crawl lol HD Wallpaper
- Complete a python game in 28 minutes, "customer service play over the president card"
- The universal Python praise machine (commonly known as the brushing machine) in the whole network. Do you want to know the principle? After reading this article, you can write one yourself
- How does Python compare file differences between two paths
- Common OS operations for Python
- [Python data structure series] linear table - explanation of knowledge points + code implementation
- How Python parses web pages using BS4
- How do Python Network requests pass parameters
- Python core programming - decorator
- Python Network Programming -- create a simple UPD socket to realize mutual communication between two processes
- leetcode 110. Balanced Binary Tree(python)
- Django uses Django celery beat to dynamically add scheduled tasks
- The bear child said "you haven't seen Altman" and hurriedly studied it in Python. Unexpectedly
- Optimization iteration of nearest neighbor interpolation and bilinear interpolation algorithm for Python OpenCV image
- Bilinear interpolation algorithm for Python OpenCV image, the most detailed algorithm description in the whole network
- Use of Python partial()
- Python game development, pyGame module, python implementation of angry birds
- leetcode 1104. Path In Zigzag Labelled Binary Tree(python)
- Save time and effort. 10 lines of Python code automatically clean up duplicate files in the computer
- Learn python, know more meat, and be a "meat expert" in the technical circle. One article is enough
- [Python data structure series] "stack (sequential stack and chain stack)" -- Explanation of knowledge points + code implementation
- Datetime module of Python time series
- Python encrypts and decrypts des to solve the problem of inconsistency with Java results
- Chapter 1: introduction to Python programming-4 Hello World
- Summary of Python technical points
- 11.5K Star! An open source Python static type checking Library
- Chapter 2: Fundamentals of python-1 grammar
- [Python daily homework] day4: write a function to count the number of occurrences of each number in the incoming list and return the corresponding dictionary.
- Python uses turtle to express white