commit ba6218b4372aae1dedd6fd833bcb830cd2247466 Author: Cx330 <1487537121@qq.com> Date: Sun May 11 21:45:52 2025 +0800 使用rembg对图像进行抠图后再进行二值化处理 diff --git a/binarization.py b/binarization.py new file mode 100644 index 0000000..4e8ff43 --- /dev/null +++ b/binarization.py @@ -0,0 +1,45 @@ +import cv2 +from PIL import Image +import matplotlib.pyplot as plt +plt.rcParams['font.sans-serif'] = ['simHei'] +plt.rcParams['axes.unicode_minus'] = False +from rembg import remove + +image_zmn = Image.open("file/zmn.jpg") + +# 对图像主题进行抠图处理 +if __name__=='__main__': + + # 待处理的图片路径 + input_path = 'file/zmn.jpg' + # 处理后存储的图片路径 + output_path = 'buckle/zmn.png' + + with open(input_path, 'rb') as i: + with open(output_path, 'wb') as o: + input = i.read() + output = remove(input) + o.write(output) + +# 读取抠图后照片 +image = cv2.imread("buckle/zmn.png", cv2.IMREAD_GRAYSCALE) + +# 对图像进行二值化处理 +_, binary_image = cv2.threshold(image, 1, 255, cv2.THRESH_BINARY) + +# 保存结果照片 +cv2.imwrite("result/result_zmn.png", binary_image) + +# 展示图像 +plt.subplot(1,2,1) +plt.imshow(image_zmn) +plt.title('原图') +plt.axis('off') + +plt.subplot(1,2,2) +plt.imshow(binary_image, cmap='gray') +plt.title('二值化处理') +plt.axis('off') + +plt.tight_layout() +plt.show() \ No newline at end of file diff --git a/buckle/zmn.png b/buckle/zmn.png new file mode 100644 index 0000000..ac27bff Binary files /dev/null and b/buckle/zmn.png differ diff --git a/file/zmn.jpg b/file/zmn.jpg new file mode 100644 index 0000000..582f950 Binary files /dev/null and b/file/zmn.jpg differ diff --git a/result/result_zmn.png b/result/result_zmn.png new file mode 100644 index 0000000..ddc915a Binary files /dev/null and b/result/result_zmn.png differ