铅笔素描算法

by Rishav Agarwal

通过里沙夫·阿加瓦尔

如何用10行代码将任何图像变成铅笔素描 (How to turn any image into a pencil sketch with 10 lines of code)

使用基本的计算机视觉和Python的Numpy库 (Use basic computer vision and Python’s Numpy library)

I have always been fascinated by computer vision, and especially by its power to manipulate images.

我一直着迷于计算机视觉,尤其是其操纵图像的能力。

An image is basically an array of numbers to Python. So we can perform a variety of matrix manipulations to get some very interesting results. In this post, I talk about how to reduce an image into a ‘pencil’ outline.

图像基本上是Python的数字数组。 因此,我们可以执行各种矩阵操作来获得一些非常有趣的结果。 在本文中,我将讨论如何将图像缩小为“铅笔”轮廓。

步骤 (The Steps)

The process is pretty simple:

这个过程非常简单:

  1. Grayscale the image灰度图像
  2. Invert it倒转
  3. Blur the inverted image模糊倒置图像
  4. Dodge blend the blurred and grayscale image.闪避将模糊的灰度图像混合。

We can pick any image from the Internet. I will go with this image of Indian cricketer Virat Kohli:

我们可以从互联网上选择任何图像。 我将使用印度板球运动员Virat Kohli的这张照片:

1.加载图片 (1. Load image)

import imageioimg="http://static.cricinfo.com/db/PICTURES/CMS/263600/263697.20.jpg"start_img = imageio.imread(img)

You can see how Python sees this image with the shape attribute:

您可以看到Python如何看到带有shape属性的图像:

start_img.shape(196, 160, 30)

So this is a three channel image of size 196x160.

因此,这是一个大小为196x160的三通道图像。

2.灰度 (2. Greyscale)

We then make the image black and white.

然后,我们将图像设为黑白。

Numpy doesn’t have any in-built function for grayscaling, but we can easily convert the image using the formula. You can learn why this formula works right here.

Numpy没有任何内置的灰度功能,但是我们可以使用公式轻松转换图像。 您可以在这里了解为什么使用此公式。

Y= 0.299 R + 0.587 G + 0.114 B

So our function will look like:

因此我们的函数将如下所示:

import numpy as npdef grayscale(rgb): return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

Applying grayscale:

应用灰度:

gray_img = grayscale(start_img)

3.反转图像 (3. Invert image)

We can invert images simply by subtracting from 255, as grayscale images are 8 bit images or have a maximum of 256 tones.

我们可以简单地通过减去255来反转图像,因为灰度图像是8位图像或最多具有256个色调。

inverted_img = 255-gray_img

4.图像模糊 (4. Blur Image)

We now blur the inverted image. Blurring is done by applying a Gaussian filter to the inverted image. The key here is the variance of the Gaussian function or sigma.

现在,我们对倒置的图像进行模糊处理。 通过对反转图像应用高斯滤波器来完成模糊处理。 此处的关键是高斯函数或sigma的方差。

As sigma increases, the image becomes more blurred. Sigma controls the extent of the variance and thus, the degree of blurring.

随着sigma的增加,图像变得更加模糊。 Sigma控制差异的程度,从而控制模糊程度。

import scipy.ndimageblur_img = scipy.ndimage.filters.gaussian_filter(inverted_img,sigma=5)

5.闪避和合并 (5. Dodge and Merge)

The Colour Dodge blend mode divides the bottom layer by the inverted top layer. This lightens the bottom layer depending on the value of the top layer. We have the blurred image, which highlights the boldest edges.

“ 颜色减淡”混合模式将底层除以反转的顶层。 这会根据顶层的值减轻底层的重量。 我们有模糊的图像,该图像突出显示了最大胆的边缘。

As all our images are read using Numpy, all the matrix calculations are super fast.

由于我们使用Numpy读取所有图像,因此所有矩阵计算都非常快。

def dodge(front,back): result=front*255/(255-back)  result[result>255]=255 result[back==255]=255 return result.astype(‘uint8’)
final_img= dodge(blur_img,gray_img)

And that’s it!

就是这样!

6.绘制并保存 (6. Plot and save)

We can plot our final image using plt.imgshow. Note that we need to keep the cmap argument equal to “gray”.

我们可以使用plt.imgshow绘制最终图像。 注意,我们需要保持cmap参数等于“gray”

import matplotlib.pyplot as pltplt.imshow(final_img, cmap=”gray”)

We can save the image using:

我们可以使用以下方法保存图像:

plt.imsave(‘img2.png’, final_img, cmap=’gray’, vmin=0, vmax=255)

最后结果 (Final result)

整个代码生效 (Entire code in action)

Here we don’t have much room to play with, except with the sigma parameter while blurring.

在这里,除了模糊时使用sigma参数,我们没有太多玩的余地。

As sigma increases, the pic becomes clearer but run time also increases. So a sigma of 5 works well for us.

随着sigma的增加,图片变得更清晰,但运行时间也会增加。 因此5的sigma对我们来说效果很好。

简码: (Condensed code:)

I promised 10 lines or less, so here you go:

我答应不超过10行,因此您可以:

As always, you can find the entire detailed code on my GitHub.

与往常一样,您可以在我的GitHub上找到完整的详细代码。

PS this is how I created my Medium DP. If you like this blog, show some ❤️ :)

PS这就是我创建中型DP的方式。 如果您喜欢此博客,请显示一些❤️:)

Also I don’t own this image of Virat. I hope he doesn’t mind!

我也不拥有Virat的形象。 我希望他不介意!

翻译自: https://www.freecodecamp.org/news/sketchify-turn-any-image-into-a-pencil-sketch-with-10-lines-of-code-cf67fa4f68ce/

铅笔素描算法

铅笔素描算法_如何用10行代码将任何图像变成铅笔素描相关推荐

  1. python加密敏感信息_仅需10行代码,使用python加密用户敏感数据

    原标题:仅需10行代码,使用python加密用户敏感数据 数据分析师必须要遵守的一个规则就是数据保密,但在跨部门沟通的时候,难免会有数据泄露的情况,所以,对于用户的姓名.手机号.地址等敏感信息,一般需 ...

  2. 我是如何用10行代码搬运目标图片的?

    嗯呢,你没看错,就是教你把一个路径下的所有目标图片搬运到制定路径下.有读者说:小詹你忽悠人吧,要搬运目标图片复制粘贴不就好了嘛,要什么代码,搬砖脑子秀逗了? 咳咳,对于目标文件夹复制粘贴当然可以,还简 ...

  3. 如何用10行代码设置微信回复功能?

    大家还记得教会父母玩微信是什么时候吗?父母学会后,我们的生活就发生了「质」的变化,父母也许会 吐槽你的微信头像 不好,要你换一个头像.微信对于年轻的我来说就是一个支付工具罢了,要说好玩,花样的我觉得还 ...

  4. 2019机器学习代码实现_如何用3行代码实现任何机器学习项目

    2019机器学习代码实现 Wouldn't it be great to be able to solve complex machine learning problems quickly and ...

  5. vant组件实现上传图片裁剪_如何用 120 行代码,实现交互完整的拖拽上传组件?...

    作者 | 前端劝退师 责编 | 伍杏玲 你将在该篇学到: 如何将现有组件改写为 React Hooks函数组件 useState.useEffect.useRef是如何替代原生命周期和Ref的. 一个 ...

  6. ds--8600使用手册_我如何用57行代码复制一个价值8600万美元的项目

    ds--8600使用手册 by Tait Brown 泰特·布朗(Tait Brown) 我如何用57行代码复制一个价值8600万美元的项目 (How I replicated an $86 mill ...

  7. python包裹和运费_这个Python库真的太好用了,10行代码就能轻松搞定目标检测

    目标检测是指计算机和软件系统对图像或场景中的目标进行定位和识别的任务. 目标检测已广泛应用于人脸检测.车辆检测.人流量统计.网络图像.安防系统和无人驾驶等多个领域. 早期目标检测的实现基于经典算法,比 ...

  8. python自动化--如何用10行Python代码调一个闹钟 #电脑调闹钟

    python自动化--如何用10行Python代码写一个闹钟 前言: 文末彩蛋--轻松一刻 更多关于Python的知识请加关注哟~~.若需联系博主请私信或者加博主联系方式:       QQ:1542 ...

  9. html搜索框代码_解放双手 | 10行Python代码实现一款网页自动化工具

    各种各样的网站在我们日常工作和学习中占据着举足轻重的地位,学习.影音娱乐.查询资料.协同办公,越来越多的任务都被迁移到浏览器. 因此,网页也蕴含着很多有价值.我们能够用得到的资源.例如,数据.歌曲.影 ...

最新文章

  1. 微信公众号H5订阅消息开发 uniapp订阅消息
  2. 判断两个图片的特征向量_响应式布局提高篇 图片正确的打开方式
  3. Examine Scheduling Policies
  4. 男子刷机多次拒绝来电 维修师傅的做法被网友怒赞
  5. 计算机中定义事物各种特点的术语,计算机科学中具有特殊含义或易溷淆的术语辨析(2版).doc...
  6. java window的对象方法_Javascript:window对象的方法
  7. 解决无法使用miracast,导致手机无法投屏到电脑的问题
  8. ICT技术认证都有哪些?他们的区别是什么?
  9. 创业维艰--书摘+乱七八糟
  10. 对赌协议里,隐藏了多少致命陷阱
  11. 搭建属于你的家庭网络实时监控–HTML5在嵌入式系统中的应用·高级篇
  12. Nodejs安装在D盘酱紫报错?
  13. c语言西华大学陈晓亮,计算机与软件工程学院
  14. 数据库拆分的理解和案例(详细版)
  15. 最老程序员创业开发实训12---Android---在MVC架构下Activity设计及实现
  16. 对rman命令report obsolete的一点说明
  17. IT码农进入日企之路——模拟试题
  18. Docker harbor私有仓库部署与管理
  19. 【小技巧】AI语音合成,文本音频互转经验分享
  20. Nexus 7 二代 刷机

热门文章

  1. C++课后习题/期末考试复习资料
  2. java学习心得(1)
  3. vue中mousewheel滚动
  4. umap算法_生命科学中的 UMAP(降维算法)
  5. MP支原体污染解决方案 | MP代理泽平科技
  6. 简述mysql 数据库的特点_MySQL数据库之MySQL的特点
  7. 2018HBCPC个人题解
  8. python爬虫之下载盗墓笔记(bs4解析HTML)
  9. java 动态数据展示地图_Echarts实现地图下钻+地图数据展示
  10. SAP表的锁定与解锁