Python -- OpenCV2实现静态图像的几何变换

import cv2
import numpy as np

# 将图片平移100px
def test():
    img = cv2.imread("1.JPG", 1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]

    dst = np.zeros(imgInfo, np.uint8)
    for i in range(height):
        for j in range(width - 100):
            dst[i, j - 100] = img[i, j]
    cv2.imshow('image', dst)
    cv2.waitKey(0)

# test()

# 图片镜像
def test1():
    img = cv2.imread("1.JPG", 1)
    cv2.imshow("src", img)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    deep = imgInfo[2]

    dst = np.zeros([height * 2, width, deep], np.uint8)
    for i in range(height):
        for j in range(width):
            dst[i, j] = img[i, j]
            dst[height*2-i-1, j] = img[i, j]

    for i in range(width):
        dst[height, i] = (0, 0, 255)
    cv2.imshow('image', dst)
    cv2.waitKey(0)

# test1()

# 缩放
def test2():
    img = cv2.imread("1.JPG", 1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    mode = imgInfo[2]
    # 1 放大 缩小 2 等比例 非等比例
    destheight = int(height * 2)
    destwidth = int(width * 2)

    # 最近邻域插值 双线性插值 像素关系重采样 立方插值
    dest = cv2.resize(img, (destheight, destwidth))
    cv2.imshow("image", dest)
    cv2.waitKey(0)

# test2()


def test3():
    img = cv2.imread("1.JPG", 1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]

    destheight = int(height/2)
    destwidth = int(width/2)

    dest = np.zeros([destheight, destwidth, 3], np.uint8)
    for i in range(destheight):
        for j in range(destwidth):
            iNew = i * (height * 1.0 / destheight)
            jNew = j * (width * 1.0 / destwidth)
            dest[i, j] = img[int(iNew), int(jNew)]
    cv2.imshow("image", dest)
    cv2.waitKey(0)


# test3()

# 旋转
def test4():
    img = cv2.imread("1.JPG", 1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]

    # 定义一个旋转矩阵:  cv2.getRotationMatrix2D(),参数1:需要旋转的中心点.参数2:需要旋转的角度.参数三:需要缩放的比例
    matRotation = cv2.getRotationMatrix2D((int(height / 2), int(width / 2)), 45, 2)
    dst = cv2.warpAffine(img, matRotation, (height, width))
    cv2.imshow("image", dst)
    cv2.waitKey(0)

# test4()

# 仿射
def test5():
    img = cv2.imread('1.JPG', 1)
    cv2.imshow('src', img)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    # src 3 -> dst 3 (左上角, 左下角,右上角)
    """需要确定图像矩阵的三个点坐标, 及(左上角, 左下角, 右上角).定义两个矩阵, matSrc
    为原图的三个点坐标, matDst为进行仿射的三个点坐标"""
    matSrc = np.float32([[0, 0], [0, height - 1], [width - 1, 0]])  # 需要注意的是 行列 和 坐标 是不一致的
    matDst = np.float32([[50, 50], [100, height - 50], [width - 200, 100]])

    matAffine = cv2.getAffineTransform(matSrc, matDst)  # mat 1 src 2 dst 形成组合矩阵
    dst = cv2.warpAffine(img, matAffine, (height, width))
    cv2.imshow('image', dst)
    cv2.waitKey(0)


# test5()

pil_img = Image.open("1.jpg").convert("L")
# 创建缩略图
pil_img.thumbnail((128, 128))
pil_img.save("2.jpg")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Enougme

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

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

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

打赏作者

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

抵扣说明:

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

余额充值