python 图像重叠分块恢复

图像重叠分块请阅读这篇文章:

https://blog.csdn.net/coolsunxu/article/details/102639674


import cv2
import math 
import numpy as np
import functools
 
from pathlib import Path

def custom_sort(x,y):
	x = x.split(tag)[-1].split('.')[0].split('_')
	x = [int(i) for i in x]
	y = y.split(tag)[-1].split('.')[0].split('_')
	y = [int(i) for i in y]
	for i in range(len(x)):
		if(x[i]>y[i]):
			return 1
		elif(x[i]<y[i]):
			return -1;
		elif(i==len(x)-1):
			return 0
	
tag = '\\' # 路径分隔符

data_root = Path('./sunxu')
temp_path = '2.jpg'

height = 208
width = 208
 
# overlap 
over_x = 50
over_y = 50
h_val = height - over_x
w_val = width - over_y

images_path = data_root.glob('*')
images_path = [str(path) for path in images_path]
images_path = sorted(images_path,key=functools.cmp_to_key(custom_sort))
#print(images_path)

s = set()
for path in images_path:
	path = path.split(tag)[-1].split('_')
	s.add(int(path[0]))
#print(s)

output = []
for i in range(len(s)):
	output.append([])

for path in images_path:
	image = cv2.imread(path)
	path = path.split(tag)[-1].split('.')[0].split('_')
	if(int(path[3])==width): 
		if(int(path[1])==0):
			output[int(path[0])].append(image[:,:,:])
		else:
			output[int(path[0])].append(image[:,over_y:,:])	
	else:
		output[int(path[0])].append(image[:,over_y:,:])

temp = []
for i in range(len(output)):
	t = np.concatenate(output[i],1)
	if(i==0):
		temp.append(t[:,:,:])
	else:
		temp.append(t[over_x:,:,:])


temp = np.concatenate(temp,0)

cv2.imwrite(temp_path,temp)



 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值