OpenCV image pyramid operator

        This article is the 14th article on the road to getting started with OpenCV image vision. This article introduces in detail the various operations of the image pyramid operator, such as: Gaussian pyramid operator, Laplacian pyramid operator and other operations .

        The higher levels (lower resolutions) in the Gaussian pyramid are formed by first convolving the image with a Gaussian kernel and then removing the even rows and columns, and then each pixel of the higher level is formed by the contribution of 5 pixels from the base level with Gaussian weights , through such an operation, the M x N image becomes an M/2 x N/2 image, so the area is reduced to a quarter of the original. We call it Octave. When our pyramid is higher, this mode becomes more to continue.

    Downsampling method: 1. Gaussian kernel convolution is performed on the image; 2. All even rows and columns are removed. The lower level (high resolution) of the image is doubled in each dimension by the higher level (low resolution), and the newly added rows and columns (even rows and columns) are filled with 0, and then used The specified filter performs convolution to estimate an approximation of missing pixels.

    Upsampling method: 1. Expand the image to twice the original size in each dimension, and fill the new rows and columns with 0; 2. Use the same original kernel (x4) to convolve with the image after the method to obtain a new Approximate value in pixels. Some information is lost during the scaling process. If you want to reduce the loss of information during the scaling process, you need to use the Laplacian pyramid.

OpenCV image pyramid operator directory

1 Gaussian Pyramid

2 Pyramid of Laplace


1 Gaussian Pyramid

Gaussian Pyramid: Upsampling method (enlargement) cv.pyrUp(src) function: Only one parameter needs to be passed in, which represents the image resource and is used to upsample the image

Gaussian Pyramid: Downsampling method (reduction) cv.pyrDown() function: The parameter passing is consistent with cv.pyrUp(), which is used to downsample the image, and usually can also be used for image blurring

import cv2
import numpy as np
from numpy import unicode

if __name__ == '__main__':
    img = cv2.imread('D:/Jupyter_Notebooks/0.jpg') #, cv2.IMREAD_GRAYSCALE

    cv2.imshow("img", img)
    print(img.shape)

    up = cv2.pyrUp(img)
    up2 = cv2.pyrUp(up)


    cv2.namedWindow("up", 0)
    cv2.imshow("up", up)
    print(up.shape)

    cv2.namedWindow("up2", 0)
    cv2.imshow("up2", up2)
    print(up2.shape)


    down = cv2.pyrDown(img)
    up_down = cv2.pyrDown(up)

    cv2.namedWindow("down", 0)
    cv2.imshow("down", down)
    print(down.shape)

    cv2.namedWindow("up_down", 0)
    cv2.imshow("up_down", up_down)
    print(up_down.shape)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

import cv2
import numpy as np
from numpy import unicode

if __name__ == '__main__':
    img = cv2.imread('D:/Jupyter_Notebooks/0.jpg') #, cv2.IMREAD_GRAYSCALE

    up = cv2.pyrUp(img)
    up_down = cv2.pyrDown(up)
    cv2.imshow("up_down", up_down)

    cv2.imshow("up_downs", np.hstack((img, up_down)))

    up = cv2.pyrUp(img)
    up_down = cv2.pyrDown(up)
    cv2.imshow("img-up_down", img - up_down)


    cv2.waitKey(0)
    cv2.destroyAllWindows()

This code uses the image pyramid functions cv2.pyrUp()and cv2.pyrDown()and img - up_downdisplays .

Image pyramid is a common multi-resolution image processing technology, which can reduce or enlarge the image according to different scales to obtain a series of images. cv2.pyrUp()and are the functions of upsampling and downsampling cv2.pyrDown()respectively , which can be used to generate Gaussian pyramid and Laplacian pyramid.

In this code, cv2.pyrUp()the imgup-sampled using to get a higher resolution image up. Then use cv2.pyrDown()to updownsample the , resulting in an image with the same resolution as the original image but possibly with different content up_down.

Finally cv2.imshow(), img - up_downthe difference image of is displayed by the function, which is the difference between the original image and the up-downsampled image. Doing so can help us understand the effect of pyramid image processing, as well as visually analyze changes in image content.

2 Pyramid of Laplace

1. The picture in the upper left corner is the original picture

2. Gaussian smoothing of the original image

3. Perform a downsampling, the image becomes 1/4 of the original

4. Perform an upsampling, and the image becomes the size of the original image

5. Perform Gaussian Blur again

6. Subtract the Gaussian blurred image from the original image to get the Laplacian image

import cv2
import numpy as np
from numpy import unicode

if __name__ == '__main__':
    img = cv2.imread('D:/Jupyter_Notebooks/0.jpg') #, cv2.IMREAD_GRAYSCALE

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    down = cv2.pyrDown(img)
    dst = cv2.pyrUp(down)
    res = img - dst
    cv2.imshow("img", img)
    cv2.imshow("res", res)


    cv2.waitKey(0)
    cv2.destroyAllWindows()

 

Guess you like

Origin blog.csdn.net/qq_37529913/article/details/129053722