使用rembg对图像进行抠图后再进行二值化处理
This commit is contained in:
commit
ba6218b437
45
binarization.py
Normal file
45
binarization.py
Normal file
@ -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()
|
BIN
buckle/zmn.png
Normal file
BIN
buckle/zmn.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 635 KiB |
BIN
file/zmn.jpg
Normal file
BIN
file/zmn.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 452 KiB |
BIN
result/result_zmn.png
Normal file
BIN
result/result_zmn.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
Loading…
x
Reference in New Issue
Block a user