Opencv Basics

Keywords: Python OpenCV

This article is a systematic arrangement of the opencv knowledge learned some time ago, mainly the arrangement and preservation of the code, which is convenient for future reference.
This study mainly refers to the following articles:
Python+OpenCV image processing (a full article).

1, Opencv Basics

Image reading and display

#Read picture
img = cv2.imread("01.jpg") #When reading, the path uses two slashes. The first slash represents the escape character
#Display image
cv2.imshow("image", img) #Window name

Image splitting, display and saving

# Split channel
b, g, r = cv2.split(img)
# Display original image
cv2.imshow("B", b)
cv2.imshow("G", g)
cv2.imshow("R", r)

#Wait for the display. If there is no such sentence, the picture will be displayed and disappear immediately
cv2.waitKey(0) #0 means unlimited waiting (press any key to exit), greater than 0 (e.g. 5) means waiting for 5 seconds to automatically close the window, and less than zero means knocking the keyboard will close the window 
cv2.destroyAllWindows() #Clear window from memory
#Save image
cv2.imwrite("D:\\lalala.jpg", img) #File address, file name

At this time, you need to add a waiting display:

# Method 1: press any key to directly destroy all open high GUI windows
cv2.waitKey(0)  # Wait for user input and press any key

Image addition and type conversion

Method 1: Numpy addition operation, target image = image 1 + image 2, and modular operation is carried out for the operation result. There are two situations:
#(1) When the pixel value < = 255, the result is "image 1 + image 2", for example: 120 + 48 = 168
#(2) When the pixel value > 255, the result is the result of modulo of 255, for example: (255 + 64)% 255 = 64

result1 = img + test

Method 2: OpenCV addition operation, target image = cv2.add (image 1, image 2). At this time, the result is saturation operation. There are two cases:
#(1) When the pixel value < = 255, the result is "image 1 + image 2", for example: 120 + 48 = 168
#(2) When the pixel value > 255, the result is 255, for example: (255 + 64) = 255

result2 = cv2.add(img, test)

Image fusion:

result3 = cv2.addWeighted(result1, 1,result2, 1, 0) 

Image type conversion (grayscale image) cvtColor()
#1) cv2.COLOR_BGR2GRAY color to gray is similar to rgb2gray() in Matlab

result4 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Image type conversion (BRG channel to RGB channel)
result5 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

Image zoom (resize):

result6 = cv2.resize(img, (200,100))
print (result6.shape)
# Read picture data,# Image scaling dsize (column, row)
rows, cols, channel = img.shape
print(rows, cols)
result7 = cv2.resize(img, (int(cols * 0.6), int(rows * 1.2)))
# Image scaling (fx,fy) zooms in or out of the image.
result8 = cv2.resize(img, None, fx=0.3, fy=0.3)

Image rotation and flip

#Rotate around the center of the image, parameters: rotation center rotation degree scale

M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)
# Parameters: original image rotation parameter element image width and height
result9 = cv2.warpAffine(img, M, (cols, rows))

Image flip, flip() 0 flip with X axis as symmetry axis > 0 flip with Y axis as symmetry axis < 0x axis flip with Y axis

img1 = cv2.flip(img, 0)
img2 = cv2.flip(img, 1)
img3 = cv2.flip(img, -1)

Display graphics (note the usage of multiple images in one window)

img1 = cv2.flip(img, 0)
img2 = cv2.flip(img, 1)
img3 = cv2.flip(img, -1)
# Display graphics (note the usage of multiple images in one window)
titles = ['Source', 'Image1', 'Image2', 'Image3']
images = [img, img1, img2, img3]
for i in range(4):
    plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')
    plt.xticks([]), plt.yticks([])

Image translation

Image translation first defines the translation matrix M, and then calls the warpAffine() function to realize the translation
Image pan down, up, right, left

M1 = np.float32([[1, 0, 0], [0, 1, 100]])
img4 = cv2.warpAffine(img, M1, (img.shape[1], img.shape[0]))
M2 = np.float32([[1, 0, 0], [0, 1, -100]])
img5 = cv2.warpAffine(img, M1, (img.shape[1], img.shape[0]))
M3 = np.float32([[1, 0, 100], [0, 1, 0]])
img6 = cv2.warpAffine(img, M1, (img.shape[1], img.shape[0]))
M4 = np.float32([[1, 0, -100], [0, 1, 0]])
img7 = cv2.warpAffine(img, M1, (img.shape[1], img.shape[0]))
# display graphics
titles = ['img4', 'img5', 'img6', 'img7']

2, Image advanced knowledge

Threshold processing

ret,thresh1=cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY)     #Binary thresholding
ret,thresh2=cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY_INV) #Anti binary thresholding
ret,thresh3=cv2.threshold(GrayImage,127,255,cv2.THRESH_TRUNC)      #Truncation thresholding
ret,thresh4=cv2.threshold(GrayImage,127,255,cv2.THRESH_TOZERO)     #Thresholding to 0
ret,thresh5=cv2.threshold(GrayImage,127,255,cv2.THRESH_TOZERO_INV) #De thresholding to 0

Gaussian filtering

# Gaussian filtering
result = cv2.GaussianBlur(source, (3, 3), 0) #You can change the core size. The larger the core, the less clear it may be

Morphological 6 operations

#Corrosion cv2.erode(src, kernel, iterations) iterations indicates several times of corrosion
#The minimum value of the field value specified by the structure element is taken as the output gray value of the position. Taking the minimum value in each position field, the average value of the overall brightness of the output image after corrosion will be lower than the original image, the area of the brighter area in the image will become smaller or even disappear, and the size of the darker object will expand.

fushi = cv2.erode(src, kernel) 

#Expansion cv2.dilate(src, kernel, iterations)
#Taking the maximum value in each position field, the average value of the overall brightness of the expanded output image will increase compared with the original image, the area of the brighter area in the image will become larger, and the size of the darker object will decrease or even disappear.

pengzhang = cv2.dilate(src, kernel)

#The opening operation cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel) corrodes first and then expands, smoothes the contour of the object, breaks the narrow neck and eliminates thin protrusions.
#The closed operation cv2.morphologyEx(src, cv2.MORPH_CLOSE, kernel) expands first and then corrodes, smoothing part of the contour. Bridge narrow discontinuities and slender gullies, eliminate small holes and fill the fractures in the contour line.

kai = cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel)
bi = cv2.morphologyEx(src, cv2.MORPH_CLOSE, kernel)

Top hat operation original subtraction open operation
Black hat operation closed operation minus original

tophat = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)
blackhat = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, kernel)

Masking and region of interest

Generation of mask, extraction of region of interest, shielding, structural feature extraction, production of special shape image.

mask = np.zeros(src.shape[:2],np.uint8)
mask[200:400, 200:400]=255

Gray histogram

#Gray histogram dst=cv2.equalizeHist(src): the image with known gray probability density distribution is transformed into a new image with uniform gray probability density distribution.
#The normalized histogram and cumulative histogram are calculated from the statistical histogram of the original image, and then the cumulative histogram is interval converted and the gray level is converted to 256. So far, the conversion is completed,
#The finished images are similar, but the converted images are distributed in a larger range, indicating that they are more balanced

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('01.jpg', cv2.IMREAD_GRAYSCALE) # cv2.IMREAD_GRAYSCALE is read as a grayscale image, and its channel return value is a single
img1 = cv2.imread('01.jpg')
b,g,r = cv2.split(img1) #Channel segmentation
img_RGB=cv2.merge([r,g,b])#Channel combination

#img_RGB = cv2.imread('01.jpg',cv2.COLOR_BGR2RGB )      # ????????    Read image in rgb format -- none

equ = cv2.equalizeHist(img)

#cv2.imshow("src", img)
#cv2.imshow("result", equ)

plt.hist(img.ravel(), 256)
plt.hist(equ.ravel(), 256)
plt.subplot(221),plt.imshow(img_RGB, 'gray'),plt.title('img_rgb'), plt.xticks([]),plt.yticks([])
plt.subplot(222),plt.imshow(equ, 'gray'),plt.title('equ'), plt.xticks([]),plt.yticks([])

spectrum shaping

#Time domain to frequency domain
Fourier transform obtains low-frequency and high-frequency information, which can achieve different purposes for low-frequency and high-frequency
#(2) The Fourier process is reversible. The image can be restored to the original image after Fourier transform and inverse Fourier transform
#(3) The image is processed in the frequency domain, and the processing in the frequency domain will be reflected on the inverse transform image
#High pass filtering: Fourier transform to complex array – move the low-frequency component to the center – obtain the center point – set the low-frequency area and filter – convert the low-frequency to the upper left corner – inverse Fourier = = convert the inverse Fourier transform result to gray value

import cv2 
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('01.jpg', 0)

f = np.fft.fft2(img)   #Implement Fourier transform and return a complex array 
fshift = np.fft.fftshift(f)   #Move the low-frequency component to the center of the spectrum (the low-frequency after Fourier transform is in the upper left corner of the spectrum. For viewing convenience, move to the center)
result = 20*np.log(np.abs(fshift))#Convert complex number to grayscale value [0-255] range

rows, cols = img.shape[0:2] #Get image center point
rows,cols = int(rows/2), int(cols/2)  #Get image center point
fshift[rows-30:rows+30, cols-30:cols+30] = 0  #Set the 30 area around the center point to 0, that is, remove the low-frequency information to realize high pass filtering
ishift = np.fft.ifftshift(fshift)   #Restore the low frequency shift to the upper left corner
iimg = np.fft.ifft2(ishift)   #Inverse Fourier transform
iimg = np.abs(iimg)    #The result of inverse Fourier transform is converted into gray value and displayed as an image

plt.subplot(221),plt.imshow(img, cmap = 'gray'),plt.title('original'),plt.axis('off')
plt.subplot(222),plt.imshow(result, cmap = 'gray'),plt.title('result'),plt.axis('off')
plt.subplot(223), plt.imshow(iimg, cmap='gray'),plt.title('iimg'),plt.axis('off')


Posted by awpti on Sat, 20 Nov 2021 15:54:47 -0800