欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 結(jié)合opencv實(shí)現(xiàn)圖片截取和拼接代碼實(shí)踐

 更新時(shí)間:2023年09月27日 11:48:04   作者:授客  
這篇文章主要介紹了Python 結(jié)合opencv實(shí)現(xiàn)圖片截取和拼接代碼實(shí)踐,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

實(shí)踐環(huán)境

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

注意:下載時(shí)不用下abi版的,比如 opencv_python-4.6.0.66-cp36-abi3-win_amd64.whl 不能用,

因?yàn)閿?shù)據(jù)類型為 np.uint8,也就是0~255,

依賴包安裝

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit-build # 解決     ModuleNotFoundError: No module named 'skbuild'問(wèn)題
pip install opencv_python-4.5.4.60-cp36-cp36m-win_amd64.whl

代碼實(shí)踐

示例圖片

代碼

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維數(shù)組
    # 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)

運(yùn)行結(jié)果

截取的圖片

合并的圖片

代碼補(bǔ)充說(shuō)明

1.imread(filename, flags=None)

  • filename 圖片路徑

函數(shù)返回一個(gè)3三元組: (height, width, channel) ,元素中元素從左到右分別表示圖片的高度,寬度,通道數(shù)(彩色圖片是三通道的,每個(gè)通道表示圖片的一種顏色( RGB ),對(duì)于OpenCV讀取到的圖片的通道順序是 BGR ) ,假設(shè)圖片3元組為 (315, 510, 4) ,表示有315行,即315個(gè)二維數(shù)組,510列,即每個(gè)二維數(shù)組有510個(gè)一維數(shù)組。

  • flags 標(biāo)志位
    • cv2.IMREAD_COLOR :默認(rèn)參數(shù),表示讀入一副彩色圖片,忽略alpha通道,可用1作為實(shí)參替代
    • cv2.IMREAD_GRAYSCALE :讀入灰度圖片,可用0作為實(shí)參替代
    • cv2.IMREAD_UNCHANGED :讀入完整圖片,包括alpha通道,可用-1作為實(shí)參替代

PS: alpha 通道,又稱A通道,是一個(gè)8位的灰度通道,該通道用256級(jí)灰度來(lái)記錄圖像中的透明度復(fù)信息,定義透明、不透明和半透明區(qū)域,其中黑表示全透明,白表示不透明,灰表示半透明

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

將圖片矩陣以文件的形式儲(chǔ)存起來(lái)

  • filename 待保存的圖片路徑
  • img Mat或Mat的矢量)要保存的一個(gè)或多個(gè)圖像。
  • params 特定格式的參數(shù)對(duì)(paramId_1、paramValue_1、paramId_2、paramValue_2……),參閱cv::ImwriteFlags

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

返回一個(gè)用零填充的給定形狀和類型的新數(shù)組( ndarray )

  • shape 整數(shù)或者整數(shù)元組。新數(shù)組的形狀,例如 (2, 3) or 2
  • dtype 數(shù)據(jù)類型,可選。數(shù)組所需的數(shù)據(jù)類型,比如, numpy.int8 。 默認(rèn) numpy.float64
  • order {'C', 'F'} ,可選,默認(rèn): 'C' 。是否在內(nèi)存中按行優(yōu)先(row-major)順序(C語(yǔ)言風(fēng)格)或者列優(yōu)先(column-major)(Fortran風(fēng)格)順序存儲(chǔ)多維數(shù)據(jù)。

示例

>>> import numpy as np
# 創(chuàng)建2維數(shù)組
>>> array = np.zeros([2, 3]) 
>>> print(array) # 輸出一個(gè)二維數(shù)組 一個(gè)包含2個(gè)一維數(shù)組,每個(gè)一維數(shù)組包含3個(gè)元素
[[0. 0. 0.]
 [0. 0. 0.]]
>>> array = np.zeros([2, 3], np.int64) # 指定數(shù)組元素?cái)?shù)據(jù)類型為int64
>>> print(array)
[[0 0 0]
 [0 0 0]]
>>> array = np.zeros([2, 3], np.float64) #  指定數(shù)組元素?cái)?shù)據(jù)類型為float64
>>> print(array)
[[0. 0. 0.]
 [0. 0. 0.]]
>>> array = np.zeros([3, 2])  # 輸出一個(gè)二維數(shù)組 一個(gè)包含3個(gè)一維數(shù)組,每個(gè)一維數(shù)組包含2個(gè)元素
>>> print(array)
[[0. 0.]
 [0. 0.]
 [0. 0.]]
# 創(chuàng)建3維數(shù)組
>>> array = np.zeros((2, 3, 4), np.int8)
>>> print(array) # 輸出一個(gè)3維數(shù)組 一個(gè)包含2個(gè)二維數(shù)組,每個(gè)二維數(shù)組包含3個(gè)一維數(shù)組,每個(gè)一維數(shù)組包含4個(gè)元素
[[[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.冒號(hào)在Numpy數(shù)組索引中的作用說(shuō)明

3維數(shù)組為例

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

indexN:indexM 表示獲取索引在范圍 [indexN, indexM) 內(nèi)的數(shù)組元素(注意,不包含索引為 indexM 的元素),這里的 indexN 代表起始元素索引,可選,默認(rèn)為0, indexM 代表結(jié)束元素索引,可選,默認(rèn)為所在層級(jí)數(shù)組元素個(gè)數(shù)+1

index1:index2 表示獲取三維數(shù)組中,索引在范圍 [index1, index2) 內(nèi)的數(shù)組元素,即二維數(shù)組

index3:index4 表示獲取上述二維數(shù)組中,索引在范圍 [index3, index4) 內(nèi)的數(shù)組元素,即一維數(shù)組

index5:index6 表示獲取上述一維數(shù)組中,索引在范圍 [index5, index6) 內(nèi)的數(shù)組元素

示例

>>> 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]]]) # 創(chuàng)建一個(gè)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[:] # 獲取全部元素,等價(jià)于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)范圍內(nèi)的二維數(shù)組
array([[[11, 12, 13],
        [14, 15, 16],
        [17, 18, 19]]])
>>> array[1:]    # 獲取索引在[1,3)范圍內(nèi)的二維數(shù)組
array([[[11, 12, 13],
        [14, 15, 16],
        [17, 18, 19]],
       [[20, 21, 22],
        [23, 24, 25],
        [26, 27, 28]]])
>>> array[:2]    # 獲取索引在[0,2)范圍內(nèi)的二維數(shù)組
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)范圍內(nèi)的二維數(shù)組,二維數(shù)組中只獲取索引在[1,2)范圍內(nèi)的一維數(shù)組
array([[[14, 15, 16]]])
>>> array[1:2, :2]  # 獲取索引在[1,2)范圍內(nèi)的二維數(shù)組,二維數(shù)組中只獲取索引在[0,2)范圍內(nèi)的一維數(shù)組
array([[[11, 12, 13],
        [14, 15, 16]]])
>>> array[1:2, 1:]  # 獲取索引在[1,2)范圍內(nèi)的二維數(shù)組,二維數(shù)組中只獲取索引在[1,3)范圍內(nèi)的一維數(shù)組
array([[[14, 15, 16],
        [17, 18, 19]]])
>>> array[1:2, :]   # 獲取索引在[1,2)范圍內(nèi)的二維數(shù)組的全部元素
array([[[11, 12, 13],
        [14, 15, 16],
        [17, 18, 19]]])
>>> array[1:2, 1:2, 1:2]  # 獲取索引在[1,2)范圍內(nèi)的二維數(shù)組,二維數(shù)組中只獲取索引在[1,2)范圍內(nèi)的一維數(shù)組,一維數(shù)組中只獲取索引在[1,2)范圍內(nèi)的元素
array([[[15]]])
>>> array[1:2, 1:2, 1:]   # 獲取索引在[1,2)范圍內(nèi)的二維數(shù)組,二維數(shù)組中只獲取索引在[1,2)范圍內(nèi)的一維數(shù)組,一維數(shù)組中只獲取索引在[1,3)范圍內(nèi)的元素
array([[[15, 16]]])
>>> array[1:2, 1:2, :2]   # 獲取索引在[1,2)范圍內(nèi)的二維數(shù)組,二維數(shù)組中只獲取索引在[1,2)范圍內(nèi)的一維數(shù)組,一維數(shù)組中只獲取索引在[0,2)范圍內(nèi)的元素
array([[[14, 15]]])
>>> array[1:2, 1:2, :]    # 獲取索引在[1,2)范圍內(nèi)的二維數(shù)組,二維數(shù)組中只獲取索引在[1,2)范圍內(nèi)的一維數(shù)組,獲取一維數(shù)組的所有元素
array([[[14, 15, 16]]])

到此這篇關(guān)于Python 結(jié)合opencv實(shí)現(xiàn)圖片截取和拼接的文章就介紹到這了,更多相關(guān)Python opencv圖片截取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論