Initial commit
This commit is contained in:
128
split14.py
Normal file
128
split14.py
Normal file
@@ -0,0 +1,128 @@
|
||||
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]
|
||||
|
||||
# 创建九个掩码,初始化为全黑
|
||||
masks = [np.zeros((height, width), dtype=np.uint8) for _ in range(9)]
|
||||
|
||||
# 定义曲线参数,增加弯曲幅度
|
||||
amplitude_h = height / 15 # 横向曲线的最大幅度(修改为height / 15,增加弯曲)
|
||||
amplitude_v = width / 15 # 纵向曲线的最大幅度(修改为width / 15,增加弯曲)
|
||||
|
||||
# 定义 x 和 y 坐标
|
||||
x = np.arange(width)
|
||||
y = np.arange(height)
|
||||
|
||||
# 定义横向曲线(沿着 x 轴变化),在每个区域的中间进行一次弯曲
|
||||
y_h1 = np.zeros_like(x, dtype=float)
|
||||
y_h2 = np.zeros_like(x, dtype=float)
|
||||
|
||||
# 定义纵向曲线(沿着 y 轴变化),在每个区域的中间进行一次弯曲
|
||||
x_v1 = np.zeros_like(y, dtype=float)
|
||||
x_v2 = np.zeros_like(y, dtype=float)
|
||||
|
||||
# 定义横向曲线的三个段
|
||||
segment_width = width / 3
|
||||
|
||||
for i in range(3):
|
||||
# 定义每个段的起始和结束位置
|
||||
start_x = i * segment_width
|
||||
end_x = (i + 1) * segment_width
|
||||
center_x = (start_x + end_x) / 2
|
||||
|
||||
# 创建掩码,确定在哪个段上
|
||||
mask = (x >= start_x) & (x < end_x)
|
||||
|
||||
# 计算在该段上的相对位置
|
||||
x_relative = x[mask] - center_x
|
||||
|
||||
# 计算幅度调节函数,使得在段的边界处为零,在中心为一
|
||||
amplitude_modifier = np.cos(np.pi * x_relative / (end_x - start_x)) ** 2
|
||||
|
||||
# 计算曲线 y 值
|
||||
y_h1[mask] = (height / 3) + amplitude_h * amplitude_modifier * np.sin(np.pi * x_relative / (end_x - start_x))
|
||||
y_h2[mask] = (2 * height / 3) + amplitude_h * amplitude_modifier * np.sin(np.pi * x_relative / (end_x - start_x))
|
||||
|
||||
# 确保 y 值在图像范围内
|
||||
y_h1 = np.clip(y_h1, 0, height - 1).astype(int)
|
||||
y_h2 = np.clip(y_h2, 0, height - 1).astype(int)
|
||||
|
||||
# 定义纵向曲线的三个段
|
||||
segment_height = height / 3
|
||||
|
||||
for i in range(3):
|
||||
# 定义每个段的起始和结束位置
|
||||
start_y = i * segment_height
|
||||
end_y = (i + 1) * segment_height
|
||||
center_y = (start_y + end_y) / 2
|
||||
|
||||
# 创建掩码,确定在哪个段上
|
||||
mask = (y >= start_y) & (y < end_y)
|
||||
|
||||
# 计算在该段上的相对位置
|
||||
y_relative = y[mask] - center_y
|
||||
|
||||
# 计算幅度调节函数,使得在段的边界处为零,在中心为一
|
||||
amplitude_modifier = np.cos(np.pi * y_relative / (end_y - start_y)) ** 2
|
||||
|
||||
# 计算曲线 x 值
|
||||
x_v1[mask] = (width / 3) + amplitude_v * amplitude_modifier * np.sin(np.pi * y_relative / (end_y - start_y))
|
||||
x_v2[mask] = (2 * width / 3) + amplitude_v * amplitude_modifier * np.sin(np.pi * y_relative / (end_y - start_y))
|
||||
|
||||
# 确保 x 值在图像范围内
|
||||
x_v1 = np.clip(x_v1, 0, width - 1).astype(int)
|
||||
x_v2 = np.clip(x_v2, 0, width - 1).astype(int)
|
||||
|
||||
# 创建掩码
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
y_curve1 = y_h1[j]
|
||||
y_curve2 = y_h2[j]
|
||||
x_curve1 = x_v1[i]
|
||||
x_curve2 = x_v2[i]
|
||||
|
||||
# 判断所在区域
|
||||
if i <= y_curve1:
|
||||
if j <= x_curve1:
|
||||
masks[0][i, j] = 255 # 左上角
|
||||
elif j <= x_curve2:
|
||||
masks[1][i, j] = 255 # 上中
|
||||
else:
|
||||
masks[2][i, j] = 255 # 右上角
|
||||
elif i <= y_curve2:
|
||||
if j <= x_curve1:
|
||||
masks[3][i, j] = 255 # 中左
|
||||
elif j <= x_curve2:
|
||||
masks[4][i, j] = 255 # 中间
|
||||
else:
|
||||
masks[5][i, j] = 255 # 中右
|
||||
else:
|
||||
if j <= x_curve1:
|
||||
masks[6][i, j] = 255 # 左下角
|
||||
elif j <= x_curve2:
|
||||
masks[7][i, j] = 255 # 下中
|
||||
else:
|
||||
masks[8][i, j] = 255 # 右下角
|
||||
|
||||
# 确保输出目录存在
|
||||
output_dir = 'output'
|
||||
if not os.path.exists(output_dir):
|
||||
os.makedirs(output_dir)
|
||||
|
||||
# 应用掩码并保存图片
|
||||
for idx, mask in enumerate(masks):
|
||||
img_part = cv2.bitwise_and(image, image, mask=mask)
|
||||
cv2.imwrite(os.path.join(output_dir, f'output{idx+1}.png'), img_part)
|
||||
|
||||
print("图片已成功切割成9张,并保存到 'output' 目录下。")
|
||||
Reference in New Issue
Block a user