当前位置:网站首页>[opencv] [image gradient]
[opencv] [image gradient]
2022-07-02 12:52:00 【A sea of stars】
Let's calculate the gradient of each pixel in the image
We can use first-order Sobel Operator and Scharr operator , And use secondary Laplace operator , The experiment is as follows :
The original image is :
The gradient of the first-order operator is calculated as follows :
Find the gradient of each pixel of the image . Calculated above sobel The operator can be approximated as X and Y Directional gradient , So the total gradient is :
G = (Gx^2 + Gy ^2) ^ 0.5 Or it can be approximately calculated as
G = |Gx| + |Gy|
Second order operator , The gradient can be obtained directly .
import numpy as np
import random
import cv2
import matplotlib.pyplot as plt
# Show the image , Encapsulate as a function
def cv_show_image(name, img):
cv2.imshow(name, img)
cv2.waitKey(0) # Waiting time , In milliseconds ,0 Represents any key termination
cv2.destroyAllWindows()
img = cv2.imread('images/yuan.png') # Read the original image
print(img.shape)
# Sobel operator , For example, the gradient in the right direction
# [[-1, 0, 1],
# Gx = [-2, 0, 2],
# [-1, 0, 1]]
# For example, it is a downward gradient
# [[-1, -2, -1],
# Gy = [0, 0, 0],
# [1, 2, 1]]
# It is equivalent to subtracting the pixel value on the left from the pixel value on the right
# dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
# src: Input the original image
# ddepth: The depth of the image , It's usually -1
# dx and dy: Horizontal and vertical
# ksize: The size of the convolution kernel
# Let's look at the results of operations in different directions
# The result of pixel subtraction is positive and negative , If there is a negative number ,opencv The default truncation is 0
# But what we want to see is the size of the difference , Even negative numbers have different values , Cannot be smeared 0 No difference , So we need to save negative numbers , Negative calculation results can be retained .
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, 3) # The image is converted to a form with a negative number , Because the number of digits is higher .
sobel_x_abs = cv2.convertScaleAbs(sobel_x) # Take the absolute value , Keep our difference value
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0 ,1, 3) # The image is converted to a form with a negative number , Because the number of digits is higher .
sobel_y_abs = cv2.convertScaleAbs(sobel_y) # Take the absolute value , Keep our difference value
# Find the gradient of each pixel of the image . Calculated above sobel The operator can be approximated as X and Y Directional gradient , So the total gradient is :
# G = (Gx^2 + Gy^2)^0.5 Or it can be approximately calculated as
# G = |Gx| + |Gy|
sobel_xy_add = cv2.addWeighted(sobel_x_abs, 0.5, sobel_y_abs, 0.5, 0) # Use x + y To find the gradient .
sobel_xy_abs_add = cv2.convertScaleAbs(sobel_xy_add) # Take the absolute value , Keep our difference value
# The following method is not recommended , This effect is not very good .
sobel_xy = cv2.Sobel(img, cv2.CV_64F, 1, 1, 3) # The image is converted to a form with a negative number , Because the number of digits is higher .
sobel_xy_abs = cv2.convertScaleAbs(sobel_xy) # Take the absolute value , Keep our difference value
ret = np.hstack((sobel_x, sobel_y, sobel_xy_add, sobel_xy))
cv2.imshow('sobel_src', ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
ret = np.hstack((sobel_x_abs, sobel_y_abs, sobel_xy_abs_add, sobel_xy_abs))
cv2.imshow('sobel_abs', ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Two other operators for finding image gradient , This and Sobel Operators are very similar , Or the degree of emphasis is different
# Scharr operator
# [[-3, 0, 3],
# Gx = [-10, 0, 10],
# [-3, 0, 3]]
# For example, it is a downward gradient
# [[-3, -10, -3],
# Gy = [0, 0, 0],
# [3, 10, 3]]
# Let's look at the results of operations in different directions
# The result of pixel subtraction is positive and negative , If there is a negative number ,opencv The default truncation is 0
# But what we want to see is the size of the difference , Even negative numbers have different values , Cannot be smeared 0 No difference , So we need to save negative numbers , Negative calculation results can be retained .
scharr_x = cv2.Scharr(img, cv2.CV_64F, 1, 0, 3) # The image is converted to a form with a negative number , Because the number of digits is higher .
scharr_x_abs = cv2.convertScaleAbs(scharr_x) # Take the absolute value , Keep our difference value
scharr_y = cv2.Scharr(img, cv2.CV_64F, 0 ,1, 3) # The image is converted to a form with a negative number , Because the number of digits is higher .
scharr_y_abs = cv2.convertScaleAbs(scharr_y) # Take the absolute value , Keep our difference value
# Find the gradient of each pixel of the image . Calculated above sobel The operator can be approximated as X and Y Directional gradient , So the total gradient is :
# G = (Gx^2 + Gy^2)^0.5 Or it can be approximately calculated as
# G = |Gx| + |Gy|
scharr_xy_add = cv2.addWeighted(scharr_x_abs, 0.5, scharr_y_abs, 0.5, 0) # Use x + y To find the gradient .
scharr_xy_abs_add = cv2.convertScaleAbs(scharr_xy_add) # Take the absolute value , Keep our difference value
ret = np.hstack((scharr_x, scharr_y, scharr_xy_add))
cv2.imshow('scharr_src', ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
ret = np.hstack((scharr_x_abs, scharr_y_abs, scharr_xy_abs_add))
cv2.imshow('scharr_abs', ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Laplace operator
# Laplace The operator can approximately calculate the second derivative of the image , It has rotation invariance , That is, edges in all directions can be detected .
# Laplace There are two kinds of operators , Consider separately 4- Adjacency (D4) and 8- Adjacency (D8) Second order differential of two neighborhoods .
# For example, one 3x3 Of 4- Adjacency is
# for example 4- Adjacency
# [[0, -1, 0],
# [-1, 4, -1],
# [ 0, -1, 0]]
# for example 8- Adjacency
# [[-1, -1, -1],
# [ -1, 8, -1],
# [ -1, -1, -1]]
Laplacian = cv2.Laplacian(img, cv2.CV_64F) # Because it is second-order , No, x and y The concept of direction
Laplacian_abs = cv2.convertScaleAbs(Laplacian) # Take the absolute value , Keep our difference value
cv_show_image('Laplacian', Laplacian)
# Finally, summarize the effects of the three methods
ret = np.hstack((sobel_xy_abs_add, scharr_xy_abs_add, Laplacian_abs))
cv2.imshow('all_here', ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
The effect is as follows :
Use Sobel operator , The effect of not adding absolute value operation :
Use Sobel operator , Add the effect of absolute value operation :
Use Scharr operator , The effect of not adding absolute value operation :
Use Scharr operator , Add the effect of absolute value operation :
Use Laplace operator , The effect of adding absolute value and not adding absolute value 
Finally, add the absolute value to each xy Summary of directions , The effect is as follows :

The overall effect shows , The effect of using second-order operators is better .
边栏推荐
- Interval DP acwing 282 Stone merging
- Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)
- Std:: vector batch import fast de duplication method
- Browser storage scheme
- 2.6 using recursion and stack - [tower of Hanoi problem]
- Docsify deploy IIS
- Rust search server, rust quick service finding tutorial
- 浏览器存储方案
- spfa AcWing 852. spfa判断负环
- Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
猜你喜欢

Heap acwing 838 Heap sort
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

VLAN experiment
![[FFH] little bear driver calling process (take calling LED light driver as an example)](/img/e7/153ae9f1befc12825d277620049f9d.jpg)
[FFH] little bear driver calling process (take calling LED light driver as an example)

浏览器node事件循环

js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)

Browser node event loop

Hash table acwing 840 Simulated hash table

Five best software architecture patterns that architects must understand

js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
随机推荐
Mongodb redis differences
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
JSON serialization and parsing
spfa AcWing 852. spfa判断负环
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
Interesting interview questions
基于STM32的OLED 屏幕驱动
std::vector批量导入快速去重方法
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
spfa AcWing 852. SPFA judgement negative ring
Floyd AcWing 854. Floyd求最短路
[ybtoj advanced training guidance] judgment overflow [error]
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Heap acwing 838 Heap sort
Interview questions for software testing - a collection of interview questions for large factories in 2022
移动式布局(流式布局)
哈希表 AcWing 841. 字符串哈希
[ybtoj advanced training guide] similar string [string] [simulation]
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
区间DP AcWing 282. 石子合并