Python 结合opencv实现图片截取和拼接

发布时间 2023-09-25 00:39:36作者: 授客

实践环境

python 3.6.2

scikit-build-0.16.7

win10

opencv_python-4.5.4.60-cp36-cp36m-win_amd64.whl

下载地址:

https://pypi.org/project/opencv-python/4.5.4.60/#files

https://files.pythonhosted.org/packages/57/6c/7f4f56b2555d5c25dd4f41fc72a16dc6402cb2b4f967da11d8d26c669b55/opencv_python-4.5.4.60-cp36-cp36m-win_amd64.whl

注意:下载时不用下abi版的,比如 opencv_python-4.6.0.66-cp36-abi3-win_amd64.whl 不能用,

因为数据类型为 np.uint8,也就是0~255,

依赖包安装

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit-build # 解决     ModuleNotFoundError: No module named 'skbuild'问题
pip install opencv_python-4.5.4.60-cp36-cp36m-win_amd64.whl

代码实践

示例图片

代码

import os
import numpy as np
import cv2
from datetime import datetime
from PIL import Image

def capture_image(image_file_path, left, upper, width, height, target_file_name=None):
    '''截取图片'''

    right = left + width
    lower = upper + height
    if os.path.exists(image_file_path):
        image = Image.open(image_file_path)
        # width, height = image.size
        # print('图片宽度', width, '图片高度', height)

        head, ext = os.path.splitext(image_file_path)
        if not target_file_name:
            target_file_name = 'pic_captured%s%s' % (datetime.now().strftime('%Y%m%d%H%M%S%f'), ext)

        target_file_path = '%s%s' % (head, target_file_name)
        image.crop((left, upper, right, lower)).save(target_file_path)
        return target_file_path
    else:
        error_msg = '图片文件路径不存在:%s' % image_file_path
        print(error_msg)
        raise Exception(error_msg)


def append_picture(image1_path, image2_path):
    '''拼接图片'''

    image1 = cv2.imread(image1_path, -1)
    shape = image1.shape
    height1, width1, channel1 = shape
    # print(shape)     # 输出:(315, 510, 4)
    # print(image1)    # 输出一3维数组
    # print(len(image1), len(image1[0]))  # 输出:315 510

    image2 = cv2.imread(image2_path, -1)
    height2, width2, channel2 =  image2.shape

    total_height = max(height1, height2)
    total_width = width1 + width2

    dst = np.zeros((total_height, total_width, channel1), np.uint8)
    dst[0:height1, 0:width1] = image1

    dst[0:height2, width1:total_width] = image2
    cv2.imwrite("merge.png", dst)

if __name__ == '__main__':
    # 截取图片
    image_path1 = capture_image('example.png', 10, 30, 510, 315)
    image_path2 = capture_image('example.png', 520, 30, 518, 315)

    append_picture(image_path1, image_path2)

运行结果

截取的图片

合并的图片

代码补充说明

  1. imread(filename, flags=None)

    • filename 图片路径

    函数返回一个3三元组:(height, width, channel) ,元素中元素从左到右分别表示图片的高度,宽度,通道数(彩色图片是三通道的,每个通道表示图片的一种颜色(RGB),对于OpenCV读取到的图片的通道顺序是BGR) ,假设图片3元组为 (315, 510, 4) ,表示有315行,即315个二维数组,510列,即每个二维数组有510个一维数组。

    • flags 标志位

      • cv2.IMREAD_COLOR:默认参数,表示读入一副彩色图片,忽略alpha通道,可用1作为实参替代

      • cv2.IMREAD_GRAYSCALE:读入灰度图片,可用0作为实参替代

      • cv2.IMREAD_UNCHANGED:读入完整图片,包括alpha通道,可用-1作为实参替代

        PS:alpha通道,又称A通道,是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度复信息,定义透明、不透明和半透明区域,其中黑表示全透明,白表示不透明,灰表示半透明

  2. imwrite(filename, img, params=None)

    将图片矩阵以文件的形式储存起来

    • filename 待保存的图片路径

    • img Mat或Mat的矢量)要保存的一个或多个图像。

    • params 特定格式的参数对(paramId_1、paramValue_1、paramId_2、paramValue_2……),参阅cv::ImwriteFlags

  3. zeros(shape, dtype=None, order='C')

    返回一个用零填充的给定形状和类型的新数组(ndarray)

    • shape 整数或者整数元组。新数组的形状,例如(2, 3) or 2
    • dtype 数据类型,可选。数组所需的数据类型,比如,numpy.int8。 默认 numpy.float64
    • order {'C', 'F'},可选,默认: 'C'。是否在内存中按行优先(row-major)顺序(C语言风格)或者列优先(column-major)(Fortran风格)顺序存储多维数据。

    示例

    >>> import numpy as np
    
    # 创建2维数组
    >>> array = np.zeros([2, 3]) 
    >>> print(array) # 输出一个二维数组 一个包含2个一维数组,每个一维数组包含3个元素
    [[0. 0. 0.]
     [0. 0. 0.]]
    >>> array = np.zeros([2, 3], np.int64) # 指定数组元素数据类型为int64
    >>> print(array)
    [[0 0 0]
     [0 0 0]]
    >>> array = np.zeros([2, 3], np.float64) #  指定数组元素数据类型为float64
    >>> print(array)
    [[0. 0. 0.]
     [0. 0. 0.]]
    >>> array = np.zeros([3, 2])  # 输出一个二维数组 一个包含3个一维数组,每个一维数组包含2个元素
    >>> print(array)
    [[0. 0.]
     [0. 0.]
     [0. 0.]]
    
    # 创建3维数组
    >>> array = np.zeros((2, 3, 4), np.int8)
    >>> print(array) # 输出一个3维数组 一个包含2个二维数组,每个二维数组包含3个一维数组,每个一维数组包含4个元素
    [[[0 0 0 0]
      [0 0 0 0]
      [0 0 0 0]]
    
     [[0 0 0 0]
      [0 0 0 0]
      [0 0 0 0]]]
    
  4. 冒号在Numpy数组索引中的作用说明

    3维数组为例

    ndarray[index1:index2, index3:index4, index5:index6]

    indexN:indexM 表示获取索引在范围[indexN, indexM)内的数组元素(注意,不包含索引为indexM的元素),这里的indexN代表起始元素索引,可选,默认为0,indexM代表结束元素索引,可选,默认为所在层级数组元素个数+1

    index1:index2 表示获取三维数组中,索引在范围[index1, index2)内的数组元素,即二维数组

    index3:index4 表示获取上述二维数组中,索引在范围[index3, index4)内的数组元素,即一维数组

    index5:index6 表示获取上述一维数组中,索引在范围[index5, index6)内的数组元素

    示例

    >>> array = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[11, 12, 13], [14, 15, 16], [17, 18, 19]], [[20, 21, 22], [23, 24, 25], [26, 27, 28]]]) # 创建一个3维 ndarray
    >>> array
    array([[[ 1,  2,  3],
            [ 4,  5,  6],
            [ 7,  8,  9]],
    
           [[11, 12, 13],
            [14, 15, 16],
            [17, 18, 19]],
    
           [[20, 21, 22],
            [23, 24, 25],
            [26, 27, 28]]])
    >>> array[:] # 获取全部元素,等价于array[:, :, :]  
    array([[[ 1,  2,  3],
            [ 4,  5,  6],
            [ 7,  8,  9]],
    
           [[11, 12, 13],
            [14, 15, 16],
            [17, 18, 19]],
    
           [[20, 21, 22],
            [23, 24, 25],
            [26, 27, 28]]])
    >>> array[:, :, :]
    array([[[ 1,  2,  3],
            [ 4,  5,  6],
            [ 7,  8,  9]],
    
           [[11, 12, 13],
            [14, 15, 16],
            [17, 18, 19]],
    
           [[20, 21, 22],
            [23, 24, 25],
            [26, 27, 28]]])
    
    >>> array[1:2]  # 获取索引在[1,2)范围内的二维数组
    array([[[11, 12, 13],
            [14, 15, 16],
            [17, 18, 19]]])
    >>> array[1:]    # 获取索引在[1,3)范围内的二维数组
    array([[[11, 12, 13],
            [14, 15, 16],
            [17, 18, 19]],
    
           [[20, 21, 22],
            [23, 24, 25],
            [26, 27, 28]]])
    >>> array[:2]    # 获取索引在[0,2)范围内的二维数组
    array([[[ 1,  2,  3],
            [ 4,  5,  6],
            [ 7,  8,  9]],
    
           [[11, 12, 13],
            [14, 15, 16],
            [17, 18, 19]]])
            
    >>> array[1:2, 1:2] # 获取索引在[1,2)范围内的二维数组,二维数组中只获取索引在[1,2)范围内的一维数组
    array([[[14, 15, 16]]])
    >>> array[1:2, :2]  # 获取索引在[1,2)范围内的二维数组,二维数组中只获取索引在[0,2)范围内的一维数组
    array([[[11, 12, 13],
            [14, 15, 16]]])
    >>> array[1:2, 1:]  # 获取索引在[1,2)范围内的二维数组,二维数组中只获取索引在[1,3)范围内的一维数组
    array([[[14, 15, 16],
            [17, 18, 19]]])
    >>> array[1:2, :]   # 获取索引在[1,2)范围内的二维数组的全部元素
    array([[[11, 12, 13],
            [14, 15, 16],
            [17, 18, 19]]])
    >>> array[1:2, 1:2, 1:2]  # 获取索引在[1,2)范围内的二维数组,二维数组中只获取索引在[1,2)范围内的一维数组,一维数组中只获取索引在[1,2)范围内的元素
    array([[[15]]])
    >>> array[1:2, 1:2, 1:]   # 获取索引在[1,2)范围内的二维数组,二维数组中只获取索引在[1,2)范围内的一维数组,一维数组中只获取索引在[1,3)范围内的元素
    array([[[15, 16]]])
    >>> array[1:2, 1:2, :2]   # 获取索引在[1,2)范围内的二维数组,二维数组中只获取索引在[1,2)范围内的一维数组,一维数组中只获取索引在[0,2)范围内的元素
    array([[[14, 15]]])
    >>> array[1:2, 1:2, :]    # 获取索引在[1,2)范围内的二维数组,二维数组中只获取索引在[1,2)范围内的一维数组,获取一维数组的所有元素
    array([[[14, 15, 16]]])