Hexo

凡事预则立,不预则废


  • Home

  • Tags

  • Archives

  • Navigation

  • Search

Python——修改图片背景颜色

利用Python的skimage包实现修改图片背景色等


修改签名背景,按照灰度图像处理

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from skimage import io, color
import matplotlib.pyplot as plt


def change_grey_to_white(origin, new):
"""
Change background color for image, such as signature
:param origin: the path of original image
:param new: the path of new image
:return: None
"""
# img = io.imread('./origin.jpeg')
img = io.imread(origin)
io.imshow(img)
plt.show()
img_gray = color.rgb2gray(img)
rows, cols = img_gray.shape
for i in range(rows):
for j in range(cols):
if img_gray[i, j] <= 0.5:
img_gray[i, j] = 0
else:
img_gray[i, j] = 1
io.imshow(img_gray)
plt.show()
io.imsave(new, img_gray)

# example
change_grey_to_white('./origin.jpeg', 'new.jpeg')

说明

  • 这里可接受彩色图片,比如拍照片得到的原始图片
  • 函数会将其转换为灰度图片然后处理,最终输出也是灰度图片
  • 如果需要处理指定灰度,可通过修改判断语句中0.5这个值从而实现(比如修改为区间等)

修改证件照背景,按彩色图像处理

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from skimage import io
import numpy as np


def change_pixel_color(pixel, old_pixel, new_pixel=None, error=60):
"""
change color for pixel if pixel in range [old_pixel-error, old_pixel+error]
:param pixel:
:param old_pixel:
:param new_pixel:
:param error:
:return: the new pixel
"""
if new_pixel is None:
new_pixel = [255, 255, 255]
similar = True
for i in abs(pixel - old_pixel):
if i > error:
similar = False
break
if similar:
return new_pixel
else:
return pixel


def change_background(origin_path, new_path, new_color, error=60):
"""
change background, auto detect background
:param origin_path:
:param new_path:
:param new_color: target background color
:param error: the error for change color
:return: None
"""
img = io.imread(origin_path)
bg = img[10:30, 10:30, :]
bg_pixel = [1.0 * np.sum(bg[:, :, channel])/bg[:, :, channel].size for channel in range(3)]
print("background color: %s" % bg_pixel)
new_img = np.array([[change_pixel_color(pixel, [0, 160, 234], new_color, error) for pixel in row] for row in img])
io.imshow(new_img)
# import matplotlib.pyplot as plt
# plt.show()
io.imsave(new_path, new_img)


# example
change_background(origin_path="origin.jpeg",
new_path='new.jpeg',
new_color=[0, 255, 255])

说明

  • 自动读取图片的左上角部分像素的平均值作为背景颜色
  • 允许差范围在合适的范围内,可通过error参数调节,该参数不宜过大也不宜过小,可测试多次选择比较合适的

ML——缺失值处理

missing值处理


对于数值类型的特征

中位数填充

  • 用当前特征所有未缺失的值的中位数(median)填充当前特征的缺失值

均值填充

  • 用当前特征所有未缺失的值的均值填充当前特征的缺失值

加权填充

  • 引入相似性矩阵,评估缺失样本与其他未缺失样本的相似性,按照相似性分配权重
  • 效果更好,但是需要更多时间

对于Category类型

特殊字符填充

  • 使用某种未出现过的特殊字符填充
  • 等价于将缺失值看成是个特殊类别

填充众数

  • 寻找所有未缺失数据中样本最多的类别,然后将缺失值填充为该众数类别

什么情况下不用填充

树模型

普通树模型
  • ID3不支持缺失值处理(也可能可以,但是我没看到具体介绍)
  • C4.5和CART都使用下面的方法进行缺失值处理
  • 结点分裂时:
    • 参考自周志华机器学习书籍中
    • 先按照无缺失的数据正常划分(缺失数据不参与计算)
    • 对于无缺失值的样本,正常分配到对应叶子节点
    • 对于缺失值的样本,每个样本以不同的概率分配到各个叶子节点, 概率值为: 子节点中无缺失样本的数量 / 无缺失样本的总数
  • 问题: 如果是训练时没有缺失,预测时有缺失怎么办?
    • 一种可能的方式是直接放到某个结点中
    • [待更新]
GBDT
  • 如果使用的是树模型(CART)作为基分类器是否可以直接借用树模型对缺失值的处理方法?
  • 如果使用的是逻辑回归模型作为基分类器,需要自己对缺失值进行处理
XGBoost
  • 寻找分裂点时(split point):
    • 不遍历缺失值对应的样本,只使用无缺失的样本确定分裂点(节省开销)
    • 尝试将缺失值分配到左叶子结点或者右叶子结点,分别计算增益(保证完备性)
    • 选择增益大的点即可
  • 如果训练时没有缺失值,预测时有缺失值:
    • 将缺失值自动放到右子树中即可

不同模型对缺失值的敏感度总结

不敏感模型

  • 树模型
  • 神经网络的鲁棒性强, 数据量够的话不敏感?
    • 神经网络的输入必须是没有缺失值的,需要我们使用特征工程的方法填充缺失值
    • 其实我觉得自己需要填充数据的其实都有点敏感吧, 所以神经网络在使用时有时候感觉并不敏感,因为缺失值被我们填充后总能得到不错的效果

敏感模型

  • 距离度量模型: KNN, SVM等
    • 在尽量保证缺失值是随机的前提下使用基于统计分布的填充方法可能降低缺失值造成的负面影响
    • 但是SVM这样的模型对缺失值的抗性非常差,不恰当的非随机缺失值可能导致模型出现意外
  • 线性模型的损失函数往往也涉及到距离的计算?
1…260261262…352
San Ye

San Ye

Stay Hungry. Stay Foolish.

704 posts
53 tags
© 2026 San Ye
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4