图像处理之几何变换

一、柱形畸变

import cv2
import numpy as np

def cylindrical_projection(image, f):
    h, w = image.shape[:2]
    map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
    
    x_c = w / 2
    y_c = h / 2
    
    theta = (map_x - x_c) / f
    h_c = (map_y - y_c) / np.sqrt((map_x - x_c)**2 + f**2)
    
    map_x_cyl = f * np.tan(theta) + x_c
    map_y_cyl = f * h_c + y_c
    
    return cv2.remap(image, map_x_cyl.astype(np.float32), map_y_cyl.astype(np.float32), cv2.INTER_LINEAR)


# 读取图像
image = cv2.imread(r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg")

# 设置焦距
focal_length = 120

# 进行柱面投影
result = cylindrical_projection(image, focal_length)

# 显示结果
cv2.imwrite("test.jpg",result)

在这里插入图片描述

在这里插入图片描述

def cylindrical_projection(image, f):
    h, w = image.shape[:2]
    map_x, map_y = np.meshgrid(np.arange(w), np.arange(h))
    
    x_c = w / 2
    y_c = h * 1.5
    
    theta = (map_x - x_c) / f
    h_c = (map_y - y_c) / np.sqrt((map_x - x_c)**2 + f**2)
    
    map_x_cyl = f * np.tan(theta) + x_c
    map_y_cyl = f * h_c + y_c
    
    return cv2.remap(image, map_x_cyl.astype(np.float32), map_y_cyl.astype(np.float32), cv2.INTER_LINEAR)

在这里插入图片描述

二、桶形畸变

import cv2
import numpy as np

def Barrel_projection(image, f):
    """  
    模拟桶形畸变效果  
    :param image: 输入图像  
    :param k_factor: 桶形畸变因子,值越大畸变越明显  
    :return: 模拟桶形畸变后的图像  
    """  
    h, w = image.shape[:2]  
    y_i, x_i = np.ogrid[:h, :w]  
    center_x, center_y = w // 2, h // 2  
    r = np.sqrt((x_i - center_x) ** 2 + (y_i - center_y) ** 2)  
    r_max = np.sqrt((w / 2) ** 2 + (h / 2) ** 2)  
      
    # 桶形变换公式,你可以根据需要调整这个公式  
    r_distorted = r * (1 + f * (r / r_max) ** 2)  
      
    # 计算变换后的坐标  
    theta = np.arctan2(y_i - center_y, x_i - center_x)  
    x_distorted = center_x + r_distorted * np.cos(theta)  
    y_distorted = center_y + r_distorted * np.sin(theta)  
      
    # 使用映射进行图像变换  
    distorted_image = cv2.remap(image, x_distorted.astype(np.float32), y_distorted.astype(np.float32),   
                                interpolation=cv2.INTER_LINEAR)  
    return distorted_image 

# 读取图像
image = cv2.imread(r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg")

# 设置焦距,负值为枕形
focal_length = 5

# 进行柱面投影
result = Barrel_projection(image, focal_length)

# 显示结果
cv2.imwrite("test.jpg",result)  

在这里插入图片描述

三、旋转变换(不丢失信息)

import cv2
import numpy as np

def rotate_projection(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)
    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

    # perform the actual rotation and return the image

    img_rotated = cv2.warpAffine(image, M, (nW, nH))

    return img_rotated

# 读取图像
image = cv2.imread(r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg")

# 设置角度,正值为逆时针
focal_length = 60

# 进行旋转
result = rotate_projection(image, focal_length)

# 显示结果
cv2.imwrite("test.jpg",result)

在这里插入图片描述

四、扇形畸变

import cv2
import numpy as np

def fan_distortion(img, center, radius, angle_range):
    height, width = img.shape[:2]
    map_x, map_y = np.meshgrid(np.arange(width), np.arange(height))
    map_x = map_x.astype(np.float32)
    map_y = map_y.astype(np.float32)

    # 计算每个像素点相对于圆心的距离和角度
    dist = np.sqrt((map_x - center[0])**2 + (map_y - center[1])**2)
    angle = np.arctan2(map_y - center[1], map_x - center[0])

    # 将距离映射到0-1范围内
    dist_normalized = dist / radius

    # 计算畸变的强度
    distortion = (dist_normalized**2) * angle_range

    # 应用畸变
    distorted_x = dist * np.cos(angle + distortion) + center[0]
    distorted_y = dist * np.sin(angle + distortion) + center[1]

    # 对图像进行透视变换
    map_distorted = cv2.remap(img, distorted_x, distorted_y, cv2.INTER_LINEAR)

    return map_distorted
# 定义扇形畸变的属性
center = (0, image.shape[0] // 2)  # 图像中心点坐标
radius = min(image.shape[1], image.shape[0]) // 1  # 半径
angle_range = np.pi / 6  # 畸变角度范围

# 进行扇形畸变
distorted_image = fan_distortion(image, center, radius, angle_range)

# 显示结果
cv2.imwrite("test.jpg",distorted_image)

在这里插入图片描述

五、透视畸变(不丢失信息)

import cv2
import numpy as np
import random

def random_perspective_distortion(img):
    height, width = img.shape[:2]
    # 定义透视变换后的四个顶点坐标(随机生成)
    random_offset = 150
    # 适当缩放原始图像
    
    img = cv2.copyMakeBorder(img, random_offset, random_offset, random_offset, random_offset, borderType=0)
    new_width = width + 2*random_offset
    new_height = height + 2*random_offset

    # 定义透视变换前的四个顶点坐标
    pts_original = np.float32([[0, 0], [new_width, 0], [0, new_height], [new_width, new_height]])
    
    pts_random = np.float32([[0 + np.random.uniform(-random_offset, random_offset), 
                            0 + np.random.uniform(-random_offset, random_offset)], 
                            [new_width + np.random.uniform(-random_offset, random_offset), 
                            0 + np.random.uniform(-random_offset, random_offset)], 
                            [0 + np.random.uniform(-random_offset, random_offset), 
                            new_height + np.random.uniform(-random_offset, random_offset)], 
                            [new_width + np.random.uniform(-random_offset, random_offset), 
                            new_height + np.random.uniform(-random_offset, random_offset)]])

    # 计算透视变换矩阵
    matrix = cv2.getPerspectiveTransform(pts_original, pts_random)

    # 对图像进行透视变换
    distorted_image = cv2.warpPerspective(img, matrix, (new_width, new_height))
    
    return distorted_image
# 读取图像
image = cv2.imread(r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg")

# 进行扇形畸变
distorted_image = random_perspective_distortion(image)

# 显示结果
cv2.imwrite("test.jpg",distorted_image)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

誓天断发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值