130 lines
3.8 KiB
Python
130 lines
3.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
# @Time : 2022-7-26 0026 9:58
|
|
# @Author : Qing
|
|
# @Email : derighoid@gmail.com
|
|
# @File : Subtractive_Algorithm.py
|
|
# @Software: PyCharm
|
|
import os.path
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from Picture_Reading import *
|
|
|
|
|
|
class ReduceColor(pictureRead):
|
|
def __init__(self, filePath):
|
|
self.filePath = filePath
|
|
|
|
def readImg(self):
|
|
face4 = Image.open(self.filePath)
|
|
return face4
|
|
|
|
def imageMatrix(self):
|
|
im4 = np.array(self.readImg())
|
|
return im4
|
|
|
|
# 减色算法
|
|
def decrease_color(self):
|
|
'''
|
|
val=|32 (0<=var<64)
|
|
|96 (64<=var<128)
|
|
|160 (128<=var<192)
|
|
|224 (192<=var<256)
|
|
:return:
|
|
'''
|
|
img = self.imageMatrix().copy()
|
|
# img=img//64*64+32
|
|
H = img.shape[0] # 获取图片的高
|
|
W = img.shape[1] # 获取图片的宽
|
|
|
|
# 创建新的图
|
|
newImg = np.zeros((H, W, 3), np.uint8)
|
|
newImg2 = np.zeros((H, W, 3), np.uint8)
|
|
newImg3 = np.zeros((H, W, 3), np.uint8)
|
|
|
|
# 量化操作
|
|
for i in range(H):
|
|
for j in range(W):
|
|
for k in range(3): # RGB三分量
|
|
if img[i, j][k] < 128:
|
|
gray = 0
|
|
else:
|
|
gray = 128
|
|
newImg[i, j][k] = np.uint8(gray) # 传给新图片
|
|
|
|
for i in range(H):
|
|
for j in range(W):
|
|
for k in range(3):
|
|
if img[i, j][k] < 64:
|
|
gray = 0
|
|
elif img[i, j][k] < 128:
|
|
gray = 64
|
|
elif img[i, j][k] < 192:
|
|
gray = 128
|
|
else:
|
|
gray = 192
|
|
newImg2[i, j][k] = np.uint8(gray)
|
|
|
|
for i in range(H):
|
|
for j in range(W):
|
|
for k in range(3):
|
|
if newImg3[i, j][k] < 32:
|
|
gray = 0
|
|
elif newImg3[i, j][k] < 96:
|
|
gray = 32
|
|
elif newImg3[i, j][k] < 96:
|
|
gray = 96
|
|
elif newImg3[i, j][k] < 128:
|
|
gray = 96
|
|
elif newImg3[i,j][k]<160:
|
|
gray=128
|
|
elif newImg3[i,j][k]<192:
|
|
gray=160
|
|
elif newImg3[i,j][k]<244:
|
|
gray=192
|
|
else:
|
|
gray = 224
|
|
newImg3[i, j][k] = np.uint8(gray)
|
|
|
|
#定义画布
|
|
plt.figure(figsize=(16,8))
|
|
plt.subplot(131)
|
|
plt.imshow(img)
|
|
plt.title("origin")
|
|
|
|
plt.subplot(132)
|
|
plt.imshow(newImg)
|
|
plt.title("1")
|
|
|
|
plt.subplot(133)
|
|
plt.imshow(newImg2)
|
|
plt.title("2")
|
|
|
|
# plt.subplot(144)
|
|
# plt.imshow(newImg3)
|
|
# plt.title("3")
|
|
plt.show()
|
|
return newImg,newImg2,newImg3
|
|
|
|
# 图像二值化(以128为阈值进行二值化)
|
|
def imgBinarization(self):
|
|
a = self.imageGrey() - np.array([[128]])
|
|
b = np.floor(a / np.array([[256]]))
|
|
c = b + np.array([[1]], dtype=np.int16)
|
|
bfilter = c.astype("uint8")
|
|
result = bfilter * np.array([[255]], dtype=np.uint8)
|
|
# result=np.array(self.readImg().convert("1"))
|
|
plt.imshow(result)
|
|
plt.show()
|
|
return result
|
|
|
|
# 保存图片
|
|
def img_Save(self):
|
|
root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
|
|
if not os.path.exists(root):
|
|
os.mkdir(root)
|
|
else:
|
|
Image.fromarray(np.uint8(self.decrease_color)).save(root + 'picture8' + '.jpg')
|
|
Image.fromarray(np.uint8(self.imgBinarization())).save(root + 'picture9' + '.jpg')
|
|
print("Picture Save successfully")
|