代码和笔记
import cv2
import numpy as np"""
图像的基本变换
""" cat = cv2. imread( './img/cat.jpeg' ) """
缩放
"""
new_cat = cv2. resize( cat, ( 499 , 360 ) , interpolation= cv2. INTER_NEAREST)
new_cat1 = cv2. resize( cat, dsize= None , fx= 1.5 , fy= 0.5 , interpolation= cv2. INTER_LINEAR) cv2. imshow( 'new_cat' , new_cat)
cv2. imshow( 'new_cat1' , new_cat1) """
翻转
"""
new_cat2 = cv2. flip( cat, 0 ) cv2. imshow( 'new_cat2' , new_cat2) """
旋转
"""
new_cat3 = cv2. rotate( cat, rotateCode= cv2. ROTATE_90_COUNTERCLOCKWISE)
cv2. imshow( 'new_cat3' , new_cat3) """
仿射变换(原图和一个矩阵进行运算)
"""
M = np. float32( [ [ 1 , 0 , 200 ] , [ 0 , 1 , 100 ] ] )
new_cat4 = cv2. warpAffine( cat, M, ( 499 , 360 ) )
cv2. imshow( 'new_cat4' , new_cat4)
M1 = cv2. getRotationMatrix2D( ( 100 , 100 ) , 15 , 1 )
src = np. float32( [ [ 200 , 100 ] , [ 300 , 100 ] , [ 200 , 300 ] ] )
dst = np. float32( [ [ 100 , 150 ] , [ 360 , 200 ] , [ 280 , 120 ] ] )
M2 = cv2. getAffineTransform( src, dst)
src1 = np. float32( [ [ 200 , 100 ] , [ 300 , 100 ] , [ 200 , 300 ] , [ 150 , 240 ] ] )
dst1 = np. float32( [ [ 0 , 0 ] , [ 360 , 0 ] , [ 0 , 120 ] , [ 360 , 120 ] ] )
M3 = cv2. getPerspectiveTransform( src1, dst1)
new_cat5 = cv2. warpPerspective( cat, M3, ( 360 , 120 ) ) cv2. waitKey( 0 )
cv2. destroyAllWindows( )