Python-OpenCV 处理图像(二):滤镜和图像运算
来源:互联网 发布:淘宝哪些北美代购正品 编辑:程序博客网 时间:2024/06/10 09:24
0x01. 滤镜
喜欢自拍的人肯定都知道滤镜了,下面代码尝试使用一些简单的滤镜,包括图片的平滑处理、灰度化、二值化等:
import cv2.cv as cvimage=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the imagecv.ShowImage("Original", image)grey = cv.CreateImage((image.width ,image.height),8,1) #8depth, 1 channel so grayscalecv.CvtColor(image, grey, cv.CV_RGBA2GRAY) #Convert to gray so act as a filtercv.ShowImage('Greyed', grey)# 平滑变换smoothed = cv.CloneImage(image)cv.Smooth(image,smoothed,cv.CV_MEDIAN) #Apply a smooth alogrithm with the specified algorithm cv.MEDIANcv.ShowImage("Smoothed", smoothed)# 均衡处理cv.EqualizeHist(grey, grey) #Work only on grayscaled picturescv.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_RECTpos=3element = 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 optionnalcv.ShowImage("Dilated", grey)cv.WaitKey(0)
0x02. HighGUI
OpenCV 内建了一套简单的 GUI 工具,方便我们在处理界面上编写一些控件,动态的改变输出:
import cv2.cv as cvim = 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 trackbarcv.CreateTrackbar("Thresh", "Image", 100, 255, onChange) #Threshold value arbitrarily set to 100cv.WaitKey(0)
0x03. 选区操作
有事希望对图像中某一块区域进行变换等操作,就可以使用如下方式:
import cv2.cv as cvim = 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 ROIcv.ShowImage("Image",im)cv.WaitKey(0)
0x04. 运算
对于多张图片,我们可以进行一些运算操作(包括算数运算和逻辑运算),下面的代码将演示一些基本的运算操作:
import cv2.cv as cv#or simply import cvim = 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 blackcv.ShowImage("Div", res)# 与cv.And(im, im2, res) #Bit and for every pixelscv.ShowImage("And", res)# 或cv.Or(im, im2, res) # Bit or for every pixelscv.ShowImage("Or", res)# 非cv.Not(im, res) # Bit not of an imagecv.ShowImage("Not", res)# 异或cv.Xor(im, im2, res) #Bit Xorcv.ShowImage("Xor", res)# 乘方cv.Pow(im, res, 2) #Pow the each pixel with the given valuecv.ShowImage("Pow", res)# 最大值cv.Max(im, im2, res) #Maximum between two pixels#Same form Min MinScv.ShowImage("Max",res)cv.WaitKey(0)
0 0
- Python-OpenCV 处理图像(二):滤镜和图像运算
- Python-OpenCV 处理图像(二):滤镜和图像运算
- Python-OpenCV 处理图像(二)(三):滤镜和图像运算 图像像素点操作
- 【OpenCV】图像处理(二)图像形态学
- Python-OpenCV 处理图像:图像中边界和轮廓检测
- Python-OpenCV 处理图像(七):图像灰度化处理
- Python-OpenCV 处理图像(八):图像二值化处理
- Python-OpenCV 处理图像(七):图像灰度化处理
- Python-OpenCV图像算术运算
- 【Android图像处理】毛玻璃(模糊)滤镜(效果)二
- OpenCV入门(二) 图像平滑处理
- OPENCV图像处理提高(二):模糊
- 【OpenCV图像处理】二十二、图像边缘检测(上)
- 【OpenCV图像处理】二十三、图像边缘检测(下)
- Python图像处理(1):替换OpenCV
- Python-OpenCV 处理图像(三):图像像素点操作
- Python-OpenCV 处理图像(三):图像像素点操作
- Python-OpenCV 处理图像(三):图像像素点操作
- vim 程序编辑器
- iOS获取对象属性列表
- 写个android代码封装jar包然后代码混淆的笔记
- Android px,dp,pt,sp的区别
- 大数据学习之Scala中数据类型(3)
- Python-OpenCV 处理图像(二):滤镜和图像运算
- 命令行的艺术
- iOS LaunchImage图片尺寸
- idea 下java project 手动加载配置文件 file not found 问题
- Python-OpenCV 处理图像(三):图像像素点操作
- UML类图几种关系的总结
- Android中 int 和 String 互相转换的多种方法
- onWindowFocusChanged(boolean hasFocus)
- ListActivity 的使用