Python OpenCV处理图像之滤镜和图像运算


Posted in Python onJuly 10, 2018

本文实例为大家分享了Python OpenCV处理图像之滤镜和图像运算的具体代码,供大家参考,具体内容如下

0x01. 滤镜

喜欢自拍的人肯定都知道滤镜了,下面代码尝试使用一些简单的滤镜,包括图片的平滑处理、灰度化、二值化等:

import cv2.cv as cv
 
image=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image
cv.ShowImage("Original", image)
 
grey = cv.CreateImage((image.width ,image.height),8,1) #8depth, 1 channel so grayscale
cv.CvtColor(image, grey, cv.CV_RGBA2GRAY) #Convert to gray so act as a filter
cv.ShowImage('Greyed', grey)
 
# 平滑变换
smoothed = cv.CloneImage(image)
cv.Smooth(image,smoothed,cv.CV_MEDIAN) #Apply a smooth alogrithm with the specified algorithm cv.MEDIAN
cv.ShowImage("Smoothed", smoothed)
 
# 均衡处理
cv.EqualizeHist(grey, grey) #Work only on grayscaled pictures
cv.ShowImage('Equalized', grey)
 
# 二值化处理
threshold1 = cv.CloneImage(grey)
cv.Threshold(threshold1,threshold1, 100, 255, cv.CV_THRESH_BINARY)
cv.ShowImage("Threshold", threshold1)
 
threshold2 = cv.CloneImage(grey)
cv.Threshold(threshold2,threshold2, 100, 255, cv.CV_THRESH_OTSU)
cv.ShowImage("Threshold 2", threshold2)
 
element_shape = cv.CV_SHAPE_RECT
pos=3
element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape)
cv.Dilate(grey,grey,element,2) #Replace a pixel value with the maximum value of neighboors
#There is others like Erode which replace take the lowest value of the neighborhood
#Note: The Structuring element is optionnal
cv.ShowImage("Dilated", grey)
 
cv.WaitKey(0)

0x02. HighGUI

OpenCV 内建了一套简单的 GUI 工具,方便我们在处理界面上编写一些控件,动态的改变输出:

import cv2.cv as cv
 
im = cv.LoadImage("img/lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
thresholded = cv.CreateImage(cv.GetSize(im), 8, 1)
 
def onChange(val):
  cv.Threshold(im, thresholded, val, 255, cv.CV_THRESH_BINARY)
  cv.ShowImage("Image", thresholded)
 
# 创建一个滑动条控件
onChange(100) #Call here otherwise at startup. Show nothing until we move the trackbar
cv.CreateTrackbar("Thresh", "Image", 100, 255, onChange) #Threshold value arbitrarily set to 100
 
cv.WaitKey(0)

0x03. 选区操作

有事希望对图像中某一块区域进行变换等操作,就可以使用如下方式:

import cv2.cv as cv
 
im = cv.LoadImage("img/lena.jpg",3)
 
# 选择一块区域
cv.SetImageROI(im, (50,50,150,150)) #Give the rectangle coordinate of the selected area
 
# 变换操作
cv.Zero(im)
#cv.Set(im, cv.RGB(100, 100, 100)) put the image to a given value
 
# 解除选区
cv.ResetImageROI(im) # Reset the ROI
 
cv.ShowImage("Image",im)
 
cv.WaitKey(0)

0x04. 运算

对于多张图片,我们可以进行一些运算操作(包括算数运算和逻辑运算),下面的代码将演示一些基本的运算操作:

import cv2.cv as cv#or simply import cv
 
im = cv.LoadImage("img/lena.jpg")
im2 = cv.LoadImage("img/fruits-larger.jpg")
cv.ShowImage("Image1", im)
cv.ShowImage("Image2", im2)
 
res = cv.CreateImage(cv.GetSize(im2), 8, 3)
 
# 加
cv.Add(im, im2, res) #Add every pixels together (black is 0 so low change and white overload anyway)
cv.ShowImage("Add", res)
 
# 减
cv.AbsDiff(im, im2, res) # Like minus for each pixel im(i) - im2(i)
cv.ShowImage("AbsDiff", res)
 
# 乘
cv.Mul(im, im2, res) #Multiplie each pixels (almost white)
cv.ShowImage("Mult", res)
 
# 除
cv.Div(im, im2, res) #Values will be low so the image will likely to be almost black
cv.ShowImage("Div", res)
 
# 与
cv.And(im, im2, res) #Bit and for every pixels
cv.ShowImage("And", res)
 
# 或
cv.Or(im, im2, res) # Bit or for every pixels
cv.ShowImage("Or", res)
 
# 非
cv.Not(im, res) # Bit not of an image
cv.ShowImage("Not", res)
 
# 异或
cv.Xor(im, im2, res) #Bit Xor
cv.ShowImage("Xor", res)
 
# 乘方
cv.Pow(im, res, 2) #Pow the each pixel with the given value
cv.ShowImage("Pow", res)
 
# 最大值
cv.Max(im, im2, res) #Maximum between two pixels
#Same form Min MinS
cv.ShowImage("Max",res)
 
cv.WaitKey(0)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用md5sum检查目录中相同文件代码分享
Feb 02 Python
用Python实现通过哈希算法检测图片重复的教程
Apr 02 Python
用Python计算三角函数之atan()方法的使用
May 15 Python
Python实现图片转字符画的示例
Aug 22 Python
Python线性方程组求解运算示例
Jan 17 Python
让Django支持Sql Server作后端数据库的方法
May 29 Python
python3监控CentOS磁盘空间脚本
Jun 21 Python
深入浅析Python 中 is 语法带来的误解
May 07 Python
基于Python的图像数据增强Data Augmentation解析
Aug 13 Python
Python实现桌面翻译工具【新手必学】
Feb 12 Python
如何使用Python抓取网页tag操作
Feb 14 Python
python之MSE、MAE、RMSE的使用
Feb 24 Python
Python使用cx_Freeze库生成msi格式安装文件的方法
Jul 10 #Python
python操作excel文件并输出txt文件的实例
Jul 10 #Python
深入浅析Python传值与传址
Jul 10 #Python
Python+OpenCV目标跟踪实现基本的运动检测
Jul 10 #Python
python3读取excel文件只提取某些行某些列的值方法
Jul 10 #Python
python读取excel指定列数据并写入到新的excel方法
Jul 10 #Python
python 常用的基础函数
Jul 10 #Python
You might like
PHP学习之数组的定义和填充
2011/04/17 PHP
使用php shell命令合并图片的代码
2011/06/23 PHP
利用PHP实现智能文件类型检测的实现代码
2011/08/02 PHP
php获取微信共享收货地址的方法
2017/12/21 PHP
在 Laravel 6 中缓存数据库查询结果的方法
2019/12/11 PHP
基于jquery的一行代码轻松实现拖动效果
2010/12/28 Javascript
基于jquery实现图片广告轮换效果代码
2011/07/07 Javascript
Jvascript学习实践案例(开发常用)
2012/06/25 Javascript
JavaScript的事件绑定(方便不支持js的时候)
2013/10/01 Javascript
javaScript对文字按照拼音排序实现代码
2013/12/27 Javascript
jquery实现全屏滚动
2015/12/28 Javascript
JavaScript事件 "事件对象"的注意要点
2016/01/14 Javascript
AngularJS模板加载用法详解
2016/11/04 Javascript
移动端基础事件总结与应用
2017/01/12 Javascript
jQuery实现按比例缩放图片的方法
2017/04/29 jQuery
JS模拟超市简易收银台小程序代码解析
2017/08/18 Javascript
JavaScript设计模式之原型模式分析【ES5与ES6】
2018/07/26 Javascript
Node.js net模块功能及事件监听用法分析
2019/01/05 Javascript
深入探讨JavaScript的最基本部分之执行上下文
2019/02/12 Javascript
vue路由切换之淡入淡出的简单实现
2019/10/31 Javascript
使用Vant完成通知栏Notify的提示操作
2020/11/11 Javascript
[02:07]DOTA2新英雄展现中国元素,完美“圣典”亮相央视
2016/12/19 DOTA
[56:29]Secret vs Optic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
在Django中输出matplotlib生成的图片方法
2018/05/24 Python
python3.6数独问题的解决
2019/01/21 Python
python开发游戏的前期准备
2019/05/05 Python
Python深拷贝与浅拷贝用法实例分析
2019/05/05 Python
Python爬虫requests库多种用法实例
2020/05/28 Python
python爬虫要用到的库总结
2020/07/28 Python
英国鹦鹉店:Parrot Essentials
2018/12/03 全球购物
Molly Bracken法国电子商店:法国女性时尚品牌
2019/07/24 全球购物
招聘与培训专员岗位职责
2014/01/30 职场文书
我们的节日元宵活动方案
2014/08/23 职场文书
个人四风问题整改措施思想汇报
2014/10/04 职场文书
2016年植树节红领巾广播稿
2015/12/17 职场文书
eclipse创建项目没有dynamic web的解决方法
2021/06/24 Java/Android