栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

numpy数组做图片拼接的实现(concatenate、vstack、hstack)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

numpy数组做图片拼接的实现(concatenate、vstack、hstack)

两种方法拼接

#img = np.vstack((img, img2))  # vstack按垂直方向,hstack按水平方向
img = np.concatenate((img, img2), axis=0)  # axis=0 按垂直方向,axis=1 按水平方向

统一图片大小,保证数组维度一致避免拼接失败。 把图片全部调整成第一张图的宽高

def img_size(image_names,width, height):
  for i in image_names:
    img = cv2.imread(os.path.join(img_path, i))
    img_resize = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
    cv2.imwrite(os.path.join(img_path, i), img_resize)
    print(os.path.join(img_path, i))

完整案例,拼接文件夹中的所有图片

import cv2
import os
import numpy as np

def img_size(image_names,width, height):
  for i in image_names:
    img = cv2.imread(os.path.join(img_path, i))
    img_resize = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
    cv2.imwrite(os.path.join(img_path, i), img_resize)
    print(os.path.join(img_path, i))

if __name__ == '__main__':
  img_path = r'F:studytest'
  image_names = [name for name in os.listdir(img_path) if os.path.splitext(name)[1] == ".jpg"]
  img1 = cv2.imread(os.path.join(img_path, image_names[0]))
  width, height = img1.shape[:2][::-1]
  img_size(image_names,width, height)
  img = img1

  for i in range(1,len(image_names)):
    img_page = image_names[i]
    img2 = cv2.imread(os.path.join(img_path, img_page))
    #img = np.vstack((img, img2))  # vstack按垂直方向,hstack按水平方向
    img = np.concatenate((img, img2), axis=0)  # axis=0 按垂直方向,axis=1 按水平方向
  cv2.imwrite(os.path.join(img_path,"res.jpg"), img)
  # cv2.imshow("img",img)
  # cv2.waitKey()
``

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/23440.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号