本文主要内容:
负片、调节图片亮度与对比度、图像裁剪、图像尺寸变换、图像旋转、图像添加噪声、图像模糊与滤波
所有代码已经在Spyder上编译通过的,python版本是3.7,希望能有帮助
参考书目:《Python人脸识别——从入门到工程实践》王天庆著
import cv2
import numpy as np
img=cv2.imread("1.png")
cv2.imshow("lala",img)
cv2.waitKey(1000)
print(img)
height=img.shape[0]
width=img.shape[1]
negative_file=np.zeros((height,width,3))
b,g,r=cv2.split(img)
r=255-r
b=255-b
g=255-g
negative_file[:,:,0]=b
negative_file[:,:,1]=g
negative_file[:,:,2]=r
cv2.imwrite("negative.jpg",negative_file)
img2=cv2.imread("negative.jpg")
cv2.imshow("fupian",img2)
cv2.waitKey(2000)
cv2.destroyAllWindows();
import cv2
import numpy as np
def convert_img1(img,alpha,beta):
blank=np.zeros(img.shape,img.dtype)
return cv2.addWeighted(img,alpha,blank,0,beta)
def convert_img2(img,alpha,beta):
rows,cols,channel=img.shape
new_img=np.zeros(img.shape,img.dtype)
for i in range(0,rows):
for j in range(0,cols):
for k in range(0,channel):
new_img[i,j,k]=np.clip(alpha *img[i,j,k]+beta,0,255)
return new_img
img=cv2.imread('1.png')
cv2.imshow('old',img)
cv2.waitKey(1000)
cv2.imwrite('convert3.jpg',convert_img1(img,1.5,50))
img1=cv2.imread('convert3.jpg')
cv2.imshow('convert',img1)
cv2.waitKey(2000)
cv2.destroyAllWindows()
import cv2
#import numpy as np
image=cv2.imread('1.png')
cv2.imshow('lena',image)
cv2.waitKey(1000)
cv2.imshow('lena2',image[50:400,50:400])
cv2.waitKey(5000)
cv2.destroyAllWindows()
import cv2
#import numpy as np
image=cv2.imread('1.png')
cv2.imshow('lena',image)
cv2.waitKey(1000)
new_img1=cv2.resize(image,(200,200),interpolation=cv2.INTER_AREA)
cv2.imshow('lena1',new_img1)
cv2.waitKey(1000)
new_img2=cv2.resize(image,(200,200),interpolation=cv2.INTER_CUBIC)
cv2.imshow('lena2',new_img2)
cv2.waitKey(1000)
new_img3=cv2.resize(image,(200,200),interpolation=cv2.INTER_LINEAR)
cv2.imshow('lena3',new_img3)
cv2.waitKey(10000)
cv2.destroyAllWindows()
import cv2
#import numpy as np
image=cv2.imread('1.png')
cv2.imshow('lena',image)
cv2.waitKey(1000)
rotate_img=cv2.getRotationMatrix2D((image.shape[1]/2,image.shape[0]/2),45,0.5)
rmg = cv2.warpAffine(image, rotate_img, (image.shape[0], image.shape[1]))
cv2.imwrite('rotate_img.jpg',rmg)
image2=cv2.imread('rotate_img.jpg')
cv2.imshow('lena1',image2)
cv2.waitKey(5000)
import cv2
import numpy as np
import random
def salt_and_pepper_noise(img,percentage):
rows,cols=img.shape
num=int(percentage*rows*cols)
for i in range(num):
x=random.randint(0,rows-1)
y=random.randint(0,cols-1)
if random.randint(0,1)==1:
img[x,y]=0
else:
img[x,y]=255
return img
def gaussian_noise(img,mu,sigma,k):
rows,cols=img.shape
for i in range(rows):
for j in range(cols):
value=int(img[i,j]+k *random.gauss(mu=mu,sigma=sigma))
value=np.clip(a_max=255,a_min=0,a=value)
img[i,j]=value
return img
image=cv2.imread('1.png')
gray_img=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray_lena',gray_img)
cv2.waitKey(1000)
gray_img2=gray_img.copy()
gaus_img=gaussian_noise(gray_img,0,1,32)
cv2.imshow('gaus_lena',gaus_img)
cv2.waitKey(1000)
salt_img=salt_and_pepper_noise(gray_img2,0.3)
cv2.imshow('salt_lena',salt_img)
cv2.waitKey(1000)
import cv2
import numpy as np
import random
def salt_and_pepper_noise(img,percentage):
rows,cols=img.shape
num=int(percentage*rows*cols)
for i in range(num):
x=random.randint(0,rows-1)
y=random.randint(0,cols-1)
if random.randint(0,1)==1:
img[x,y]=0
else:
img[x,y]=255
return img
def gaussian_noise(img,mu,sigma,k):
rows,cols=img.shape
for i in range(rows):
for j in range(cols):
value=int(img[i,j]+k *random.gauss(mu=mu,sigma=sigma))
value=np.clip(a_max=255,a_min=0,a=value)
img[i,j]=value
return img
image=cv2.imread('1.png')
gray_img=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray_lena',gray_img)
cv2.waitKey(1000)
gray_img2=gray_img.copy()
gaus_img=gaussian_noise(gray_img,0,1,32)
cv2.imshow('gaus_lena',gaus_img)
cv2.waitKey(1000)
salt_img=salt_and_pepper_noise(gray_img2,0.3)
cv2.imshow('salt_lena',salt_img)
cv2.waitKey(1000)
kernel=np.ones((5,5),np.float32)/25
conv_2d_img=cv2.filter2D(salt_img,-1,kernel)
cv2.imshow('conv_2d',conv_2d_img)
cv2.waitKey(1000)
median_img=cv2.medianBlur(salt_img,5)
cv2.imshow('median_blur',median_img)
cv2.waitKey(1000)
gaussian_img=cv2.GaussianBlur(gaus_img,(5,5),0)
cv2.imshow('guass_blur',gaussian_img)
cv2.waitKey(1000)
bilateral_img=cv2.bilateralFilter(gaus_img,9,75,75)
cv2.imshow('bilateral_filter',bilateral_img)
cv2.waitKey(8000)
cv2.destroyAllWindows()