[opencv][python] 学习手册1:练习代码2

[opencv][python] 学习手册1:练习代码2

09_图像的平移.py
10_图像平移的api.py
11_图像的旋转.py
12_图像的仿射变换.py
13_图像金字塔.py
14_图像的融合.py
15_灰度图.py
16_灰度图的颜色反转.py
17_彩色图的颜色反转.py
18_马赛克效果.py


文章目录

  • [opencv][python] 学习手册1:练习代码2
    • 09_图像的平移.py
    • 10_图像平移的api.py
    • 11_图像的旋转.py
    • 12_图像的仿射变换.py
    • 13_图像金字塔.py
    • 14_图像的融合.py
    • 15_灰度图.py
    • 16_灰度图的颜色反转.py
    • 17_彩色图的颜色反转.py
    • 18_马赛克效果.py



09_图像的平移.py

代码

"""
需求:
1. 读取图片并获取信息
2. 创建一个同等大小的画布
3. 定义平移矩阵
4. 通过双for循环对原图像进行平移操作(结合课件笔记)
5. 显示图像并等待鼠标按键

# 平移矩阵原理
# 思考:向左上平移如何不用原图填充
-------------------------------------------------
    x = 10
    y = 20

    # 原始坐标点  齐次矩阵
    matrixA = np.array([x,y,1])

    # 平移的转换矩阵 2*3
    matrixB = np.array([[1,0,50],
                        [0,1,100]])

    #               2*3       3*1    ---> 2*1
    resultMatrix = matrixB.dot(matrixA.T)
    print(resultMatrix)

"""

import cv2 as cv
import numpy as np

# 1. 读取图片并获取信息
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_h = src.shape[0]
src_w = src.shape[1]

# 2. 创建一个同等大小的三通道画布
dst = np.zeros((src_h, src_w, 3), np.uint8)

# 3. 定义平移矩阵(2x3),平移矩阵必须是整形,使用 np.array
# matrix_translate = np.array([
#     [1, 0, -50],
#     [0, 1, 0]
# ])

matrix_translate = np.float32([
    [1, 0, -50],
    [0, 1, 0]
])

# 4. 通过双for循环对原图像进行平移操作(结合课件笔记)
for row in range(src_h):
    for col in range(src_w):
        # 定义原矩阵的转置 (1x3), (x, y, 1) <==> (col, row, 1)
        matrix_src = np.array([col, row, 1])
        # 计算结果矩阵 rst_21 = mT_23 (dot) src_31
        matrix_result = matrix_translate.dot(matrix_src.T)
        # 获得结果矩阵的坐标 (x, y) <==> (col, row)
        rst_col = int(matrix_result[0])
        rst_row = int(matrix_result[1])
        # print("rst ({0},{1})".format(rst_row, rst_col))

        # 只有 rst_row 小于 row,rst_col 小于 src_w 才能上色
        if rst_row < src_h and rst_col < src_w:
            # 将负索引区域截断,通过不停地在轴0区域进行赋值,直到索引不为负
            if rst_row < 0:
                rst_row = 0
            if rst_col < 0:
                rst_col = 0
            dst[rst_row, rst_col] = src[row, col]

# 5. 显示图像并等待鼠标按键
cv.imshow("src", src)
cv.imshow("dst", dst)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第1张图片


10_图像平移的api.py

代码

"""
需求:
1. 读取图片并获取信息
2. 定义平移矩阵
3. 仿射变换 affine
4. 显示图像并等待鼠标按键

变换 = 缩放 + 旋转 + 平移

"""

import cv2 as cv
import numpy as np

# 1. 读取图片并获取信息
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_h = src.shape[0]
src_w = src.shape[1]

# 2. 定义变换矩阵(平移+旋转)(2x3)
# 定义缩放矩阵
matrix_scale = np.float32([
    [0.5, 0],
    [0, 0.7]
])

# 定义平移矩阵
matrix_translate = np.array([
    [1, 0, -50],
    [0, 1, 0]
])

# 计算变换矩阵 = 缩放(dot)旋转(dot)平移
matrix_shift = matrix_scale.dot(matrix_translate)

# matrix_shift = np.float32([
#     [1, 0, -50],
#     [0, 1, -100]
# ])


# 3. 仿射变换,通用 api
dst = cv.warpAffine(src, matrix_shift, (src_h, src_w))

# 4. 显示图像并等待鼠标按键
cv.imshow("src", src)
cv.imshow("dst", dst)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第2张图片


11_图像的旋转.py

代码

"""
需求:
1. 读取图像,获取信息
2. 定义旋转矩阵
3. 仿射变换api计算结果图像
4. 显示图像,等待按键
"""

import cv2 as cv
import numpy as np


def angle_2_radian(angle):
    """todo
    角度转弧度  π/180×角度
    :param angle: 角度值
    :return: 弧度值
    """
    pass


def radian_2_angle(radian):
    """todo
    弧度变角度  180/π×弧度
    :param radian: 弧度值
    :return: 角度值
    """
    pass


def get_rotation_matrix(angle):
    """
    自定义旋转矩阵计算函数
    :param angle: 旋转角度值
    :return: 旋转矩阵
    """
    radian = np.deg2rad(angle)
    return np.float32([
        [np.cos(radian), -np.sin(radian), 0],
        [np.sin(radian), np.cos(radian), 0]
    ])


# 1. 读取图像,获取信息
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_h = src.shape[0]
src_w = src.shape[1]

# 2. 定义旋转矩阵 自定义旋转矩阵函数
# matrix_rotate = get_rotation_matrix(30)

# 2. 定义旋转矩阵 getRotationMatrix api
matrix_rotate = cv.getRotationMatrix2D((src_h / 2, src_w / 2), 45, 0.3)

# 3. 仿射变换api计算结果图像
dst = cv.warpAffine(src, matrix_rotate, (src_h, src_w))

# 4. 显示图像,等待按键
cv.imshow("src", src)
cv.imshow("dst", dst)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第3张图片


12_图像的仿射变换.py

代码

"""
需求:
通过原始矩阵与目标位姿矩阵计算变换矩阵
再通过变换矩阵将原始图像转化到目标位姿

1. 读取图像,获取信息
2. 在原始图像中定义原始矩阵
3. 定义目标矩阵
4. 计算变换矩阵,通过 getAffineTransform api
5. 通过计算得到的变换矩阵将原始图像转换到目标位姿
6. 显示图像,等待按键
"""

import cv2 as cv
import numpy as np

# 1. 读取图像,获取信息
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_h = src.shape[0]
src_w = src.shape[1]

# 2. 在原始图像中定义原始矩阵 ,左上角,左下角,右上(下?)角
matrix_src = np.float32([
    [0, 0],
    [0, src_w],
    [src_h, src_w]
])

# 3. 定义目标矩阵 
# matrix_dst = np.float32([
#     [100, 50],
#     [200, src_h - 50],
#     [src_w - 200, 100]
# ])

matrix_dst = np.float32([
    [100, 50],
    [150, 350],
    [370, 330]
])

# 4. 计算变换矩阵,通过 getAffineTransform api
# Calculates an affine transform from three pairs of the corresponding points.
# src	Coordinates of triangle vertices in the source image.
# dst	Coordinates of the corresponding triangle vertices in the destination image.
matrix_shift = cv.getAffineTransform(matrix_src, matrix_dst)
print("matrix_shift:\n{0}".format(matrix_shift))

# 5. 通过计算得到的变换矩阵将原始图像转换到目标位姿
dst = cv.warpAffine(src, matrix_shift, (src_h, src_w))

# 6. 显示图像,等待按键
cv.imshow("src", src)
cv.imshow("dst", dst)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第4张图片


13_图像金字塔.py

代码

"""
需求:
金字塔下采样与resize对比

1. 进行4次金字塔下采样
2. 进行4次resize
"""

import cv2 as cv
import numpy as np


# 读取图像,获得信息
# filename = r"../img/sky (1).jpg"
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_h = src.shape[0]
src_w = src.shape[1]
cv.imshow("src", src)

# 1. 进行四次金字塔下采样,默认结果尺寸为原图的二分之一
dst = cv.pyrDown(src)
cv.imshow("d1/2", dst)

dst = cv.pyrDown(dst)
cv.imshow("d1/4", dst)

dst = cv.pyrDown(dst)
cv.imshow("d1/8", dst)

dst = cv.pyrDown(dst)
cv.imshow("d1/16", dst)

# 2. 进行四次resize
ret = cv.resize(src, (int(src_w / 2), int(src_h / 2)))
cv.imshow("r1/2", ret)

ret = cv.resize(ret, (int(src_w / 4), int(src_h / 4)))
cv.imshow("r1/4", ret)

ret = cv.resize(ret, (int(src_w / 8), int(src_h / 8)))
cv.imshow("r1/8", ret)

ret = cv.resize(ret, (int(src_w / 16), int(src_h / 16)))
cv.imshow("r1/16", ret)

# 等待按键
key = cv.waitKey(0)
print("key = ", key)

运行结果
resize
[opencv][python] 学习手册1:练习代码2_第5张图片
pyrdown
[opencv][python] 学习手册1:练习代码2_第6张图片


14_图像的融合.py

代码

"""
需求:
1. 读取两张图像
2. 融合两张图像,通过 addWeighted api
3. 显示图像,等待按键

# 图像融合的前提: 宽高要一致 img1 150 75 img2 50 25 100 + 155 ==》 255 白
#                   1. 图1 2.系数 3. 图2  4.系数  5. 偏移量
"""

import cv2 as cv
import numpy as np

# 1. 读取两张图像
filename1 = "../img/room.jpg"
filename2 = "../img/tony.jpg"
src1 = cv.imread(filename1, cv.IMREAD_COLOR)
src2 = cv.imread(filename2, cv.IMREAD_COLOR)

# 2. 融合两张图像,通过 addWeighted api
dst = cv.addWeighted(src1, 0.5, src2, 0.5, 0)

# 3. 显示图像,等待按键
cv.imshow("src1", src1)
cv.imshow("src2", src2)
cv.imshow("dst", dst)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第7张图片


15_灰度图.py

代码

"""
需求:
将三通道彩色图像转化为单通道灰度图
通过三种方法

方法1:在读取图像时直接通过参数转化
方法2;通过cv的cvColor api将读取的彩色图转化
方法3:通过公式 (B+G+R)/3 计算单通道灰度值,再将此值填充进画布
方法4:通过公式 R*0.299 + G*0.587 + B*0.114 计算出单通道灰度值,再将此值填充进画布


"""

import cv2 as cv
import numpy as np

# 读取图像,并获取信息
filename = r"../img/lena.jpg"

# 方法1:在读取图像时直接通过参数转化
src1 = cv.imread(filename, cv.IMREAD_GRAYSCALE)
cv.imshow("src1_gray", src1)

# 方法2;通过cv的cvColor api将读取的彩色图转化
src2 = cv.imread(filename, cv.IMREAD_COLOR)
src2_gray = cv.cvtColor(src2, cv.COLOR_BGR2GRAY)
cv.imshow("src2_gray", src2_gray)

# 方法3:通过公式 (B+G+R)/3 计算单通道灰度值,再将此值填充进画布
src3 = cv.imread(filename, cv.IMREAD_COLOR)
src3_h = src3.shape[0]
src3_w = src3.shape[1]

dst1 = np.zeros((src3_h, src3_w), np.uint8)  # (row, col), (0-255)
for row in range(src3_h):
    for col in range(src3_w):
        # color_value = src3[row, col][0] + src3[row, col][1] + src3[row, col][2]  # RuntimeWarning: overflow encountered in ubyte_scalars, uint 溢出
        # print(color_value)

        # 将 uint(0-255)转化为 int
        B = int(src3[row, col][0])
        G = int(src3[row, col][1])
        R = int(src3[row, col][2])
        color_value = B + G + R
        color_value = color_value / 3
        dst1[row, col] = color_value

cv.imshow("dst", dst1)

# 方法4:通过心理学公式 R*0.299 + G*0.587 + B*0.114 计算出单通道灰度值,再将此值填充进画布
dst2 = np.zeros((src3_h, src3_w), np.uint8)  # (row, col), (0-255)
for row in range(src3_h):
    for col in range(src3_w):
        # 将 uint(0-255)转化为 int
        B = int(src3[row, col][0])
        G = int(src3[row, col][1])
        R = int(src3[row, col][2])
        color_value = int(R*0.299 + G*0.587 + B*0.114)
        dst2[row, col] = color_value

cv.imshow("dst2", dst2)

# 等待按键
key = cv.waitKey(0)
print("key = ", key)

运行结果


16_灰度图的颜色反转.py

代码

"""
需求:
对灰度图进行颜色反转,255-当前灰度值,类型uint

1. 读取图像,获取信息,转成灰度图
2. 通过公式对灰度图进行反转
3. 显示图像,等待按键
"""

import cv2 as cv
import numpy as np

# 1. 读取图像,获取信息,转成灰度图
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
src_gray_h = src_gray.shape[0]
src_gary_w = src_gray.shape[1]

# 2. 通过公式对灰度图进行反转
dst = np.zeros((src_gray_h, src_gary_w), np.uint8)  # (row, col), uint(0-255)

for row in range(src_gray_h):
    for col in range(src_gary_w):
        inv_color = 255 - src_gray[row, col]
        dst[row, col] = inv_color

# 3. 显示图像,等待按键
cv.imshow("src_gray", src_gray)
cv.imshow("inv_gray", dst)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第8张图片


17_彩色图的颜色反转.py

代码

"""
需求:
对彩色图进行颜色反转,255-当前灰度值,类型uint
目标画布是三通道

1. 读取图像,获取信息,
2. 通过公式对彩色图每一个通道进行反转
3. 显示图像,等待按键
"""

import cv2 as cv
import numpy as np

# 1. 读取图像,获取信息,转成灰度图
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_h = src.shape[0]
src_w = src.shape[1]

# 2. 通过公式对彩色图进行反转
dst = np.zeros((src_h, src_w, 3), np.uint8)  # (row, col), uint(0-255)

for row in range(src_h):
    for col in range(src_w):
        inv_b = 255 - src[row, col][0]
        inv_g = 255 - src[row, col][1]
        inv_r = 255 - src[row, col][2]
        dst[row, col] = (inv_b, inv_g, inv_r)

# 3. 显示图像,等待按键
cv.imshow("src", src)
cv.imshow("inv", dst)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第9张图片


18_马赛克效果.py

原理
[opencv][python] 学习手册1:练习代码2_第10张图片

代码

"""
需求:
将图像中的 roi 进行马赛克处理
每一个马赛克块大小是 row x col = offset x offset
offset 是像素距数目值,
如果 offset = 10,表示将 10 x 10像素区域做成一个马赛克块

具体做法
1. 选择 roi
2. 通过 offset,将 roi 划分区域块,并选定每个区域块的锚定点
3. 将每个区域块的其他像素赋值成锚定点像素

流程
1. 读取图像,并获取信息
2. 选择 roi
3. 定义 offset
4. 对 roi 进行马赛克处理
5. 显示图像,等待按键
"""

import cv2 as cv
import numpy as np

# 1. 读取图像,并获取信息
filename = r"../img/lena.jpg"
src = cv.imread(filename, cv.IMREAD_COLOR)
src_h = src.shape[0]
src_w = src.shape[1]

# 2. 选择 roi(手动),(兴趣区域以后会通过机器学习获得)
roi = src[175:240, 180:300]
roi_h = roi.shape[0]
roi_w = roi.shape[1]

# 3. 定义 offset
offset = 10

# 4. 对 roi 进行马赛克处理
for row in range(roi_h):
    for col in range(roi_w):
        # 划分区块,确定当前区块锚定像素
        if row % offset == 0 and col % offset == 0:
            anchor_pix = roi[row, col]
            # 对当前区块剩余像素赋值
            for i in range(row, row + offset):
                for j in range(col, col + offset):
                    # 在边界内的像素点可以被赋值
                    if i < roi_h and j < roi_w:
                        roi[i, j] = anchor_pix
        else:
            pass

# 5. 显示图像,等待按键
cv.imshow("roi", roi)
cv.imshow("src", src)

key = cv.waitKey(0)
print("key = ", key)

运行结果
[opencv][python] 学习手册1:练习代码2_第11张图片

你可能感兴趣的:(opencv)