02 How to calculate the gradient


foreword

Record the relevant programming content of the gradient in image sharpening. The language used in this article is python, and the model is Ms. Lena.


1. Theoretical reserve

Image sharpening and image smoothing are opposite operations. Sharpening is to reduce the blur in the image by enhancing high-frequency components, enhance image details, edges and contours, and enhance grayscale contrast, which is convenient for later identification and processing of targets. Sharpening increases the noise of the image while enhancing the edge of the image. Methods usually include differential method and high-pass filtering method .

For specific theory, please refer to this article: Image sharpening

1 Read the gray value of a row in the graph

This part is very simple, and the principle will not be repeated.

def read(img,h):
    """
           读某一行的灰度值
           :param img: 输入灰度值图像
           :param h  : 读第h行的灰度值
           :return gray:第h行的灰度值
    """
    gray = img[h,:]
    return gray

The renderings are as follows:
insert image description here

2 How to realize the differential operation

The first step required by the differential method is to require the gradient, and we usually think that the direction of the gradient is the direction of the maximum rate of change in the image, and the magnitude of the gradient is proportional to the gray level difference of adjacent pixels.

Write a function below to realize the gradient function of the array. The method of finding the first-order gradient is as follows:
insert image description here
and the method of finding the second-order gradient is as follows:
insert image description here
In fact, it is to find the first-order gradient for the first-order gradient.

def my_diff(arr,n=1):
    """
    计算梯度
    :param arr: 输入的数组
    :param n: 进行n阶微分,默认为1:return :
    """
    new_arr = np.empty_like(arr)
    for i in range(n) :
        new_arr[:-1] =arr[1:] - arr[:-1]
        arr = new_arr[:-1]
        #print("arr:",arr)
    return arr

insert image description here
The effect diagram of the first order differential is shown in the figure.
However, there will be problems when calling the above function for secondary differentiation. The specific reasons need to be analyzed: As
mentioned earlier: the gradient is the difference between adjacent elements
insert image description here
, so it is no problem to think about the first differentiation, but you must be careful about the second differentiation Yes, because the first differentiation will make the elements of the array less than the original array by 1, and
new_arr = np.empty_like(arr) in my code
does not decrease by 1 as the loop progresses, it is the arr passed in all the time size. So the code is modified as follows:

def my_diff(arr,n=1):
    """
    计算梯度
    :param arr: 输入的数组
    :param n: 进行n阶微分,默认为1:return :
    """
    #print("arr:", arr.shape)
    for i in range(n) :
        new_arr = np.empty_like(arr)#放进循环就好啦
        new_arr[:-1] =arr[1:] - arr[:-1]
        arr = new_arr[:-1]
        #print("arr:",arr.shape)
    return arr

insert image description here

Summarize

This article is a code implementation of the theory used by the gradient, basically no third-party library functions are used.

Guess you like

Origin blog.csdn.net/CSDN_Yangk/article/details/129798918