今天我们来聊一点能让大家比较提的起兴趣的内容, 大家可能觉得说 Python 的数据处理也挺枯燥的, 尤其那些对于数字没有什么感觉的同学. 当然, Python 不仅仅是可以处理数据了. 我们今天就来看看一些好玩的图像的处理.
图像处理对于很多同学来说, 可能觉得这也太复杂了吧. 本来图像处理确实是一个不那么容易的学科, 但是我们有了 Python 的一些包之后, 这件事就变得无比的简单. 我们甚至可以用 Python 来做很多有意思的和好玩的事情, 并非只有算法科学家们才能玩转图像处理了.
在图像处理界有一个鼎鼎有名的美女, 名叫 Lenna. 相信看到图片之后, 大家会说, 原来是她.
那么我们在 Python 里如何来秀出这张图呢? 首先做图像处理, OpenCV 是少不了的选择了. 我们先 import 包, 然后我们这里会定一个函数, 这个函数用来显示图片. 在整片文章里都将会用到这个函数.
- import cv2
- def show_image(image):
- >>>>cv2.imshow('Baby', image)
- >>>>cv2.waitKey(0)
- >>>>cv2.destroyAllWindows()
那么同学们, 我们现在就开始摆弄一下 Lenna 吗? 不, 我们的主角今天是一个可爱的小姑娘, 先上图片.
灰度图
闲话不多说, 我们先来看看灰度图如何获得!
- file = 'baby.jpg'
- img = cv2.imread(file)
- gray = cv2.cvtColor(
- img,
- cv2.COLOR_BGR2GRAY)
- show_image(gray)
运行结果:
修改对比度
可能同学们觉得, 不就用个软件就搞定了吗? 但是自己写个代码, 来实现这个功能不是更有意思吗? 并且只用了区区几行代码!
- file = 'baby.jpg'
- img = cv2.imread(file)
- contrast = cv2.addWeighted(
- img,
- 1.5,
- np.zeros(img.shape, img.dtype),
- 0,
- 0)
- show_image(contrast)
运行结果:
高斯模糊
高斯模糊是一个非常有用的图像处理过程, 我们很多的操作都需要这个步骤. 在电子世界里面, 高斯噪声也是非常常见的一种噪声, 因此在去噪上用途也很广泛.
- file = 'baby.jpg'
- img = cv2.imread(file)
- blur = cv2.GaussianBlur(
- img,
- (15,15),
- 0)
- show_image(blur)
运行结果:
二值化
做过二值化之后, 我们的图像就会变成黑白色, 事实上二值化也是图像处理和识别中必不可少的一个过程.
- file = 'baby.jpg'
- img = cv2.imread(file)
- gauss = cv2.cvtColor(
- cv2.GaussianBlur(img,(7,7), 0),
- cv2.COLOR_BGR2GRAY)
- _, thresh = cv2.threshold(
- gauss,
- 80,
- 255,
- cv2.THRESH_BINARY)
- colored = cv2.cvtColor(
- thresh,
- cv2.COLOR_GRAY2RGB)
- show_image(colored)
运行结果:
边缘风格
- file = 'baby.jpg'
- img = cv2.imread(file)
- gauss = cv2.cvtColor(
- cv2.GaussianBlur(img, (7,7), 0),
- cv2.COLOR_BGR2GRAY)
- _, thresh = cv2.threshold(
- gauss,
- 80,
- 255,
- cv2.THRESH_BINARY)
- cups_edges = cv2.Canny(
- thresh,
- threshold1=90,
- threshold2=110)
- colored = cv2.cvtColor(
- cups_edges,
- cv2.COLOR_GRAY2RGB)
- show_image(colored)
降噪
我们先将原图加上一些高斯噪点, 加过噪点的图片如下.
接下来我们进行去噪.
- file = 'noise.jpg'
- img = cv2.imread(file)
- denoised = cv2.fastNlMeansDenoisingColored(
- img,
- None,
- 20,
- 10,
- 7,
- 21)
- show_image(denoised)
运行结果:
我们可以看到去噪后的图片显得有点模糊, 但是整体比有噪点的效果是不是看着舒服多了呢? 这里其实和我们的参数设置有关系, 噪点也比较狠一点.
画出轮廓
下面我们来找出图像中的轮廓, Python 可以完美的勾画
- file = 'baby.jpg'
- img = cv2.imread(file)
- gray = cv2.cvtColor(
- img,
- cv2.COLOR_BGR2GRAY)
- gauss = cv2.GaussianBlur(
- gray,
- (5, 5),
- 0)
- _, bin = cv2.threshold(
- gauss,
- 150,
- 255,
- cv2.THRESH_BINARY)
- bin = cv2.bitwise_not(bin)
- _, contours, _ = cv2.findContours(
- bin,
- cv2.RETR_EXTERNAL,
- cv2.CHAIN_APPROX_SIMPLE)
- imgWithContours = np.copy(img)
- minArea = 300
- filtedContours = [cnt
- for cnt in contours
- if cv2.contourArea(cnt)> minArea]
- cv2.drawContours(
- imgWithContours,
- filtedContours,
- -1,
- (0,255,0))
- show_image(imgWithContours)
运行结果:
大家可以看到, 图中画出了仅仅画帽子的轮廓, 原因是因为我们要去做更精细的一些调试才可以画出其他的轮廓, 毕竟我们这里仅仅只是秀一下 Python 可以做什么. 几行代码已经很让人惊艳了对吗?
物体检测
物体检测我们用了一群孩子的图片, 当然物体检测只是个名称而已, 之所以我们没有叫行人检测, 是因为我比较不喜欢吹牛, 我们仅仅是检测到不知道是什么, 然后画个最大框.
- file = 'children.jpg'
- img = cv2.imread(file)
- img = cv2.resize(
- img,
- (1060, 707))
- gray = cv2.cvtColor(
- img,
- cv2.COLOR_BGR2GRAY)
- gauss = cv2.GaussianBlur(
- gray,
- (5, 5),
- 0)
- _, bin = cv2.threshold(
- gauss,
- 150,
- 255,
- cv2.THRESH_BINARY)
- bin = cv2.bitwise_not(bin)
- _, contours, _ = cv2.findContours(
- bin,
- cv2.RETR_EXTERNAL,
- cv2.CHAIN_APPROX_SIMPLE)
- imgWithContours = np.copy(img)
- minArea = 3000
- maxArea = 20000
- filtedContours = [cnt
- for cnt in contours
- if cv2.contourArea(cnt)> minArea
- and cv2.contourArea(cnt) <maxArea]
- cv2.drawContours(
- imgWithContours,
- filtedContours,
- -1,
- (0,255,0))
- imgWithBounding = np.copy(img)
- for contour in filtedContours:
- >>>>x, y, w, h = cv2.boundingRect(contour)
- >>>>cv2.rectangle(
- >>>>imgWithBounding,
- >>>>(x, y),
- >>>>(x + w, y + h),
- >>>>(0, 255, 0),
- >>>>3)
- show_image(imgWithBounding)
运行结果:
人脸检测
- file = 'baby.jpg'
- cascFile = "haarcascade_frontalface_default.xml"
- cascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascFile)
- img = cv2.imread(file)
- gray = cv2.cvtColor(
- img,
- cv2.COLOR_BGR2GRAY)
- faces = cascade.detectMultiScale(
- gray,
- scaleFactor=1.1,
- minNeighbors=5,
- minSize=(300, 300),
- flags = cv2.CASCADE_SCALE_IMAGE)
- for (x, y, w, h) in faces:
- >>>>cv2.rectangle(img,
- >>>>(x, y),
- >>>>(x+w, y+h),
- >>>>(0, 255, 0),
- >>>>2)
- show_image(img)
运行结果:
同学们可能要说了, 就一人脸, 我们能检测多张人脸吗? 当然可以, 我们来看下面的代码.
- file = 'children.jpg'
- cascFile = "haarcascade_frontalface_default.xml"
- cascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascFile)
- img = cv2.imread(file)
- img = cv2.resize(
- img,
- (1920, 1080))
- gray = cv2.cvtColor(
- img,
- cv2.COLOR_BGR2GRAY)
- faces = cascade.detectMultiScale(gray,
- scaleFactor=1.1,
- minNeighbors=5,
- minSize=(10, 10),
- maxSize=(100,100),
- flags = cv2.CASCADE_SCALE_IMAGE)
- for (x, y, w, h) in faces:
- >>>>cv2.rectangle(img,
- (x, y),
- (x+w, y+h),
- (0, 255, 0),
- 2)
- show_image(img)
运行结果:
是不是很神奇, 我们可能一直觉得人脸检测是前沿技术, 再和人工智能, 深度学习一结合, 简直就是难上加难. 其实不然, 大家看到, 其实很简单的几行代码, 也可以实现一个简单的人脸检测. 当然, 背后其实也有很多的代码. 但是 Python 的要义就是, 如果有轮子, 我们干嘛要再造一个.
人工智能与深度学习做量化请关注: AI 量化 (https://t.zsxq.com/RvfY37y) 星球限时免费, 如需加入, 请私信我获得免费邀请码!
零基础学习 Python 与深度学习应用请关注星球: Python 与深度学习 https://t.zsxq.com/bUFayZ3
微信公众号: QTechAI
来源: http://www.jianshu.com/p/da9900dd21c4