OpenCV-Python系列·第七集:图像变形

版权声明:本文为博主原创文章,未经博主允许不得转载。若有任何问题,请联系QQ:575925154(加好友时,请备注:CSDN) https://blog.csdn.net/Miracle0_0/article/details/82026484
# -*- 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》

猜你喜欢

转载自blog.csdn.net/Miracle0_0/article/details/82026484
今日推荐