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

yipeiwu_com6年前Python基础

本文实例为大家分享了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中实现这个功能很简单,下面是具体代码。在实际应用中,你完全可以根据自己的要求进行修改!比如,示例...

Python多进程fork()函数详解

进程 进程是程序的一次动态执行过程,它对应了从代码加载、执行到执行完毕的一个完整过程。进程是系统进行资源分配和调度的一个独立单位。进程是由代码(堆栈段)、数据(数据段)、内核状态和一组寄...

对django后台admin下拉框进行过滤的实例

使用django admin 自带后台 admin后台下拉显示的时候需要添加过滤条件, 因为表是自己关联自己,同时还需要过滤掉自己, 需要获取当前对象的id,需要获取obj_id f...

Python的Django应用程序解决AJAX跨域访问问题的方法

引子 使用Django在服务器端写了一个API,返回一个JSON数据。使用Ajax调用该API: <!DOCTYPE HTML> <html> <he...

python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算

python3中的字符串是一种常见的数据类型。 字符串有多种表现形式:单引号、双引号和三引号,且这些字符串的表现形式(单、双、三)都必须是成对出现的。 单、双引号是英文的:‘'和"",三...