pythonpictureanalysis/Subtractive_Algorithm.py

130 lines
3.8 KiB
Python
Raw Permalink Normal View History

2022-07-26 21:27:11 +08:00
# -*- 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
2022-07-27 20:41:38 +08:00
2022-07-26 21:27:11 +08:00
from Picture_Reading import *
2022-07-27 20:41:38 +08:00
2022-07-26 21:27:11 +08:00
class ReduceColor(pictureRead):
2022-07-27 20:41:38 +08:00
def __init__(self, filePath):
self.filePath = filePath
2022-07-26 21:27:11 +08:00
def readImg(self):
2022-07-27 20:41:38 +08:00
face4 = Image.open(self.filePath)
2022-07-26 21:27:11 +08:00
return face4
def imageMatrix(self):
im4 = np.array(self.readImg())
return im4
# 减色算法
def decrease_color(self):
2022-07-27 20:41:38 +08:00
'''
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)
2022-07-26 21:27:11 +08:00
2022-07-27 20:41:38 +08:00
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)
2022-07-26 21:27:11 +08:00
2022-07-27 20:41:38 +08:00
#定义画布
plt.figure(figsize=(16,8))
plt.subplot(141)
plt.imshow(img)
plt.title("origin")
plt.subplot(142)
plt.imshow(newImg)
plt.title("1")
plt.subplot(143)
plt.imshow(newImg2)
plt.title("2")
plt.subplot(144)
plt.imshow(newImg3)
plt.title("3")
plt.show()
return newImg,newImg2,newImg3
# 图像二值化(以128为阈值进行二值化)
2022-07-26 21:27:11 +08:00
def imgBinarization(self):
2022-07-27 20:41:38 +08:00
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)
2022-07-26 21:27:11 +08:00
# result=np.array(self.readImg().convert("1"))
plt.imshow(result)
plt.show()
return result
2022-07-27 20:41:38 +08:00
# 保存图片
2022-07-26 21:27:11 +08:00
def img_Save(self):
root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
if not os.path.exists(root):
os.mkdir(root)
else:
2022-07-27 20:41:38 +08:00
Image.fromarray(np.uint8(self.decrease_color)).save(root + 'picture8' + '.jpg')
Image.fromarray(np.uint8(self.imgBinarization())).save(root + 'picture9' + '.jpg')
2022-07-26 21:27:11 +08:00
print("Picture Save successfully")