Python 图像处理(python 图像处理人脸融合保证肤色)
off999 2024-10-20 08:05 55 浏览 0 评论
以前照相从来没有那么容易。现在你只需要一部手机。拍照是免费的,如果我们不考虑手机的费用的话。就在上一代人之前,业余艺术家和真正的艺术家如果拍照非常昂贵,并且每张照片的成本也不是免费的。
我们拍照是为了及时保存伟大的时刻,被保存的记忆随时准备在未来被"打开"。
就像腌制东西一样,我们要注意正确的防腐剂。当然,手机也为我们提供了一系列的图像处理软件,但是一旦我们需要处理大量的照片,我们就需要其他的工具。这时,编程和Python就派上用场了。Python及其模块如Numpy、Scipy、Matplotlib和其他特殊模块提供了各种各样的函数,能够处理大量图片。
为了向你提供必要的知识,本章的Python教程将处理基本的图像处理和操作。为此,我们使用模块NumPy、Matplotlib和SciPy。
我们从scipy包misc开始。
# 以下行仅在Python notebook中需要:
%matplotlib inline
from scipy import misc
ascent = misc.ascent()
import matplotlib.pyplot as plt
plt.gray()
plt.imshow(ascent)
plt.show()
除了图像之外,我们还可以看到带有刻度的轴。这可能是非常有趣的,如果你需要一些关于大小和像素位置的方向,但在大多数情况下,你想看到没有这些信息的图像。我们可以通过添加命令plt.axis("off")来去掉刻度和轴:
from scipy import misc
ascent = misc.ascent()
import matplotlib.pyplot as plt
plt.axis("off") # 删除轴和刻度
plt.gray()
plt.imshow(ascent)
plt.show()
我们可以看到这个图像的类型是一个整数数组:
ascent.dtype
输出:
dtype('int64')
我们也可以检查图像的大小:
ascent.shape
输出:
(512,512)
misc包里还有一张浣熊的图片:
import scipy.misc
face = scipy.misc.face()
print(face.shape)
print(face.max)
print(face.dtype)
plt.axis("off")
plt.gray()
plt.imshow(face)
plt.show()
(768, 1024, 3)
<built-in method max of numpy.ndarray object at 0x7f9e70102800>
uint8
import matplotlib.pyplot as plt
matplotlib只支持png图像
img = plt.imread('frankfurt.png')
print(img[:3])
[[[ 0.41176471 0.56862748 0.80000001]
[ 0.40392157 0.56078434 0.79215688]
[ 0.40392157 0.56862748 0.79607844]
...,
[ 0.48235294 0.62352943 0.81960785]
[ 0.47843137 0.627451 0.81960785]
[ 0.47843137 0.62352943 0.82745099]]
[[ 0.40784314 0.56470591 0.79607844]
[ 0.40392157 0.56078434 0.79215688]
[ 0.40392157 0.56862748 0.79607844]
...,
[ 0.48235294 0.62352943 0.81960785]
[ 0.47843137 0.627451 0.81960785]
[ 0.48235294 0.627451 0.83137256]]
[[ 0.40392157 0.56862748 0.79607844]
[ 0.40392157 0.56862748 0.79607844]
[ 0.40392157 0.56862748 0.79607844]
...,
[ 0.48235294 0.62352943 0.81960785]
[ 0.48235294 0.62352943 0.81960785]
[ 0.48627451 0.627451 0.83137256]]]
plt.axis("off")
imgplot = plt.imshow(img)
lum_img = img[:,:,1]
print(lum_img)
[[ 0.56862748 0.56078434 0.56862748 ..., 0.62352943 0.627451
0.62352943]
[ 0.56470591 0.56078434 0.56862748 ..., 0.62352943 0.627451 0.627451 ]
[ 0.56862748 0.56862748 0.56862748 ..., 0.62352943 0.62352943
0.627451 ]
...,
[ 0.31764707 0.32941177 0.32941177 ..., 0.30588236 0.3137255
0.31764707]
[ 0.31764707 0.3137255 0.32941177 ..., 0.3019608 0.32156864
0.33725491]
[ 0.31764707 0.3019608 0.33333334 ..., 0.30588236 0.32156864
0.33333334]]
plt.axis("off")
imgplot = plt.imshow(lum_img)
色彩、色度和色调
现在,我们将展示如何给图像着色。色彩是色彩理论的一种表达,是画家常用的一种技法。想到画家而不想到荷兰是很难想象的。所以在下一个例子中,我们使用荷兰风车的图片。
windmills = plt.imread('windmills.png')
plt.axis("off")
plt.imshow(windmills)
输出:
<matplotlib.image.AxesImage at 0x7f9e77f02f98>
我们现在想给图像着色。我们用白色,这将增加图像的亮度。为此,我们编写了一个Python函数,它接受一个图像和一个百分比值作为参数。设置"百分比"为0不会改变图像,设置为1表示图像将完全变白:
import numpy as np
import matplotlib.pyplot as plt
def tint(imag, percent):
"""
imag: 图像
percent: 0,图像将保持不变,1,图像将完全变白色,值应该在0~1
"""
tinted_imag = imag + (np.ones(imag.shape) - imag) * percent
return tinted_imag
windmills = plt.imread('windmills.png')
tinted_windmills = tint(windmills, 0.8)
plt.axis("off")
plt.imshow(tinted_windmills)
输出:
<matplotlib.image.AxesImage at 0x7f9e6cd99978>
阴影是一种颜色与黑色的混合,它减少了亮度。
import numpy as np
import matplotlib.pyplot as plt
def shade(imag, percent):
"""
imag: 图像
percent: 0,图像将保持不变,1,图像将完全变黑,值应该在0~1
"""
tinted_imag = imag * (1 - percent)
return tinted_imag
windmills = plt.imread('windmills.png')
tinted_windmills = shade(windmills, 0.7)
plt.imshow(tinted_windmills)
输出:
<matplotlib.image.AxesImage at 0x7f9e6cd20048>
def vertical_gradient_line(image, reverse=False):
"""
我们创建一个垂直梯度线。形状 (1, image.shape[1], 3))
如果reverse为False,则值从0增加到1,
否则,值将从1递减到0。
"""
number_of_columns = image.shape[1]
if reverse:
C = np.linspace(1, 0, number_of_columns)
else:
C = np.linspace(0, 1, number_of_columns)
C = np.dstack((C, C, C))
return C
horizontal_brush = vertical_gradient_line(windmills)
tinted_windmills = windmills * horizontal_brush
plt.axis("off")
plt.imshow(tinted_windmills)
输出:
<matplotlib.image.AxesImage at 0x7f9e6ccb3d68>
现在,我们将通过将Python函数的reverse参数设置为“True”来从右向左着色图像:
def vertical_gradient_line(image, reverse=False):
"""
我们创建一个水平梯度线。形状 (1, image.shape[1], 3))
如果reverse为False,则值从0增加到1,
否则,值将从1递减到0。
"""
number_of_columns = image.shape[1]
if reverse:
C = np.linspace(1, 0, number_of_columns)
else:
C = np.linspace(0, 1, number_of_columns)
C = np.dstack((C, C, C))
return C
horizontal_brush = vertical_gradient_line(windmills, reverse=True)
tinted_windmills = windmills * horizontal_brush
plt.axis("off")
plt.imshow(tinted_windmills)
输出:
<matplotlib.image.AxesImage at 0x7f9e6cbc82b0>
def horizontal_gradient_line(image, reverse=False):
"""
我们创建一个垂直梯度线。形状(image.shape[0], 1, 3))
如果reverse为False,则值从0增加到1,
否则,值将从1递减到0。
"""
number_of_rows, number_of_columns = image.shape[:2]
C = np.linspace(1, 0, number_of_rows)
C = C[np.newaxis,:]
C = np.concatenate((C, C, C)).transpose()
C = C[:, np.newaxis]
return C
vertical_brush = horizontal_gradient_line(windmills)
tinted_windmills = windmills
plt.imshow(tinted_windmills)
输出:
<matplotlib.image.AxesImage at 0x7f9e6cb52390>
色调是由一种颜色与灰色的混合产生的,或由着色和阴影产生的。
charlie = plt.imread('Chaplin.png')
plt.gray()
print(charlie)
plt.imshow(charlie)
[[ 0.16470589 0.16862746 0.17647059 ..., 0. 0. 0. ]
[ 0.16078432 0.16078432 0.16470589 ..., 0. 0. 0. ]
[ 0.15686275 0.15686275 0.16078432 ..., 0. 0. 0. ]
...,
[ 0. 0. 0. ..., 0. 0. 0. ]
[ 0. 0. 0. ..., 0. 0. 0. ]
[ 0. 0. 0. ..., 0. 0. 0. ]]
输出:
<matplotlib.image.AxesImage at 0x7f9e70047668>
给灰度图像着色:http://scikit-image.org/docs/dev/auto_examples/plot_tinting_grayscale_images.html
在下面的示例中,我们将使用不同的颜色映射。颜色映射可以在matplotlib.pyplot.cm.datad中找到:
plt.cm.datad.keys()
输出:
dict_keys(['afmhot', 'autumn', 'bone', 'binary', 'bwr', 'brg', 'CMRmap', 'cool', 'copper', 'cubehelix', 'flag', 'gnuplot', 'gnuplot2', 'gray', 'hot', 'hsv', 'jet', 'ocean', 'pink', 'prism', 'rainbow', 'seismic', 'spring', 'summer', 'terrain', 'winter', 'nipy_spectral', 'spectral', 'Blues', 'BrBG', 'BuGn', 'BuPu', 'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd', 'PiYG', 'PRGn', 'PuBu', 'PuBuGn', 'PuOr', 'PuRd', 'Purples', 'RdBu', 'RdGy', 'RdPu', 'RdYlBu', 'RdYlGn', 'Reds', 'Spectral', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd', 'gist_earth', 'gist_gray', 'gist_heat', 'gist_ncar', 'gist_rainbow', 'gist_stern', 'gist_yarg', 'coolwarm', 'Wistia', 'Accent', 'Dark2', 'Paired', 'Pastel1', 'Pastel2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c', 'Vega10', 'Vega20', 'Vega20b', 'Vega20c', 'afmhot_r', 'autumn_r', 'bone_r', 'binary_r', 'bwr_r', 'brg_r', 'CMRmap_r', 'cool_r', 'copper_r', 'cubehelix_r', 'flag_r', 'gnuplot_r', 'gnuplot2_r', 'gray_r', 'hot_r', 'hsv_r', 'jet_r', 'ocean_r', 'pink_r', 'prism_r', 'rainbow_r', 'seismic_r', 'spring_r', 'summer_r', 'terrain_r', 'winter_r', 'nipy_spectral_r', 'spectral_r', 'Blues_r', 'BrBG_r', 'BuGn_r', 'BuPu_r', 'GnBu_r', 'Greens_r', 'Greys_r', 'Oranges_r', 'OrRd_r', 'PiYG_r', 'PRGn_r', 'PuBu_r', 'PuBuGn_r', 'PuOr_r', 'PuRd_r', 'Purples_r', 'RdBu_r', 'RdGy_r', 'RdPu_r', 'RdYlBu_r', 'RdYlGn_r', 'Reds_r', 'Spectral_r', 'YlGn_r', 'YlGnBu_r', 'YlOrBr_r', 'YlOrRd_r', 'gist_earth_r', 'gist_gray_r', 'gist_heat_r', 'gist_ncar_r', 'gist_rainbow_r', 'gist_stern_r', 'gist_yarg_r', 'coolwarm_r', 'Wistia_r', 'Accent_r', 'Dark2_r', 'Paired_r', 'Pastel1_r', 'Pastel2_r', 'Set1_r', 'Set2_r', 'Set3_r', 'tab10_r', 'tab20_r', 'tab20b_r', 'tab20c_r', 'Vega10_r', 'Vega20_r', 'Vega20b_r', 'Vega20c_r'])
import numpy as np
import matplotlib.pyplot as plt
charlie = plt.imread('Chaplin.png')
# colormaps plt.cm.datad
# cmaps = set(plt.cm.datad.keys())
cmaps = {'afmhot', 'autumn', 'bone', 'binary', 'bwr', 'brg',
'CMRmap', 'cool', 'copper', 'cubehelix', 'Greens'}
X = [ (4,3,1, (1, 0, 0)), (4,3,2, (0.5, 0.5, 0)), (4,3,3, (0, 1, 0)),
(4,3,4, (0, 0.5, 0.5)), (4,3,(5,8), (0, 0, 1)), (4,3,6, (1, 1, 0)),
(4,3,7, (0.5, 1, 0) ), (4,3,9, (0, 0.5, 0.5)),
(4,3,10, (0, 0.5, 1)), (4,3,11, (0, 1, 1)), (4,3,12, (0.5, 1, 1))]
fig = plt.figure(figsize=(6, 5))
#fig.subplots_adjust(bottom=0, left=0, top = 0.975, right=1)
for nrows, ncols, plot_number, factor in X:
sub = fig.add_subplot(nrows, ncols, plot_number)
sub.set_xticks([])
plt.colors()
sub.imshow(charlie*0.0002, cmap=cmaps.pop())
sub.set_yticks([])
#fig.show()
相关推荐
- ipv6无网络访问权限怎么解决
-
ipv6无网络访问权限解决方法如下1、点击电脑左下角的开始,进入到开始的菜单栏,在菜单栏中找到“运行”。或者通过快捷键Windows+R打开运行窗口。 2、打开运行的窗口页面后,在页面上输入“CMD...
- office ltsc版(Office LTSC版本区别)
-
office2021和2021ltsc的区别如下:1.更新策略不同。前者采用每个月月度更新的方法,提供功能更新、安全更新。后者不采用每个月月度更新的方法,且不提供功能更新。2.界面不同。2021采用了...
- 安装win7需要激活吗(现在安装win7旗舰版还需密钥吗)
-
要激活 Windows7如果是预装在计算机中的,买来之后便不用激活,这里预装指的是在厂商那里。正版的Windows7安装到计算机中,有三十天的试用期,若要永久使用,就要使...
- originos 3升级计划公布(originos升级包)
-
2023年2月。1.OriginOS3.0系统第一批升级时间为11月25日。2、包含iQOONeo7,X80系列,S15系列,iQOO9、iQOO10系列,以及折叠屏XFold系列和大屏XNo...
- 鸿蒙系统适配第三方机型(鸿蒙 第三方适配)
-
最新华为官方公布了鸿蒙系统3.0支持的机型名单,具体如下。鸿蒙系统3.0升级名单:1.Mate系列:MateXs2、MateX2、MateXs、Mate40、Mate40Pro、Mate...
- imei怎么下载(imei changer apk)
-
如果您的steam序列号激活了,可以尝试以下方法下载:1.使用steam自带的下载工具,如“下载工具”,在软件的“下载”选项卡中选择“序列号下载”。2.在下载页面中,选择要下载的游戏,然后点击“下...
- 电脑系统优化软件哪个好(系统优化软件排行榜)
-
有必要用,非常好用,WINDOWS优化大师是一个网络上下载率极高的系统维护软件。多年未曾清理过系统和硬盘的电脑,系统内部将产生大量的垃圾文件、临时文件、废旧程序等等win10系统不需要经常更新,关闭...
- 重装系统后硬盘不见了(重装系统后磁盘不见了)
-
硬盘不见可能是因为重装系统时未正确安装驱动程序或未对硬件进行正确设置。你可以按以下步骤排查问题:进入BIOS检查硬盘是否被识别,尝试重新连接数据线和电源线,更新或安装适当的硬件驱动程序,或者使用硬件故...
- 冰封u盘装win7系统教程图解(冰封u盘启动装机教程)
-
1.查找激活工具:通常来说,Win7冰封系统已经包含了必要的驱动,所以如果你的电脑上并没有出现设备错误,那你就可以正常使用。如果你需要添加任何驱动,请尝试从厂商下载相应的驱动并执行自动安装程序。如果...
- uefi模式下找不到硬盘(uefi引导找不到硬盘)
-
首先你的安装盘必须是从UEFI启动的,然后它才能安装为UEFI启动。(条件:Fat32文件系统,efi文件夹)其次你MBR+BIOS的系统想换成GPT+EFI的,分区得做一点改动,腾出来100M的空...
- win7怎么安装蓝牙驱动程序(win7电脑安装蓝牙驱动教程)
-
方法如下: 1、再开始里点击控制版面,点击【硬件和声音】找到【添加设备】 2、之后再选择你要添加的蓝牙耳机。 3、系统就会提示正在与蓝牙适配器连接,然后提示添加成功。 4、点击“开始”-“...
- 怎么装系统win7旗舰版(电脑怎么装win7旗舰版)
-
1、目前支持64位的Wincc版本有:WinccV7Sp3、WinccV11Sp2、WinccV12。2、Wincc的V11与V12两个版本不能共存,即不能同时安装在同一台电脑上。上述这两...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
慕ke 前端工程师2024「完整」
-
失业程序员复习python笔记——条件与循环
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
