Python OpenCV处理图像之滤镜和图像运算
本文实例为大家分享了PythonOpenCV处理图像之滤镜和图像运算的具体代码,供大家参考,具体内容如下
0x01.滤镜
喜欢自拍的人肯定都知道滤镜了,下面代码尝试使用一些简单的滤镜,包括图片的平滑处理、灰度化、二值化等:
importcv2.cvascv
image=cv.LoadImage('img/lena.jpg',cv.CV_LOAD_IMAGE_COLOR)#Loadtheimage
cv.ShowImage("Original",image)
grey=cv.CreateImage((image.width,image.height),8,1)#8depth,1channelsograyscale
cv.CvtColor(image,grey,cv.CV_RGBA2GRAY)#Converttograysoactasafilter
cv.ShowImage('Greyed',grey)
#平滑变换
smoothed=cv.CloneImage(image)
cv.Smooth(image,smoothed,cv.CV_MEDIAN)#Applyasmoothalogrithmwiththespecifiedalgorithmcv.MEDIAN
cv.ShowImage("Smoothed",smoothed)
#均衡处理
cv.EqualizeHist(grey,grey)#Workonlyongrayscaledpictures
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("Threshold2",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)#Replaceapixelvaluewiththemaximumvalueofneighboors
#ThereisotherslikeErodewhichreplacetakethelowestvalueoftheneighborhood
#Note:TheStructuringelementisoptionnal
cv.ShowImage("Dilated",grey)
cv.WaitKey(0)
0x02.HighGUI
OpenCV内建了一套简单的GUI工具,方便我们在处理界面上编写一些控件,动态的改变输出:
importcv2.cvascv
im=cv.LoadImage("img/lena.jpg",cv.CV_LOAD_IMAGE_GRAYSCALE)
thresholded=cv.CreateImage(cv.GetSize(im),8,1)
defonChange(val):
cv.Threshold(im,thresholded,val,255,cv.CV_THRESH_BINARY)
cv.ShowImage("Image",thresholded)
#创建一个滑动条控件
onChange(100)#Callhereotherwiseatstartup.Shownothinguntilwemovethetrackbar
cv.CreateTrackbar("Thresh","Image",100,255,onChange)#Thresholdvaluearbitrarilysetto100
cv.WaitKey(0)
0x03.选区操作
有事希望对图像中某一块区域进行变换等操作,就可以使用如下方式:
importcv2.cvascv
im=cv.LoadImage("img/lena.jpg",3)
#选择一块区域
cv.SetImageROI(im,(50,50,150,150))#Givetherectanglecoordinateoftheselectedarea
#变换操作
cv.Zero(im)
#cv.Set(im,cv.RGB(100,100,100))puttheimagetoagivenvalue
#解除选区
cv.ResetImageROI(im)#ResettheROI
cv.ShowImage("Image",im)
cv.WaitKey(0)
0x04.运算
对于多张图片,我们可以进行一些运算操作(包括算数运算和逻辑运算),下面的代码将演示一些基本的运算操作:
importcv2.cvascv#orsimplyimportcv
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)#Addeverypixelstogether(blackis0solowchangeandwhiteoverloadanyway)
cv.ShowImage("Add",res)
#减
cv.AbsDiff(im,im2,res)#Likeminusforeachpixelim(i)-im2(i)
cv.ShowImage("AbsDiff",res)
#乘
cv.Mul(im,im2,res)#Multiplieeachpixels(almostwhite)
cv.ShowImage("Mult",res)
#除
cv.Div(im,im2,res)#Valueswillbelowsotheimagewilllikelytobealmostblack
cv.ShowImage("Div",res)
#与
cv.And(im,im2,res)#Bitandforeverypixels
cv.ShowImage("And",res)
#或
cv.Or(im,im2,res)#Bitorforeverypixels
cv.ShowImage("Or",res)
#非
cv.Not(im,res)#Bitnotofanimage
cv.ShowImage("Not",res)
#异或
cv.Xor(im,im2,res)#BitXor
cv.ShowImage("Xor",res)
#乘方
cv.Pow(im,res,2)#Powtheeachpixelwiththegivenvalue
cv.ShowImage("Pow",res)
#最大值
cv.Max(im,im2,res)#Maximumbetweentwopixels
#SameformMinMinS
cv.ShowImage("Max",res)
cv.WaitKey(0)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。