python图像拼接_python numpy 和 opencv 图像拼接

将多副图片拼接为一幅大图片

20180823150919_853.jpg

加入白边,分割图像

20180823150919_534.jpg

白边的增加使用了遮罩层

import numpy as np

a = np.ones((10, 10))

print(a)

mask = np.ones(a.shape[:2],dtype=np.bool)

mask[2:-2, 2:-2] = 0

print(mask)

a[mask] = 0

print(a)

[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]

[[ True True True True True True True True True True]

[ True True True True True True True True True True]

[ True True False False False False False False True True]

[ True True False False False False False False True True]

[ True True False False False False False False True True]

[ True True False False False False False False True True]

[ True True False False False False False False True True]

[ True True False False False False False False True True]

[ True True True True True True True True True True]

[ True True True True True True True True True True]]

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

交换轴有点懵逼。。。。

import numpy as np

import cv2 as cv

import os

img_dir = 'D:/tmp/phone'

img_width_num = 6

img_height_num = 3

img_width_size = 150

img_height_size = 200

imgs = []

for name in os.listdir(img_dir)[:img_height_num * img_width_num]:

path = os.path.join(img_dir, name)

img = cv.imread(path)

img = cv.resize(img, (img_width_size, img_height_size))

# print(img.shape) # (200, 150, 3)

# 加上白边

mask = np.ones(img.shape[:2], dtype=np.bool)

mask[2:-2, 2:-2] = 0

img[mask] = 255

imgs.append(img)

img = np.concatenate(imgs, 0)

print(img.shape) # (3600, 150, 3)

img = img.reshape(img_height_num, img_width_num, img_height_size, img_width_size, 3)

print(img.shape, img[0, 0].shape) # (3, 6, 200, 150, 3) (200, 150, 3)

img = img.swapaxes(1, 2).reshape(img_height_size * img_height_num, img_width_size * img_width_num, 3)

print(img.shape) # (600, 900, 3)

cv.imshow('img', img)

cv.waitKey(0)

转载至链接:https://my.oschina.net/ahaoboy/blog/1932892

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值