【数字图像处理】实验二:几何变换与变形(python)

实验2.1:图像缩放

实现一个图像缩放函数,可以对输入图像进行任意倍数的缩放;

采用双线性插值进行重采样;

X,Y方向的缩放倍数参函数参数的形式传入;

可以只考虑输入图像为3通道,8位深度的情况;

不能调用图像处理库的缩放函数来完成;

void Scale(const MyImage &input, MyImage &output, double sx, double sy);

代码:

import cv2
import numpy as np
import math

# input:输入图像;sx、sy:缩放倍数
def Scale(img, sx, sy):

    # 得到图像的长、宽
    old_height, old_width = img.shape[:2]
    # 计算新的长、宽
    new_width = round(old_width*sx)
    new_height = round(old_height*sy)

    dst = np.zeros((new_height, new_width, 3), dtype = np.uint8)
    # 双线性插值法
    for i in range(3):# 通道
        for h in range(new_height):# 高度
            for w in range(new_width):# 宽度
                # 目标在原图像上的位置
                src_x = (w + 0.5) / sx - 0.5
                src_y = (h + 0.5) / sy - 0.5

                # 计算在原图像上四个近邻点的位置
                src_x_0 = int(np.floor(src_x))
                src_y_0 = int(np.floor(src_y))
                src_x_1 = min(src_x_0 + 1, old_width - 1)
                src_y_1 = min(src_y_0 + 1, old_height - 1)

                #双线性插值
                t0 = (src_x_1 - src_x) * img[src_y_0, src_x_0, i] + (src_x - src_x_0) * img[src_y_0, src_x_1, i]
                t1 = (src_x_1 - src_x) * img[src_y_1, src_x_0, i] + (src_x - src_x_0) * img[src_y_1, src_x_1, i]
                dst[h, w, i] = int((src_y_1 - src_y) * t0 + (src_y - src_y_0) * t1)
    return dst

if __name__ == '__main__':
    # 读入图像
    img = cv2.imread("1.jpg")
    # 创建一个窗口
    cv2.namedWindow("Image")
    # 显示图像
    cv2.imshow("input_image", img)

    img_out = Scale(img, 0.5, 0.5)

    cv2.imshow('output_image', img_out)
    cv2.waitKey(0)

实验2.2:图像变形

[x’, y’]=f([x, y])为像素坐标的一个映射,实现f所表示的图像形变。f的逆映射为:

输入图像:

输出:

代码:

# coding=utf-8
import cv2
import math
import numpy as np

def transform(src):
    # 源图像宽高
    height, width = src.shape[:2]
    #新建空矩阵存放新图像
    dst = np.zeros((height, width, 3), dtype=np.uint8)
    for i in range(3):  # 对通道遍历
        for h in range(height): # 对高遍历
            for w in range(width): # 对宽遍历
                #中心归一化坐标
                x = w/(0.5 * width) - 1
                y = h/(0.5 * height) - 1
                # 计算r
                r = math.sqrt(pow(x, 2.0) + pow(y, 2.0))
                # 计算角度
                theta = pow(1 - r, 2.0)
                if r>=1:
                    srcx = x
                    srcy = y
                else:
                    srcx = math.cos(theta)*x - math.sin(theta)*y
                    srcy = math.sin(theta)*x + math.cos(theta)*y
                #中心归一化还原
                sx = int((srcx + 1) * 0.5 * width)
                sy = int((srcy + 1) * 0.5 * height)
                dst[h, w, i] = src[sy, sx, i]
    return dst

if __name__ == '__main__':
    img_in = cv2.imread('1.jpg')
    img_out = transform(img_in)

    cv2.imshow('input_image', img_in)
    cv2.imshow('output_image', img_out)
    cv2.waitKey(0)

 

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值