Skip to content

Latest commit

 

History

History
341 lines (274 loc) · 8.72 KB

614-1189947-几何变换_翻转_缩放_旋转_透视变型.sy.md

File metadata and controls

341 lines (274 loc) · 8.72 KB
show version enable_checker
step
1.0
true

opencv

回忆

  • 上次研究的是HSV
  • HSV是一种色彩模式
简写 名称 含义
H Hue 色相
S Saturation 饱和度
V Value 亮度
  • 可以通过滑块对于HSV的图像进行控制
  • 还有什么可以玩的东西吗?

翻转图片

图片描述

import cv2
img = cv2.imread("/home/shiyanlou/kun1.png")
dst1 = cv2.flip(img,0)#flip-x
dst2 = cv2.flip(img,1)#flip-y
dst3 = cv2.flip(img,-1)#flip-xy
cv2.imshow("img",img) 
cv2.imshow("dst1",dst1)
cv2.imshow("dst2",dst2)
cv2.imshow("dst3",dst3)                                             
cv2.waitKey()         
cv2.destroyAllWindows()
  • 效果

图片描述

平移图片

import cv2
import numpy as np
img = cv2.imread("/home/shiyanlou/kun1.png")
rows = len(img)
cols = len(img[0])
M = np.float32([[1,0,50]    #50 to left
                ,[0,1,100]])#100 to bottom
dst1 = cv2.warpAffine(img,M,(cols,rows))#flip-x
cv2.imshow("img",img)
cv2.imshow("dst1",dst1)
cv2.waitKey()                                                       
cv2.destroyAllWindows()
  • 效果

图片描述

复制后翻转再粘贴图片效果

import cv2
img = cv2.imread("/home/shiyanlou/kun1.png")
ball = img[115:192,262:328]
ball = cv2.flip(ball,1)                                             
img[138:215,103:169] = ball[:,:]
cv2.imshow("img",img) 
cv2.waitKey()         
cv2.destroyAllWindows()
  • 效果

图片描述

旋转图片

import cv2   
import numpy as np
img = cv2.imread("/home/shiyanlou/kun1.png")
rows = len(img)
cols = len(img[0])
center = (rows/2,cols/2)
M = cv2.getRotationMatrix2D(center,30,0.8)
dst1 = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("img",img)
cv2.imshow("dst1",dst1)
cv2.waitKey()                                                       
cv2.destroyAllWindows()
  • 效果

图片描述

透视效果

import cv2       
import numpy as np
img = cv2.imread("/home/shiyanlou/kun1.png")
rows = len(img)  
cols = len(img[0])
p1 = np.zeros((4,2),np.float32)#original picture 3 points
p1[0] = [0, 0]#left top point of original pictrure
p1[1] = [cols -1 , 0]#right top point of original pictrure
p1[2] = [0, rows-1]# left bottom  point of original pictrure
p1[3] = [cols - 1, rows-1]# right bottom  point of original pictrure
                 
p2 = np.zeros((4,2),np.float32)
p2[0] = [90,0]   
p2[1] = [cols -90 ,0]                                                         
p2[2] = [0, rows - 1]
p2[3] = [cols - 1, rows-1]
                 
M = cv2.getPerspectiveTransform(p1, p2)
dst = cv2.warpPerspective(img, M, (cols, rows))
cv2.imshow("img",img)
cv2.imshow("dst",dst)
cv2.waitKey()    
cv2.destroyAllWindows()
  • 效果

图片描述

  • 可以把已经透视的图片
    • 变回 没有透视的 状态吗?

原始图片

图片描述

  • 标记点

图片描述

  • 具体怎么变形呢?

透视变形效果

import cv2
import numpy as np
img = cv2.imread("/home/shiyanlou/notice.png")
rows = len(img)
cols = len(img[0])
p1 = np.zeros((4,2),np.float32)#original picture 3 points
p1[0] = [0, 0]#left top point of original pictrure
p1[1] = [cols -1 , 0]#right top point of original pictrure
p1[2] = [0, rows-1]# left bottom  point of original pictrure
p1[3] = [cols - 1, rows-1]# right bottom  point of original pictrure

p2 = np.zeros((4,2),np.float32)
p2[0] = [150,20]
p2[1] = [350 ,40]
p2[2] = [162, 311]
p2[3] = [320, 270]

M = cv2.getPerspectiveTransform(p2, p1)
dst = cv2.warpPerspective(img, M, (cols, rows))
cv2.imshow("img",img)
cv2.imshow("dst",dst)
cv2.waitKey()
cv2.destroyAllWindows()
  • 效果

图片描述

  • 这种类型包括了近大远小的透视变形
    • getPerspectiveTransform

复制之后,先旋转,再粘贴

import cv2
img = cv2.imread("/home/shiyanlou/kun1.png")
ball = img[115:192,262:328]         
rows = len(ball)                 
cols = len(ball[0])              
center = (rows/2,cols/2)         
M = cv2.getRotationMatrix2D(center,60,0.7)
dst = cv2.warpAffine(ball,M,(cols,rows))
print(dst.shape)                 
for y in range(0,77):            
    for x in range(0,66):        
        print("y=",40+y,"x=",250+x,"img",img[40+y,250+x])
        print("y=",y,"x=",x,"dst",dst[y,x])
        if dst[y,x,0] == 0 and dst[y,x,1]==0 and dst[y,x,2]==0:
            continue             
        img[70+y,250+x] = dst[y,x]                                            
cv2.imshow("img",img)            
cv2.waitKey()                    
cv2.destroyAllWindows()
  • 效果

图片描述

使用透明通道和背景色

import numpy as np
import cv2
img = cv2.imread("/home/shiyanlou/kun1.png")
img = cv2.cv2.cvtColor(img,cv2.COLOR_BGR2BGRA)
y2 = 192 
y1 = 115 
x2 = 328 
x1 = 262 
ball = img[y1:y2,x1:x2]
print(ball)
rows = len(ball)
cols = len(ball[0])
center = (rows/2,cols/2)
M = cv2.getRotationMatrix2D(center,60,0.7)
dst = cv2.warpAffine(ball,M,(cols,rows),borderValue=[230,229,245,0])
cv2.imshow("dst",dst)
cv2.imwrite("d.png",dst)
print(dst.shape)
dy = y2 - y1                                                                   
dx = x2 - x1
for y in range(0,dy):
    for x in range(0,dx):
        #print("y=",40+y,"x=",250+x,"img",img[40+y,250+x])
        #print("y=",y,"x=",x,"dst",dst[y,x])
        if dst[y,x,3] == 0:
            print(dst)
            continue
        img[70+y,250+x] = dst[y,x]
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()
  • 效果

图片描述

按照原始大小进行图片旋转

import cv2                                                                     
from math import sin,cos
import numpy as np
      
def rotate_bound_white_bg(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)
      
    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    print(M)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])
      
    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))
      
    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
    print(M)
      
    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH),borderValue=(255,255,255))
    # return cv2.warpAffine(image, M, (nW, nH))

img = cv2.imread("/mnt/cgshare/kun1.png")
for i in range(0,8):
    imgRotation = rotate_bound_white_bg(img, 45*i-45)
    cv2.imshow("imgRotation",imgRotation)
    cv2.imwrite("img_" + str(i) + ".png",imgRotation)
cv2.waitKey(0)
  • 拼出九宫格环绕

图片描述

图片描述

斜切图片

import cv2
import numpy as np
img = cv2.imread("/home/shiyanlou/kun1.png")
rows = len(img)
cols = len(img[0])
p1 = np.zeros((3,2),np.float32)#original picture 3 points
p1[0] = [0, 0]#left top point of original pictrure
p1[1] = [cols -1 , 0]#right top point of original pictrure
p1[2] = [0, rows-1]# left bottom  point of original pictrure
        
p2 = np.zeros((3,2),np.float32)
p2[0] = [50,0]
p2[1] = [cols -1 ,0]
p2[2] = [0, rows - 1]
        
M = cv2.getAffineTransform(p1, p2)                                            
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img",img)
cv2.imshow("dst",dst)
cv2.waitKey()
cv2.destroyAllWindows()
  • 效果

图片描述

  • 这种方式被称为斜切
    • shear

总结 🤔

  • 这次研究了图像的各种类型的变型方法
    • 旋转
    • 翻转
    • 透视
    • 倾斜
    • 仿射变形

图片描述

  • 透视和仿射有什么区别呢??
  • 我们下次再说 👋