44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import cv2
|
|
import numpy as np
|
|
import os
|
|
|
|
# 读取输入图像
|
|
image = cv2.imread('input.jpg')
|
|
|
|
# 获取图像的尺寸
|
|
height, width = image.shape[:2]
|
|
|
|
# 创建一个全黑的掩码
|
|
mask = np.zeros((height, width), dtype=np.uint8)
|
|
|
|
# 定义曲线(例如,使用正弦函数)
|
|
x = np.arange(width)
|
|
# 调整曲线的频率和幅度
|
|
frequency = 2 * np.pi / width
|
|
amplitude = height / 4
|
|
y = (height / 2) + amplitude * np.sin(frequency * x)
|
|
|
|
# 绘制曲线以上的区域为白色
|
|
for i in range(width):
|
|
cv2.line(mask, (i, 0), (i, int(y[i])), 255)
|
|
|
|
# 创建反掩码
|
|
mask_inv = cv2.bitwise_not(mask)
|
|
|
|
# 应用掩码得到第一张图片
|
|
img1 = cv2.bitwise_and(image, image, mask=mask)
|
|
|
|
# 应用反掩码得到第二张图片
|
|
img2 = cv2.bitwise_and(image, image, mask=mask_inv)
|
|
|
|
# 确保输出目录存在
|
|
output_dir = 'output'
|
|
if not os.path.exists(output_dir):
|
|
os.makedirs(output_dir)
|
|
|
|
# 保存结果图片
|
|
cv2.imwrite(os.path.join(output_dir, 'output1.jpg'), img1)
|
|
cv2.imwrite(os.path.join(output_dir, 'output2.jpg'), img2)
|
|
|
|
print("图片已成功切割并保存到 'output' 目录下。")
|