# -*- coding: utf-8 -*-
"""
Created on Fri Aug 24 22:15:14 2018
@author: Miracle
"""
import cv2
import math
import numpy as np
#加載一個灰度圖像
image = cv2.imread('../data/lena.jpg',cv2.IMREAD_GRAYSCALE)
#獲取高、寬
rows,cols = image.shape
'''
rows = height (y軸)
cols = width (X軸)
'''
#創建一個空圖像
output = np.zeros(image.shape,dtype = image.dtype)
#垂直方向變形
for i in range(rows):
for j in range(cols):
offset_x = int(50.0*math.cos(2*math.pi*i/180.0))
offset_y = 0
if j+offset_x < cols:#X軸方向
output[i,j] = image[i,(j+offset_x)%cols]
else:
output[i,j] = 255
cv2.imshow('Original Image',image)
cv2.imshow('Vertical wave',output)
#水平方向變形
for i in range(rows):
for j in range(cols):
offset_x = 0
offset_y = int(50.0*math.sin(2*math.pi*j/180.0))
if i+offset_y < rows:#Y軸方向
output[i,j] = image[(i+offset_y)%rows,j]
else:
output[i,j] = 255
cv2.imshow('Horizontal wave',output)
#垂直+水平方向變形
for i in range(rows):
for j in range(cols):
offset_x = int(50.0*math.cos(2*math.pi*i/180))
offset_y = int(50.0*math.sin(2*math.pi*j/180))
if j+offset_x < cols and i+offset_y < rows:
output[i,j] = image[(i+offset_y)%rows,(j+offset_x)%cols]
else:
output[i,j] = 255
cv2.imshow('Vertical & Horizontal wave',output)
#凹形
for i in range(rows):
for j in range(cols):
offset_x = int(128.0*math.sin(2*math.pi*i/(2*cols)))
offset_y = 0
if j+offset_x < cols:
output[i,j] = image[i,(j+offset_x)%cols]
else:
output[i,j] = 255
cv2.imshow('Concave wave',output)
cv2.waitKey()
參考文獻《OpenCV with Python By Example》