Python-OpenCV 处理图像(二):滤镜和图像运算
admin
2023-07-31 01:53:02
0

系列文章目录

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)

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...