CSDN博客地址

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")
posted @ 2020-03-24 10:48  Yi_warmth  阅读(153)  评论(0编辑  收藏  举报
CSDN博客地址