滤镜、图像运算

1.平滑处理、灰度化、二值化等:

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)

2.运算操作(包括算数运算和逻辑运算),下面的代码将演示一些基本的运算操作:

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 )

猜你喜欢

转载自blog.csdn.net/qq_34568522/article/details/79878139