Python-OpenCV 处理图像(二):滤镜和图像运算
0x01. 滤镜
喜欢自拍的人肯定都知道滤镜了,下面代码尝试使用一些简单的滤镜,包括图片的平滑处理、灰度化、二值化等:
import cv2.cv as cvimage=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 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 trackbar
cv.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 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-OpenCV 处理图像(二):滤镜和图像运算相关推荐
- python+OpenCV笔记(二十四):Shi-Tomasi角点检测
Shi-Tomasi角点检测 原理 python+OpenCV笔记(二十二):角点检测原理(Harris角点检测原理.Shi-Tomasi角点检测原理)https://blog.csdn.net/qq ...
- python opencv cv2.cvtColor()方法(将图像从一种颜色空间转换为另一种颜色空间)(转换成灰度图)
def cvtColor(src, code, dst=None, dstCn=None): # real signature unknown; restored from __doc__" ...
- python opencv imread(filename, flags=None) 读取图像 flags cv::ImreadModes 参数上哪看去?
在调用opencv的imread(filename, flags=None)函数时,后面有个flags=None参数,但打开文档轻描淡写,不知该参数上哪找去,用全局搜索也搜不到,莫非要到官网查?(擦汗 ...
- OpenCv学习笔记(二)--Mat矩阵(图像容器)的创建及CV_8UC1,CV_8UC2等参数详解
(一)Mat矩阵(图像容器)创建时CV_8UC1,CV_8UC2等参数详解 1--Mat不但是一个非常有用的图像容器类,同时也是一个通用的矩阵类 2--创建一个Mat对象的方法很多,我们现在先看一下M ...
- OpenCV图像处理学习二十,图像直方图均衡化原理与实现
一.图像直方图的概念 图像直方图,是指对整个图像在灰度范围内的像素值(0~255)统计出现频率次数,据此生成的直方图,称为图像直方图.直方图反映了图像灰度的分布情况,是图像的统计学特征.图像的灰度直方 ...
- python+OpenCv笔记(七):图像的形态学操作(腐蚀与膨胀、开闭运算、礼帽与黑帽)
一.腐蚀与膨胀 腐蚀就是原图中高亮的部分被蚕食,效果图拥有比原图更小的高亮区域. 腐蚀的作用是:消除物体边界点,使目标缩小,可以消除小于结构元素的噪声点. 膨胀就是使原图中高亮的部分扩张,效果图拥有比 ...
- python opencv 条形码及二维码检测识别
目录 条形码检测识别 二维码检测识别 基于python opencv pyzbar 实现. 条形码检测识别 原图: 最后截取图: 直接上代码: import cv2 import numpy as n ...
- python+OpenCV图像处理(二)图像像素的访问、通道的合并与分离
图像像素的访问.通道的合并与分离 (一)像素访问 在第一篇博客中,向大家介绍了,所谓的图像在计算机看来就是一个矩阵,对于RGB图像矩阵一共有三层,分别代表着RGB通道,矩阵中每一个数的大小代表着不同通 ...
- opencv 把3通道图像转成单通道_小强学Python+OpenCV之-1.4.3图像(矩阵)加减运算
引入 问题引入,考虑以下几个问题: Q1. 一个3通道像素p的BGR分量(8bit表示)值均相同,那么这个像素是什么颜色的? Q2. 该像素p加上自己,即,像素p的各个通道值翻倍,那么这个像素的颜色会 ...
- python+OpenCv笔记(六):图像的几何变换(缩放、平移、旋转、仿射、透射、金字塔)
一.图像的缩放 OpenCv API: cv2.resize(src, dsize, fx, fy, interpolation) 参数: src:输入的图像 dsize:绝对尺寸,直接将图像调整为指 ...
最新文章
- Hive的基本操作-自定义函数
- linux中配置phpcms v9 中的sphinx
- RabbitMQ 普通集群配置_04
- 博士笔记 | 周志华《机器学习》手推笔记第二章-模型评估与选择
- flutter笔记:使用flutter webvie
- 又是一个github吗? Kubernetes 初创公司 Heptio被VMware 收购
- 【优化配置】基于matlab遗传算法求解风电混合储能容量优化配置问题【含Matlab源码 228期】
- 长尾关键词组合网站(附源码)
- 排列和组合问题完全解析
- 国家区块链漏洞库 《区块链漏洞定级细则》发布
- WFP之关联上下文数据以及注意事项
- [React hooks] Antd Form: Instance created by `useForm` is not connected to any Form element.Forget t
- 牛客寒假算法基础训练营3 G.糖果
- pandas 计算累计和及累计占比
- Django SVG 名字空间
- Django如何发送电子邮件?
- IO Active将工业机器人变为“杀手”,以警示各大机器人公司
- win7-64bit 安装Python3.5
- Python爬虫期末作业 | 爬取起点小说网作者和书名,并以Excel形式存储
- MPI实现矩阵向量乘法