pycharm

Image recognition

The airspace transformation

= = = = = = =

  1. Airspace: refers to the plane where the image is located, that is, the space where the pixel position is located.

  2. Spatial transformation: the image matrix is operated to form another image according to the position and gray value of pixel points according to the purpose of image change.

  3. Spatial transformation classification: arithmetic logic transformation, geometric transformation, gray transformation, histogram transformation.

Add operation

The main application

  1. Remove additive noise

  2. Generate image overlay effect

import cv2 as cv img1 = cv.imread("1.jpg") img2 = cv.imread("2.jpg") print(img1.shape,img2.shape) Img1 = CV. Resize (img1, (img2. Shape [1], img2 shape [0])) image = CV. AddWeighted (img1, 0.6, img2, 0.4, 0.0,) CV. Imshow (' img1 'image) cv.waitKey(0) cv.destroyAllWindows()Copy the code

subtract

“Main Application”

  1. Display the difference between two images and detect the change between two images in the same scene, such as the detection of shot boundary in video

  2. Remove unwanted superposition patterns

  3. Image segmentation: such as segmentation of moving vehicles, subtraction to remove the static part, the rest is moving elements and noise

import cv2 as cv

img1=cv.imread('5.png')
img2=cv.imread('6.png')
#img1=cv.imread('LinuxLogo.jpg')
#img2=cv.imread('WindowsLogo.jpg')
dst=cv.add(img1,img2)
dst1=cv.subtract(img1,img2)


cv.imshow('dst',dst1)
cv.imshow('dst1',dst)

cv.waitKey(0)
cv.destroyAllWindows()


Copy the code

multiplication

The main application of image partial display, such as: with binary mask image and the original image to do multiplication

import cv2 as cv

img1=cv.imread('5.png')
img2=cv.imread('6.png')
dst=img1*img2

cv.imshow('181360152',dst)


cv.waitKey(0)
cv.destroyAllWindows()

Copy the code

Logical operations

  1. G (x,y) =255-f(x,y)

  2. And operation is mainly used for: two image intersecting subset, extract the sub-image of interest, g (x,y) =f (x,y) ^h (x,y)

import cv2 as cv

img1=cv.imread('LinuxLogo.jpg')
img2=cv.imread('WindowsLogo.jpg')
and_img=cv.bitwise_and(img1,img2)
or_img=cv.bitwise_or(img1,img2)
not_img=cv.bitwise_not(img1)
xor_img=cv.bitwise_xor(img1,img2)


cv.imshow('181360152',and_img)
cv.imshow('181360152zhang',or_img)
cv.imshow('181360152yang',not_img)
cv.imshow('181360152-',xor_img)

cv.waitKey(0)
cv.destroyAllWindows()

Copy the code

The zoom

The import cv2 import math import numpy as np class Img: def __init__ (self image, rows, cols, center = [0, 0]) : Self. SRC =image # self. Rows =rows # self. Cols =cols # Self. [0,0] def Move(self,delta_x,delta_y): # shift #delta_x>0 to the left, delta_x<0 to the right #delta_y>0 to the right, Self. transform=np.array([[1,0,delta_x],[0,1,delta_y],[0,0,1]]) def Zoom(self,factor): self.transform=np.array([[1,0, delta_y],[0,0,1]]) def Zoom(self,factor): self.transform=np.array([[1,0, delta_y],[0,0,1]]) def Zoom(self,factor): Self. Transform = Np.array ([[factor,0,0],[0,factor,0],[0,0,1]]) def Horizontal(self): # level mirror self. Transform = np. Array ([[0, 1], [0, 1, the self. The cols - 1], [0, 1]]) def Vertically (self) : Self. Transform =np.array([[-1,0,self.rows-1],[0,1,0],[0,0,1]]) def Rotate(self,beta): self. Transform =np.array([[math.cos(beta),-math.sin(beta),0], [0, 0, math.sin(beta), math.cos(beta),0], [0, 0, 1]]) def Process(self): self.dst=np.zeros((self.rows,self.cols),dtype=np.uint8) for i in range(self.rows): for j in range(self.cols): src_pos=np.array([i-self.center[0],j-self.center[1],1]) [x,y,z]=np.dot(self.transform,src_pos) x=int(x)+self.center[0] y=int(y)+self.center[1] if x>=self.rows or y>=self.cols or x<0 or y<0: self.dst[i][j]=255 else: self.dst[i][j]=self.src[x][y] if __name__=='__main__': src=cv2.imread('123.jpg',0) rows = src.shape[0] cols = src.shape[1] cv2.imshow('src', Img = img (SRC,rows,cols,[248,231]) img.zoom (0.5) # Zoom img.process () cv2.imshow(' DST ', img.dst) cv2.waitkey (0)Copy the code

translation

The import cv2 import math import numpy as np class Img: def __init__ (self image, rows, cols, center = [0, 0]) : Self. SRC =image # self. Rows =rows # self. Cols =cols # Self. [0,0] def Move(self,delta_x,delta_y): # shift #delta_x>0 to the left, delta_x<0 to the right #delta_y>0 to the right, Self. transform=np.array([[1,0,delta_x],[0,1,delta_y],[0,0,1]]) def Zoom(self,factor): self.transform=np.array([[1,0, delta_y],[0,0,1]]) def Zoom(self,factor): self.transform=np.array([[1,0, delta_y],[0,0,1]]) def Zoom(self,factor): Self. Transform = Np.array ([[factor,0,0],[0,factor,0],[0,0,1]]) def Horizontal(self): # level mirror self. Transform = np. Array ([[0, 1], [0, 1, the self. The cols - 1], [0, 1]]) def Vertically (self) : Self. Transform =np.array([[-1,0,self.rows-1],[0,1,0],[0,0,1]]) def Rotate(self,beta): self. Transform =np.array([[math.cos(beta),-math.sin(beta),0], [0, 0, math.sin(beta), math.cos(beta),0], [0, 0, 1]]) def Process(self): self.dst=np.zeros((self.rows,self.cols),dtype=np.uint8) for i in range(self.rows): for j in range(self.cols): src_pos=np.array([i-self.center[0],j-self.center[1],1]) [x,y,z]=np.dot(self.transform,src_pos) x=int(x)+self.center[0] y=int(y)+self.center[1] if x>=self.rows or y>=self.cols or x<0 or y<0: self.dst[i][j]=255 else: self.dst[i][j]=self.src[x][y] if __name__=='__main__': src=cv2.imread('123.jpg',0) rows = src.shape[0] cols = src.shape[1] cv2.imshow('src', Img = img (SRC,rows,cols,[248,231]) img.Move(-30, -50) # img.Process() cv2.imshow(' DST ', img.dst) cv2.waitkey (0)Copy the code

rotating

The import cv2 import math import numpy as np class Img: def __init__ (self image, rows, cols, center = [0, 0]) : Self. SRC =image # self. Rows =rows # self. Cols =cols # Self. [0,0] def Move(self,delta_x,delta_y): # shift #delta_x>0 to the left, delta_x<0 to the right #delta_y>0 to the right, Self. transform=np.array([[1,0,delta_x],[0,1,delta_y],[0,0,1]]) def Zoom(self,factor): self.transform=np.array([[1,0, delta_y],[0,0,1]]) def Zoom(self,factor): self.transform=np.array([[1,0, delta_y],[0,0,1]]) def Zoom(self,factor): Self. Transform = Np.array ([[factor,0,0],[0,factor,0],[0,0,1]]) def Horizontal(self): # level mirror self. Transform = np. Array ([[0, 1], [0, 1, the self. The cols - 1], [0, 1]]) def Vertically (self) : Self. Transform =np.array([[-1,0,self.rows-1],[0,1,0],[0,0,1]]) def Rotate(self,beta): self. Transform =np.array([[math.cos(beta),-math.sin(beta),0], [0, 0, math.sin(beta), math.cos(beta),0], [0, 0, 1]]) def Process(self): self.dst=np.zeros((self.rows,self.cols),dtype=np.uint8) for i in range(self.rows): for j in range(self.cols): src_pos=np.array([i-self.center[0],j-self.center[1],1]) [x,y,z]=np.dot(self.transform,src_pos) x=int(x)+self.center[0] y=int(y)+self.center[1] if x>=self.rows or y>=self.cols or x<0 or y<0: self.dst[i][j]=255 else: self.dst[i][j]=self.src[x][y] if __name__=='__main__': src=cv2.imread('123.jpg',0) rows = src.shape[0] cols = src.shape[1] cv2.imshow('src', Img = img (SRC,rows,cols,[248,231]) img.rotate (-math.radians(180)) # img.process () cv2.imshow(' DST ', img.dst) cv2.waitKey(0)Copy the code

subsequent

If you like, just click on ‘I’m watching’.

Writing is not easy, thank you for your support.

This article uses the article synchronization assistant to synchronize