版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yql_617540298/article/details/84817597
一、图像变形
假设原图像为f(u,v),扭曲的目标图像是g(x,y)
1. forward warping
在已知图像坐标转换关系x(u,v)和y(u,v),直接把原图坐标映射到转换后图像相对应的位置上,近似取整得到结果。
带来的问题:新图上有很多点并不规整。
2. inverse warping
对于每个获得新坐标(x,y),用逆向映射函数u(x,y),v(x,y)找到它在原图上对应的位置(u,v),然后g(x,y)=f(u,v)。若点算出来不在格子上,用插值方法获得像素值。
二、python图像变形
import cv2
import math
import numpy as np
#加载一个灰度图像
image = cv2.imread('F:/a.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.imwrite("F:/b.jpg",output)
#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)
cv2.imwrite("F:/c.jpg",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)
cv2.imwrite("F:/d.jpg",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.imwrite("F:/e.jpg",output)
cv2.waitKey()
原图:
(1)垂直方向变形
(2)水平方向变形
(3)垂直+水平方向变形
(4)凹形
三、附:计算二维曲线长度
import numpy as np
from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt
## 二维空间曲线,采用参数形式
def curve_param_2d(dt=0.0001,plot=True):
dt = dt # 变化率
t = np.arange(0,2*np.pi, dt)
x = t*np.cos(t)
y = t*np.sin(t)
# print(len(t))
area_list = [] # 存储每一微小步长的曲线长度
# 下面的方式是循环实现
# for i in range(1,len(t)):
# # 计算每一微小步长的曲线长度,dx = x_{i}-x{i-1},索引从1开始
# dl_i = np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 )
# # 将计算结果存储起来
# area_list.append(dl_i)
# 更加pythonic的写法
area_list = [np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 ) for i in range(1,len(t))]
area = sum(area_list)# 求和计算曲线在t:[0,2*pi]的长度
print("二维参数曲线长度:{:.4f}".format(area))
if plot:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.title("2-D Parameter Curve")
plt.show()
## 二维空间曲线
def curve_2d(dt=0.0001,plot=True):
dt = dt # 变化率
t = np.arange(-6,10, dt)
x = t
y = x**3/8 - 4*x + np.sin(3*x)
# print(len(t))
area_list = [] # 存储每一微小步长的曲线长度
# for i in range(1,len(t)):
# # 计算每一微小步长的曲线长度,dx = x_{i}-x{i-1},索引从1开始
# dl_i = np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 )
# # 将计算结果存储起来
# area_list.append(dl_i)
area_list = [np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 ) for i in range(1,len(t))]
area = sum(area_list)# 求和计算曲线在t:[0,2*pi]的长度
print("二维曲线长度:{:.4f}".format(area))
if plot:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.title("2-D Curve")
plt.show()
if __name__ == '__main__':
curve_param_2d(plot=True)
curve_2d(plot=True)