1. Introduction of OpenCV

OpenCV is a cross-platform computer vision and machine learning software library distributed under the BSD license that runs on Linux, Windows, Android, and Mac OS (and is expected to run on Harmony OS in the future). It is lightweight and efficient — it consists of a series of C functions and a small number of C++ classes. It also provides interfaces to Python, Ruby, MATLAB and other languages and implements many common algorithms in image processing and computer vision.

2. Opencv module

The module function
Core The core module contains the most basic operations
Imgproc Image processing module
Objdectect Target detection module
Feature2D 2D feature detection module
Video Video processing module
HighGUI High-level graphical user interface
Calib3d 3D reconstruction module
ML Machine learning module
FLANN Nearest neighbor search module
Stitching Image Mosaic module
Photo Computational graphics
Superres Super-resolution module
GPU GPU parallel acceleration module

3. OpenCV overview

Each module in the OpenCV framework contains a large number of computer vision methods, each module can be on its own, powerful.

This article introduces the most important module in the OpenCV library: Imgproc (image processing module).

Image processing module includes: image reading, display, save; Geometric operation; Gray scale transformation; Geometric transformation; Smooth and sharpen; Mathematical morphology; Threshold segmentation; Edge detection; Color space; Shape drawing, etc.


  • Image reading, display, save

function function
cv2.imread( ) Image is read
cv2.imshow( ) Image display
cv2.imwrite( ) Image save
Image reading, display and saving

img = cv2.imread('shiyuan.png')
cv2.imwrite('shi.png',img)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code


  • Geometric operation

function function
img1+img2 Image addition
cv2.addWeight( ) Image fusion
""" Geometric operation """

img1 = cv2.imread('shiyuan.png')
img2 = cv2.imread('lizi.png')

img3 = cv2.resize(img1,(300.300))+cv2.resize(img2,(300.300))
img4 = cv2.addWeighted(cv2.resize(img1,(300.300)),0.3,cv2.resize(img2,(300.300)),0.7.20)

cv2.imshow('img3',img3)
cv2.imshow('img4',img4)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

  • Gray level transformation

function function
Logarithmic transformation Transform image gray scale
Gamma transform Transform image gray scale
Histogram equalization Transform image gray scale
Histogram specification Transform image gray scale
"" gray scale transformation ""

import cv2
import copy

img = cv2.imread('bai.png'.1)
img1 = cv2.imread('bai.png'.0)
img = cv2.resize(img,(400.300))
img1 = cv2.resize(img,(400.300))
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

Gamma transform
gamma = copy.deepcopy(gray)
rows = img.shape[0]
cols = img.shape[1]
for i in range(rows):
    for j in range(cols):
        gamma[i][j]=3*pow(gamma[i][j],0.8)

cv2.imshow('img',img)
cv2.imshow('gray',img1)
cv2.imshow('gamma',gamma)

cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code
"" gray scale transformation ""

import cv2
import copy
import math

img = cv2.imread('bai.png'.1)
img1 = cv2.imread('bai.png'.0)
img = cv2.resize(img,(400.300))
img1 = cv2.resize(img,(400.300))
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Logarithmic transformation
logc = copy.deepcopy(gray)
rows=img.shape[0]
cols=img.shape[1]
for i in range(rows):
    for j in range(cols):
        logc[i][j] = 3 * math.log(1 + logc[i][j])
        
cv2.imshow('img',img)
cv2.imshow('gray',img1)
cv2.imshow('logc',logc)

cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code
"" gray scale transformation ""

import cv2
import copy
import math

img = cv2.imread('bai.png'.1)
img1 = cv2.imread('bai.png'.0)
img = cv2.resize(img,(400.300))
img1 = cv2.resize(img,(400.300))
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Invert color transform
cover=copy.deepcopy(gray)
rows=img.shape[0]
cols=img.shape[1]
for i in range(rows):
    for j in range(cols):
        cover[i][j]=255-cover[i][j]

The first parameter is the window name and the second parameter is the image variable to read
cv2.imshow('img',img)
cv2.imshow('gray',img1)
cv2.imshow('cover',cover)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code
# histogram specification

import cv2

import numpy as np

import matplotlib.pyplot as plt

img0=cv2.imread('hua.png')# Read the original image

scr=cv2.imread('tu.png')# Read the target image

# Transform the two images into real grayscale images, because I can only do grayscale image specification

img0=cv2.cvtColor(img0,cv2.COLOR_BGR2GRAY)

img=img0.copy()# for comparison later

scr=cv2.cvtColor(scr,cv2.COLOR_BGR2GRAY)

mHist1=[]

mNum1=[]

inhist1=[]

mHist2=[]

mNum2=[]

inhist2=[]

# Equalize the original image
for i in range(256):

    mHist1.append(0)

row,col=img.shapeGet the width and height of pixels in the original image



for i in range(row):

    for j in range(col):

        mHist1[img[i,j]]= mHist1[img[i,j]]+1# Count the number of gray values

mNum1.append(mHist1[0]/img.size)

for i in range(0.255):

    mNum1.append(mNum1[i]+mHist1[i+1]/img.size)

for i in range(256):

    inhist1.append(round(255*mNum1[i]))
    
 # Equalize the target image

for i in range(256):

    mHist2.append(0)

rows,cols=scr.shapeGet the width and height of the target image pixels

for i in range(rows):

    for j in range(cols):

        mHist2[scr[i,j]]= mHist2[scr[i,j]]+1# Count the number of gray values

mNum2.append(mHist2[0]/scr.size)

for i in range(0.255):

    mNum2.append(mNum2[i]+mHist2[i+1]/scr.size)

for i in range(256):

    inhist2.append(round(255*mNum2[i]))
Copy the code

  • Geometric transformation

function function
cv2.resize( ) Image zooming
cv2.warpAffine( ) Image translation
cv2.getRotationMatrix2D( ) cv2.warpAffine( ) The image rotation
cv2.getAffineTransform( ) cv2.warpAffine( ) Affine transformation
cv2.getPerspectiveTransform( ) cv2.warpPerspective( ) Transmission transformation
cv2.pyrUp( ) Upsampling of the Gauss pyramid
cv2.pyrDown( ) Gaussian pyramid downsampling
img-cv2.pyrUp(cv2.pyrDown(img)) Laplace’s pyramid
""" Geometric Transformation """

img = cv2.imread('shiyuan.png')

img1 = cv2.resize(img,(300.300))

M = np.float32([[1.0.30], [0.1.60]])
img2 = cv2.warpAffine(img1,M,(300.300))
img2 = cv2.putText(img2,'panning', (20.30),cv2.FONT_HERSHEY_SIMPLEX,1, (0.255.0),2)

M = cv2.getRotationMatrix2D(((300-1) /2.0, (300-1) /2.0),45.1)
img3 = cv2.warpAffine(img1,M,(300.300))
img3 = cv2.putText(img3,'rotation', (20.30),cv2.FONT_HERSHEY_SIMPLEX,1, (0.255.0),2)

matr1 = np.float32([[50.50], [200.50], [50.200]])
matr2 = np.float32([[10.100], [200.50], [100.250]])
M = cv2.getAffineTransform(matr1,matr2)
img4 = cv2.warpAffine(img1,M,(300.300))
img4 = cv2.putText(img4,'affine', (20.30),cv2.FONT_HERSHEY_SIMPLEX,1, (0.255.0),2)

matr1 = np.float32([[56.65], [368.52], [28.387], [389.390]])
matr2 = np.float32([[0.0], [300.0], [0.300], [300.300]])
M = cv2.getPerspectiveTransform(matr1,matr2)
img5 = cv2.warpPerspective(img1,M,(300.300))
img5 = cv2.putText(img5,'perspective', (20.30),cv2.FONT_HERSHEY_SIMPLEX,1, (0.255.0),2)

cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('img3',img3)
cv2.imshow('img4',img4)
cv2.imshow('img5',img5)
cv2.waitKey(0)
cv2.destroyAllWindows()

Copy the code
""" Image Pyramid """

import cv2 

# Gauss Pyramid
def pyramid_demo(image) :
    level = 2     
    temp = image.copy()  
    pyramid_images = []  
    for i in range(level):
        dst = cv2.pyrDown(temp)   
        pyramid_images.append(dst)  
        cv2.imshow("pyramid"+str(i+1), dst)
        temp = dst.copy()
    return pyramid_images

# Laplacian pyramid
def lapalian_demo(image) :
    pyramid_images = pyramid_demo(image)    
    level = len(pyramid_images)
    for i in range(level-1, -1, -1) :if (i-1) < 0:
            expand = cv2.pyrUp(pyramid_images[i], dstsize = image.shape[:2])
            lpls = cv2.subtract(image, expand)
            cv2.imshow("lapalian_down_"+str(i+1), lpls)
        else:
            expand = cv2.pyrUp(pyramid_images[i], dstsize = pyramid_images[i-1].shape[:2])
            lpls = cv2.subtract(pyramid_images[i-1], expand)
            cv2.imshow("lapalian_down_"+str(i+1), lpls)
            
src = cv2.resize(cv2.imread('shiyuan.png'), (256.256))
cv2.namedWindow('input_image') 
cv2.imshow('input_image', src)
lapalian_demo(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code
"" histogram equalization ""

import cv2
import numpy as np
img = cv2.imread('bai.png'.0)
img = cv2.resize(img,(400.300))
equ = cv2.equalizeHist(img)
cv2.imshow('img',equ)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

  • Smooth and sharpen

function function
cv2.blur( ) Average filtering
cv2.GaussianBlur( ) Gaussian filter
cv2.medianBlur( ) Median filtering
cv2.bilateralFilter( ) Bilateral filtering
""" smooth and sharpen """
import cv2

img = cv2.imread('shiyuan.png')
img = cv2.resize(img,(300.300))
img1 = cv2.blur(img,(11.11))
img2 = cv2.GaussianBlur(img,(11.11),0)
img3 = cv2.medianBlur(img,11)
img4 = cv2.bilateralFilter(img,9.75.75)

M = np.ones((5.5), np.float32) / 25
img5 = cv.filter2D(img, -1, M)

cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('img3',img3)
cv2.imshow('img4',img4)
cv2.imshow('img5',img5)

cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

  • Mathematical morphology

function function
cv2.erode( ) corrosion
cv2.dilate( ) inflation
cv2.morphologyEx(,cv2.MORPH_OPEN) Open operation
cv2.morphologyEx(,cv2.MORPH_CLOSE) Close your operation
cv2.morphologyEx(,cv2.MORPH_TOPHAT) Hat operation
cv2.morphologyEx(,cv2.MORPH_BLACKHAT) The bottom cap operation
cv2.morphologyEx(,cv2.MORPH_GRADIENT) Morphological gradient
"Mathematical morphology"

import cv2

img = cv2.imread('shiyuan.png')
img = cv2.resize(img,(300.300))

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3.3))

img1 = cv2.dilate(img, kernel)
img2 = cv2.erode(img,kernel)

Set the structure element
kernel_rect=cv2.getStructuringElement(cv2.MORPH_RECT,(3.3))
kernel_cross=cv2.getStructuringElement(cv2.MORPH_CROSS,(3.3))
kernel_ellipse=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3.3))

# Image open operation processing
open_rect=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel_rect)
open_cross=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel_cross)
open_ellipse=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel_ellipse)

# Image closed operation processing
close_rect=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel_rect)
close_cross=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel_cross)
close_ellipse=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel_ellipse)

gradient_rect = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel_rect)
gradient_cross = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel_cross)
gradient_ellipse = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel_ellipse)

# Top hat transform
tophat_rect=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel_rect)
tophat_cross=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel_cross)
tophat_ellipse=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel_ellipse)

# Top hat transform
blackhat_rect=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel_rect)
blackhat_cross=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel_cross)
blackhat_ellipse=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel_ellipse)

cv2.imshow('blackhat_rect',blackhat_rect)
cv2.imshow('blackhat_cross',blackhat_cross)
cv2.imshow('blackhat_ellipse',blackhat_ellipse)
cv2.imshow('tophat_rect',tophat_rect)
cv2.imshow('tophat_cross',tophat_cross)
cv2.imshow('tophat_ellipse',tophat_ellipse)
cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('open_rect',open_rect)
cv2.imshow('open_cross',open_cross)
cv2.imshow('open_ellipse',open_ellipse)
cv2.imshow('close_rect',close_rect)
cv2.imshow('close_cross',close_cross)
cv2.imshow('close_ellipse',close_ellipse)
cv2.imshow('gradient_rect',gradient_rect)
cv2.imshow('gradient_cross',gradient_cross)
cv2.imshow('gradient_ellipse',gradient_ellipse)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

  • Threshold segmentation

function function
cv2.threshold(,cv2.THRESH_BINARY) Binarization threshold
cv2.threshold(,cv2.THRESH_BINARY_INV) Inverse binarization threshold
cv2.threshold(,cv2.THRESH_TOZERO) Low threshold zero processing
cv2.threshold(,cv2.THRESH_TOZERO_INV) The threshold is exceeded
cv2.threshold(,cv2.THRESH_OSTU) Dajin algorithm
cv2.threshold(,cv2.THRESH_TRIANGLE) Truncation thresholding processing
cv2.adaptiveThreshold(,,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,) Adaptive threshold processing
cv2.adaptiveThreshold(,,cv2.ADAPTIVE_THRESH_MEAN_C,) Adaptive threshold processing
"Threshold segmentation"

import cv2

img = cv2.imread('shiyuan.png')
img = cv2.resize(img,(400.300))
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,img1 = cv2.threshold(img,110.255,cv2.THRESH_BINARY)
ret,img2 = cv2.threshold(img,110.255,cv2.THRESH_BINARY_INV)
ret,img3 = cv2.threshold(img,110.255,cv2.THRESH_TOZERO)
ret,img4 = cv2.threshold(img,110.255,cv2.THRESH_TOZERO_INV)
ret,img5 = cv2.threshold(img,110.255,cv2.THRESH_TRUNC)
ret,img6 = cv2.threshold(img,110.255,cv2.THRESH_TRIANGLE)
ret,img7 = cv2.threshold(img,110.255,cv2.THRESH_OTSU)
ret,img8 = cv2.threshold(cv2.GaussianBlur(img,(7.7),0),110.255,cv2.THRESH_OTSU)
img9 = cv2.adaptiveThreshold(img,127, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 9.11)
img10 = cv2.adaptiveThreshold(img,127,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,9.11)

cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('img3',img3)
cv2.imshow('img4',img4)
cv2.imshow('img5',img5)
cv2.imshow('img6',img6)
cv2.imshow('img7',img7)
cv2.imshow('img8',img8)
cv2.imshow('img9',img9)
cv2.imshow('img10',img10)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

  • Edge detection

function function
cv2.Canny( ) Canny operator
cv2.findContours( ) Contour detection
cv2.filter2D( ) Edge detection
"Edge detection"

import cv2

img = cv2.imread('bai.png')
img = cv2.resize(img,(400.300))
img1 = cv2.Canny(img,123.5)
 
cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code
""" Edge detection ""

import cv2  
  
img = cv2.imread('bai.png')  
img = cv2.resize(img,(400.300))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
ret, binary = cv2.threshold(gray,127.255,cv2.THRESH_BINARY)  
  
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)  
cv2.drawContours(img,contours,-1, (0.0.255),1)  
  
cv2.imshow("img", img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()
Copy the code
""" Edge detection ""

import cv2
import numpy as np
def find_contours(kernel) :
    img = cv2.imread('bai.png')
    img = cv2.resize(img,(400.300))
    img1 = cv2.filter2D(img,-1,kernel)
    cv2.imshow('img1',img1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

kernel1 = np.array((
        [0.0625.0.125.0.0625],
        [0.125.0.25.0.125],
        [0.0625.0.125.0.0625]), dtype="float32")

# Sobel operator
kernel2 = np.array(([-1, -2, -1],
                   [0.0.0],
                   [1.2.1]))

kernel3 = np.array(([-2, -1.0], [...1.1.1],
                   [0, -1, -2]))

kernel4 = np.array([[-1, -1, -1], [...1.8, -1], [...1, -1, -1]])

kernel5 = np.array([[0, -1.0], [...1.5, -1],
                   [0, -1.0]])

kernel6 = np.array([[0.1.0],
                   [1, -4.1],
                   [0.1.0]])

find_contours(kernel1)
find_contours(kernel2)
find_contours(kernel3)
find_contours(kernel4)
find_contours(kernel5)
find_contours(kernel6)
Copy the code

  • Color space

function function
cv2.cvtColor(,cv2.COLOR_BGR2GRAY) Image graying
cv2.cvtColor(,cv2.COLOR_BGR2HSV) RGB to HSV
""" Color Space """

import cv2

img = cv2.imread('bai.png')
img = cv2.resize(img,(400.300))
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

  • Shape to draw

function function
cv2.line( ) Draw a straight line
cv2.circle( ) Draw a circle
cv2.ellipse( ) Draw the ellipse
cv2.rectangle( ) Draw a rectangle
cv2.arrowedLine( ) Draw the arrow
cv2.putText( ) Draw text
""" Shape drawing """

import cv2

img = cv2.imread('bai.png')
img = cv2.resize(img,(400.300))
imgx = img.copy()
imgy = img.copy()
imgz = img.copy()
imgw = img.copy()

img = cv2.resize(img,(400.300))
img1 = cv2.line(img,(10.10), (200.300), (0.0.255),2)
img2 = cv2.circle(imgx,(60.60),30, (0.0.213), -1)
img3 = cv2.rectangle(imgy,(10.10), (100.80), (0.0.200),2)
img4 = cv2.ellipse(imgz,(256.256), (50.40),0.5.360, (20.213.79), -1)

font=cv2.FONT_HERSHEY_SIMPLEX
img5 = cv2.putText(imgw,'opencv', (80.90), font, 2, (255.255.255),3)

cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('img3',img3)
cv2.imshow('img4',img4)
cv2.imshow('img5',img5)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

Write in the last

Data package

Next issue will pull skLearn library, which is the only choice to do machine learning, welcome everyone to move a good small bench!