Initial commit

This commit is contained in:
Le Ma
2024-12-03 11:34:51 +13:00
commit b44d0668f7
1381 changed files with 3384 additions and 0 deletions

118
split16.py Normal file
View File

@@ -0,0 +1,118 @@
import cv2
import numpy as np
import os
# 读取输入图像
image = cv2.imread('input.jpg')
# 检查图像是否读取成功
if image is None:
print("无法读取 'input.jpg',请确保文件存在且路径正确。")
exit()
# 获取图像的尺寸
height, width = image.shape[:2]
# 定义小图片的数量(例如,将图像分割成 3 x 3 个小正方形)
num_rows = 3
num_cols = 3
# 计算每个小正方形的尺寸
cell_height = height // num_rows
cell_width = width // num_cols
# 定义曲线的参数
curve_amplitude = min(cell_height, cell_width) / 6 # 曲线的幅度,可根据需要调整
curve_length_ratio = 1 / 3 # 曲线占分割线长度的中间三分之一
# 创建输出目录
output_dir = 'output'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 创建掩码列表,每个小图片对应一个掩码
masks = []
# 初始化掩码
for _ in range(num_rows * num_cols):
mask = np.zeros((height, width), dtype=np.uint8)
masks.append(mask)
# 生成曲线偏移量的函数
def generate_curve(length, amplitude):
x = np.linspace(0, length - 1, num=length)
y = amplitude * np.sin(np.pi * (x / (length - 1)))
return y.astype(int)
# 设置每个小图片的区域为白色,并创建曲线分割线
for row in range(num_rows):
for col in range(num_cols):
index = row * num_cols + col
mask = masks[index]
# 定义当前小图片的边界
y_start = row * cell_height
y_end = y_start + cell_height if row != num_rows - 1 else height
x_start = col * cell_width
x_end = x_start + cell_width if col != num_cols - 1 else width
# 在掩码上设置小图片区域为白色
mask[y_start:y_end, x_start:x_end] = 255
# 创建垂直曲线分割线(列之间)
for col in range(1, num_cols):
x_line = col * cell_width
# 曲线应用于垂直分割线的中间三分之一
curve_y_start = int(height * (1 - curve_length_ratio) / 2)
curve_y_end = int(height * (1 + curve_length_ratio) / 2)
curve_length = curve_y_end - curve_y_start
# 生成曲线偏移量
y_offsets = generate_curve(curve_length, curve_amplitude)
for i, y_offset in enumerate(y_offsets):
y_pos = curve_y_start + i
# 左侧小图片的掩码
left_index = (y_pos // cell_height) * num_cols + col - 1
if 0 <= left_index < len(masks):
masks[left_index][y_pos, x_line - y_offset:] = 0 # 将右侧区域设为黑色
# 右侧小图片的掩码
right_index = (y_pos // cell_height) * num_cols + col
if 0 <= right_index < len(masks):
masks[right_index][y_pos, :x_line + y_offset] = 0 # 将左侧区域设为黑色
# 创建水平曲线分割线(行之间)
for row in range(1, num_rows):
y_line = row * cell_height
# 曲线应用于水平分割线的中间三分之一
curve_x_start = int(width * (1 - curve_length_ratio) / 2)
curve_x_end = int(width * (1 + curve_length_ratio) / 2)
curve_length = curve_x_end - curve_x_start
# 生成曲线偏移量
x_offsets = generate_curve(curve_length, curve_amplitude)
for i, x_offset in enumerate(x_offsets):
x_pos = curve_x_start + i
# 上方小图片的掩码
top_index = (row - 1) * num_cols + (x_pos // cell_width)
if 0 <= top_index < len(masks):
masks[top_index][y_line - x_offset:, x_pos] = 0 # 将下方区域设为黑色
# 下方小图片的掩码
bottom_index = row * num_cols + (x_pos // cell_width)
if 0 <= bottom_index < len(masks):
masks[bottom_index][:y_line + x_offset, x_pos] = 0 # 将上方区域设为黑色
# 应用掩码并保存小图片
for index, mask in enumerate(masks):
img_part = cv2.bitwise_and(image, image, mask=mask)
output_path = os.path.join(output_dir, f'output{index + 1}.png')
cv2.imwrite(output_path, img_part)
print(f"图片已成功切割成 {num_rows * num_cols} 张,并保存到 'output' 目录下。")