Make writing a habit together! This is the 10th day of my participation in the “Gold Digging Day New Plan ยท April More text Challenge”. Click here for more details.

  • ๐ŸŽ‰ statement: as one of the bloggers in AI field, โค๏ธ lives up to your time and your โค๏ธ
    • ๐ŸŠ # Column: Image processing

    • ๐ŸŠ This blog post continues with the previous article # OpenCV histogram and Image Composition [9]

    • ๐ŸŠ ๐Ÿ‘‹ Follow me ๐Ÿ‘‹, let’s Get more interesting AI ๐Ÿš€ ๐Ÿ‘‹

    This post continues to test some of the OpencV Python sample code

    • Direct pull test of this post is opencv – master4.5.1, then in opencv/samples/python/tutorial_code/testing different module py file directory


    [linear addition of images]


    Run the code:

    cdopencv/samples/python/tutorial_code/core/AddingImages cp .. /.. /.. /.. /data/LinuxLogo.jpg . cp .. /.. /.. /.. /data/WindowsLogo.jpg . python adding_images.pyCopy the code

    The effect is as follows:


    The DFT of an image is taken and it’s power spectrum is displayed.ใ€ DFT ใ€‘


    Run code 1:

    cdopencv/samples/python/tutorial_code/core/discrete_fourier_transform cp .. /.. /.. /.. /data/lena.jpg . python discrete_fourier_transform.py lena.jpgCopy the code

    The running effect is as follows:


    FileStorage you can serialize objects in OpenCV


    Run code 1:

    cd opencv/samples/python/tutorial_code/core/file_input_output
    
    python3 file_input_output.py moli.json
    Copy the code

    The running effect is as follows:

    cat moli.json 
    The file contains the following contents, of course, if you want to understand, you need to look at the code:
    # It is important for me to know that there is such a file input that the generated code can be used for reference later
    
    {
        "iterationNr": 100,
        "strings": [
            "image1.jpg"."Awesomeness".".. /data/baboon.jpg"]."Mapping": {
            "One": 1,
            "Two": 2}."R_MAT": {
            "type_id": "opencv-matrix"."rows": 3."cols": 3."dt": "d"."data": [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]}."T_MAT": {
            "type_id": "opencv-matrix"."rows": 3."cols": 1,
            "dt": "d"."data": [0.0, 0.0, 0.0]},"MyData": { "A": 97, "X": 3.1415926535897931 e+00,"name": "mydata1234"}}Copy the code

    ใ€ Mat_mask_operations ใ€‘


    Run code 1:

    cd. /mat_mask_operations/ cp .. /.. /.. /.. /data/lena.jpg . python3 file_input_output.py moli.jsonCopy the code

    The running effect is as follows:


    Mat_operations.py [common OpenCV image manipulation function usage example]


    Run code 1:

    cd ../mat_operations/
    
    cat mat_operations.py 
    
    Copy the code

    Examples of common OpenCV image manipulation functions are provided in the mat_operations.py code

    from __future__ import division
    import cv2 as cv
    import numpy as np
    
    # Snippet code for Operations with images tutorial (not intended to be run)
    
    def load() :
        # Input/Output
        filename = 'img.jpg'
        ## [Load an image from a file]
        img = cv.imread(filename)
        ## [Load an image from a file]
    
        ## [Load an image from a file in grayscale]
        img = cv.imread(filename, cv.IMREAD_GRAYSCALE)
        ## [Load an image from a file in grayscale]
    
        ## [Save image]
        cv.imwrite(filename, img)
        ## [Save image]
    
    def access_pixel() :
        # Accessing pixel intensity values
        img = np.empty((4.4.3), np.uint8)
        y = 0
        x = 0
        ## [Pixel access 1]
        _intensity = img[y,x]
        ## [Pixel access 1]
    
        ## [Pixel access 3]
        _blue = img[y,x,0]
        _green = img[y,x,1]
        _red = img[y,x,2]
        ## [Pixel access 3]
    
        ## [Pixel access 5]
        img[y,x] = 128
        ## [Pixel access 5]
    
    def reference_counting() :
        # Memory management and reference counting
        ## [Reference counting 2]
        img = cv.imread('image.jpg')
        _img1 = np.copy(img)
        ## [Reference counting 2]
    
        ## [Reference counting 3]
        img = cv.imread('image.jpg')
        _sobelx = cv.Sobel(img, cv.CV_32F, 1.0)
        ## [Reference counting 3]
    
    def primitive_operations() :
        img = np.empty((4.4.3), np.uint8)
        ## [Set image to black]
        img[:] = 0
        ## [Set image to black]
    
        ## [Select ROI]
        _smallImg = img[10:110.10:110]
        ## [Select ROI]
    
        ## [BGR to Gray]
        img = cv.imread('image.jpg')
        _grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        ## [BGR to Gray]
    
        src = np.ones((4.4), np.uint8)
        ## [Convert to CV_32F]
        _dst = src.astype(np.float32)
        ## [Convert to CV_32F]
    
    def visualize_images() :
        ## [imshow 1]
        img = cv.imread('image.jpg')
        cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
        cv.imshow('image', img)
        cv.waitKey()
        ## [imshow 1]
    
        ## [imshow 2]
        img = cv.imread('image.jpg')
        grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        sobelx = cv.Sobel(grey, cv.CV_32F, 1.0)
        # find minimum and maximum intensities
        minVal = np.amin(sobelx)
        maxVal = np.amax(sobelx)
        draw = cv.convertScaleAbs(sobelx, alpha=255.0/(maxVal - minVal), beta=-minVal * 255.0/(maxVal - minVal))
        cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
        cv.imshow('image', draw)
        cv.waitKey()
        ## [imshow 2]
    
    Copy the code

    Profile of the blogger: Master of Software Engineering, graduated, soon 10W readers and fans

    • ๐ŸŠ Computer vision: some knowledge in superresolution reconstruction, image restoration, object detection, style transfer and other fields
    • ๐ŸŠ AI engineering: Ncnn, MNN, TensorRT are learning
    • ๐ŸŠ C++, Python, Java
    • ๐ŸŠ ๐Ÿ‘‹ Follow me ๐Ÿ‘‹, Get more interesting AI, combat blog tutorials, storm ๐Ÿš€ ๐Ÿ‘‹

    Please pay attention to moneo-ai and its namesake public number Moneo-AI

    On the way to learn lessons, let mohli AI accompany you to enjoy more interesting AI