current position：Home>Complex learning of Python opencv Sobel operator, ScHARR operator and Laplacian operator
Complex learning of Python opencv Sobel operator, ScHARR operator and Laplacian operator
20220130 07:22:52 【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 46 piece .
Basic knowledge
About Sobel operator 、Scharr operator 、laplacian The operator is in This blog China has learned , A second study , We can have a slightly deeper understanding of the convolution kernel of the operator .
Sobel operator
When using this function , Convolution kernel at X Direction is ： $\begin{bmatrix} 1&0&+1\\ 2&0&+2\\1&0&+1 \end{bmatrix}$, stay Y Direction is $\begin{bmatrix} 1&2&1\\ 0&0&0\\+1&+2&+1 \end{bmatrix}$
The convolution kernel above is one 3x3
Matrix , When it is convoluted with an image , If the covering matrix is $\begin{bmatrix} p_1&p_2&p_3\\ p_4&p_5&p_6\\p_7&p_8&p_9 \end{bmatrix}$
After calculation, you will get the following results $p_3p_1+p_6p_4+p_9p_7$ , The bigger the result is , The more obvious the difference , And why $p_4$ And $p_6$ spot , The value of convolution kernel is large , The simple understanding is that this point is close to the central point .
Write the following code first ：
import cv2 as cv
import numpy as np
src = cv.imread('./star.png')
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY_INV)
# Sobel Operator computing edge
sobel_x = cv.Sobel(thresh, 1, 1, 0, ksize=3)
image = np.hstack((gray, thresh, sobel_x))
cv.imshow("image", image)
cv.waitKey()
Copy code
The operation results are as follows ：
The last picture captures the left side of the figure , The reason is that . Sobel The time of calculation is Right minus left 、 Lower minus upper , Looking at the binary graph, you will find , The reason why right minus left will get the left edge is , Both sides of the left edge of the figure , White on the right 255, Black on the left 0, So you can get the edge , In the same way , At the right edge of the figure , Both sides are black on the right 、 Left white , So the edge is missing .
If you want the right edge to appear at the same time , You need to use the following functions , Get the absolute value of the negative value .
Another code change is in the code comments section ：
# Sobel Operator computing edge
# Pay attention to the calculation sobel_x When passing parameters to a function , The second parameter is from 1 It is amended as follows cv.CV_64F, The purpose is to get a negative value , Facilitate the subsequent operation of obtaining absolute value .
sobel_x = cv.Sobel(thresh, cv.CV_64F, 1, 0, ksize=3)
sobel_x = cv.convertScaleAbs(sobel_x)
image = np.hstack((gray, thresh, sobel_x))
Copy code
The above code calculates X The edge of the direction , Similarly, calculate Y The edge of the direction , Merging in X And Y The edge of the direction , You can get the final image edge .
import cv2 as cv
import numpy as np
src = cv.imread('./star.png')
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY_INV)
# Sobel Operator computing edge
sobel_x = cv.Sobel(thresh, cv.CV_64F, 1, 0, ksize=3)
sobel_y = cv.Sobel(thresh, cv.CV_64F, 0, 1, ksize=3)
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)
image = np.hstack((gray, sobel_xy, sobel_x, sobel_y))
cv.imshow("image", image)
cv.waitKey()
Copy code
After merging, the results are as follows , Direct calculation is generally not recommended X and Y The direction of the Sobel, Instead, it should be calculated separately before merging .
You can compare the effect difference between separate calculation and consolidation and direct calculation .
The above picture is the result of running the following code
import cv2 as cv
import numpy as np
src = cv.imread('./t3.jpg')
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY_INV)
# Sobel Operators are calculated separately
sobel_x = cv.Sobel(thresh, cv.CV_64F, 1, 0, ksize=3)
sobel_y = cv.Sobel(thresh, cv.CV_64F, 0, 1, ksize=3)
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)
# Direct calculation
sobel_xy1 = cv.Sobel(thresh, cv.CV_64F, 1, 1, ksize=3)
sobel_xy1 = cv.convertScaleAbs(sobel_xy1)
image = np.hstack((gray, sobel_xy, sobel_xy1))
cv.imshow("image", image)
cv.waitKey(0)
Copy code
Scharr operator
This operator has a ratio Sobel Better accuracy , Mainly because its convolution kernel is the following data $G_x =\begin{bmatrix}3&0&+3\\ 10&0&+10\\3&0&+3 \end{bmatrix}$ $G_y =\begin{bmatrix}3&10&3\\ 0&0&0\\3&10&3 \end{bmatrix}$ When used, it is still calculated separately
import cv2 as cv
import numpy as np
src = cv.imread('./t3.jpg')
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY_INV)
# Scharr Operators are calculated separately
scharr_x = cv.Scharr(thresh, cv.CV_64F, 1, 0)
scharr_y = cv.Scharr(thresh, cv.CV_64F, 0, 1)
scharr_x = cv.convertScaleAbs(scharr_x)
scharr_y = cv.convertScaleAbs(scharr_y)
scharr_xy = cv.addWeighted(scharr_x, 0.5, scharr_y, 0.5, 0)
image = np.hstack((gray, scharr_xy))
cv.imshow("image", image)
cv.waitKey(0)
Copy code
laplacian operator
The convolution kernel of the estimator is as follows ： $G =\begin{bmatrix}0&1&0\\ 1&4&1\\0&1&0 \end{bmatrix}$ laplacian Operator noise sensitive , In use, it is necessary to denoise in advance .
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/202201300722494763.html
The sidebar is recommended
 [recalling the 1970s] using Python to repair the wonderful memories of parents' generation, blackandwhite 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 programming4 Hello World
 Summary of Python technical points
 11.5K Star！ An open source Python static type checking Library
 Chapter 2: Fundamentals of python1 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