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

 更新时间:2018年07月10日 14:57:50   作者:Leo  
这篇文章主要为大家详细介绍了Python OpenCV处理图像之滤镜和图像运算,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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列表的切片与复制示例分析

    在本篇内容里小编给大家整理的是一篇关于python列表的切片与复制示例分析及相关代码,有兴趣点朋友们可以参考下。
    2021-10-10
  • Python读取excel指定列生成指定sql脚本的方法

    Python读取excel指定列生成指定sql脚本的方法

    今天小编就为大家分享一篇Python读取excel指定列生成指定sql脚本的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • pytorch逐元素比较tensor大小实例

    pytorch逐元素比较tensor大小实例

    今天小编就为大家分享一篇pytorch逐元素比较tensor大小实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Spyder中如何设置默认python解释器

    Spyder中如何设置默认python解释器

    Spyder作为一款流行的Python IDE,支持用户自定义Python解释器,包括虚拟环境的设置,通过打开Spyder,选择“Tools”->“Preferences”,在弹出窗口中选择“Use the following Python interpreter”后,浏览并选择相应的解释器或虚拟环境路径
    2024-09-09
  • python time.sleep()是睡眠线程还是进程

    python time.sleep()是睡眠线程还是进程

    这篇文章主要介绍了python time.sleep()是睡眠线程还是进程,通过实例代码给大家介绍了Python Sleep休眠函数 ,需要的朋友可以参考下
    2019-07-07
  • python使用xlrd模块读写Excel文件的方法

    python使用xlrd模块读写Excel文件的方法

    这篇文章主要介绍了python使用xlrd模块读写Excel文件的方法,较为详细的分析了xlrd模块的安装、使用与操作Excel文件的相关技巧,需要的朋友可以参考下
    2015-05-05
  • python opencv实现运动检测

    python opencv实现运动检测

    这篇文章主要为大家详细介绍了python opencv实现运动检测,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • 在Python的Flask框架下使用sqlalchemy库的简单教程

    在Python的Flask框架下使用sqlalchemy库的简单教程

    这篇文章主要介绍了在Python的Flask框架下使用sqlalchemy库的简单教程,用来简洁地连接与操作数据库,需要的朋友可以参考下
    2015-04-04
  • Python中find函数的详细使用方法

    Python中find函数的详细使用方法

    在Python中find()函数用于检测字符串中是否包含子字符串sub,如果指定start(开始)和end(结束)范围,则检查是否包含在指定范围内,这篇文章主要给大家介绍了关于Python中find函数的详细使用方法,需要的朋友可以参考下
    2023-05-05
  • Python实现GUI学生信息管理系统

    Python实现GUI学生信息管理系统

    这篇文章主要为大家详细介绍了Python实现GUI学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01

最新评论