Opencv基础使用 Mat 操作【10】

972 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第10天,点击查看活动详情

  • 🎉 声明: 作为全网 AI 领域 干货最多的博主之一,❤️ 不负光阴不负卿 ❤️
  • 1-0

    本博文接上一篇博文,继续进行部分 opencv Python 示例代码运行 效果测试

    • 本博文的测试是直接拉取 opencv-master4.5.1,然后在opencv/samples/python/tutorial_code/ 目录下对不同模块 py 文件进行测试

    0-9


    AddingImages【图片线性相加】


    运行该代码:

    cd opencv/samples/python/tutorial_code/core/AddingImages
    
    cp ../../../../data/LinuxLogo.jpg .
    
    cp ../../../../data/WindowsLogo.jpg .
    
    python adding_images.py 
    
    

    效果如下:

    1-1


    The dft of an image is taken and it's power spectrum is displayed.【离散傅里叶变换(DFT)】


    运行代码一:

    cd opencv/samples/python/tutorial_code/core/discrete_fourier_transform
    
    cp ../../../../data/lena.jpg .
    
    python discrete_fourier_transform.py  lena.jpg 
    
    

    运行效果如下:

    2-1


    FileStorage you can serialize objects in OpenCV【序列化】


    运行代码一:

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

    运行效果如下:

    3-1

    cat moli.json 
    # 文件内容如下,当然如果要理解,需要结合代码来看:
    # 对我而言,重要的是我知道有这样一份 文件输入生成的代码以后可以用来借鉴
    
    {
        "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.1415926535897931e+00, "name": "mydata1234" }
    }
    
    

    mat_mask_operations【灰度图 sharpen 处理】


    运行代码一:

    cd ../mat_mask_operations/
    
    cp ../../../../data/lena.jpg .
    
    python3 file_input_output.py moli.json
    

    运行效果如下:

    4-1


    mat_operations.py【常见OpenCV 图像操作函数使用示例】


    运行代码一:

    cd ../mat_operations/
    
    cat mat_operations.py 
    
    

    mat_operations.py 代码中 提供了常见的 OpenCV 图像操作函数使用 示例

    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]
    
    

    900.png

    博主简介:软件工程硕士、已毕业、马上 10w 读者 粉丝

    • 🍊 计算机视觉:超分重建、图像修复、目标检测、风格迁移 等领域 稍有所学
    • 🍊 AI 工程化:Ncnn、MNN、TensorRT 正在 学习
    • 🍊 C++、Python、Java 略懂一二
    • 🍊 👋 Follow me 👋,一起 Get 更多有趣 AI、实战博文教程、冲冲冲 🚀 🚀

    喜欢请关注 墨理学AI 及其 同名 公众号 墨理学AI

    取经路上,让墨理学AI 陪你畅享更多有趣AI