4.1 混合两张图像
#Import required packages import cv2 #Read image 1 img1=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") #Read image 2 img2=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat2.jpg") #Define alpha and beta alpha=0.30 beta=0.70 #Blend images final_image=cv2.addWeighted(img1,alpha,img2,beta,0.0) #show image cv2.imshow('mix',final_image) cv2.waitKey(0) cv2.destroyAllWindows()
将两张猫咪的图片混合了起来。
4.2 改变图像的对比度和亮度
#Import required packages import cv2 import numpy as np #Read images image=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") #Create a dummy image that stores different contrast and brightness new_image=np.zeros(image.shape,image.dtype) #Brightness and contrast parameters contrast=3.0 bright=2 #Change the contrast and brightness for y in range(image.shape[0]): for x in range(image.shape[1]): for c in range(image.shape[2]): new_image[y,x,c]=np.clip(contrast*image[y,x,c]+bright,0,255) cv2.namedWindow('original', cv2.WINDOW_NORMAL) cv2.imshow('original',image) cv2.namedWindow('transform', cv2.WINDOW_NORMAL) cv2.imshow('transform',new_image) cv2.waitKey(0) cv2.destroyAllWindows()
4.3 往图像中添加文字
cv2模块中的cv2.putText()函数可以往图像中添加文字。
#import required packages import cv2 #Read image image=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") #Define font font=cv2.FONT_HERSHEY_SIMPLEX #Write on the image cv2.putText(image,"I am a Cat",(230,50),font,0.8,(0,255,0),2,cv2.LINE_AA) cv2.namedWindow('AddText', cv2.WINDOW_NORMAL) cv2.imshow('AddText',image) cv2.waitKey(0) cv2.destroyAllWindows()
4.4 平滑图像
三种用于平滑图像的滤波器
- 中值滤波器
- 高斯滤波器
- 双边滤波器
4.4.1 中值滤波器
cv2.medianBlur()函数能够实现中值模糊的效果。
4.4.2 高斯滤波器
cv2.GaussianBlur()函数应用于高斯滤波器。
4.4.3 双边滤波器
cv2.bilateralFilter()函数
#Import required packages import cv2 #Read images for different blurring purposes image_Original=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") image_MedianBlur=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") image_GaussianBlur=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") image_BilateralBlur=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") #Blur images image_MedianBlur=cv2.medianBlur(image_MedianBlur,9) image_GaussianBlur=cv2.GaussianBlur(image_GaussianBlur,(9,9),10) image_BilateralBlur=cv2.bilateralFilter(image_BilateralBlur,9,100,75) #show images cv2.namedWindow('image_MedianBlur', cv2.WINDOW_NORMAL) cv2.imshow('image_MedianBlur',image_MedianBlur) cv2.namedWindow('image_GaussianBlur', cv2.WINDOW_NORMAL) cv2.imshow('image_GaussianBlur',image_GaussianBlur) cv2.namedWindow('image_BilateralBlur', cv2.WINDOW_NORMAL) cv2.imshow('image_BilateralBlur',image_BilateralBlur) cv2.waitKey(0) cv2.destroyAllWindows()
4.5 改变图像的形状
两种改变图像的操作:侵蚀(erosion)和扩张(dilation)。扩张操作导致物体边界的像素增加,侵蚀操作导致物体边界的像素减少。
cv2.getStructuringElement()函数用于定义核,作为侵蚀或扩张函数的参数。
# EROSION CODE: #Import package import cv2 #Read image image=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") #Define erosion size s1=0 s2=10 s3=10 #Define erosion type t1=cv2.MORPH_RECT t2=cv2.MORPH_CROSS t3=cv2.MORPH_ELLIPSE #Define and save the erosion template tmp1=cv2.getStructuringElement(t1,(2*s1+1,2*s1+1),(s1,s1)) tmp2=cv2.getStructuringElement(t2,(2*s2+1,2*s2+1),(s2,s2)) tmp3=cv2.getStructuringElement(t3,(2*s3+1,2*s3+1),(s3,s3)) #Apply the erosion template to the image and save in different variables final1=cv2.erode(image,tmp1) final2=cv2.erode(image,tmp2) final3=cv2.erode(image,tmp3) #Show all the images with different erosions cv2.namedWindow('final1', cv2.WINDOW_NORMAL) cv2.imshow('final1',final1) cv2.namedWindow('final2', cv2.WINDOW_NORMAL) cv2.imshow('final2',final2) cv2.namedWindow('final3', cv2.WINDOW_NORMAL) cv2.imshow('final3',final3) cv2.waitKey(0) cv2.destroyAllWindows()
# DILATION CODE: #Import package import cv2 #Read image image=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") #Define erosion size d1=0 d2=10 d3=20 #Define erosion type t1=cv2.MORPH_RECT t2=cv2.MORPH_CROSS t3=cv2.MORPH_ELLIPSE #Define and save the erosion template tmp1=cv2.getStructuringElement(t1,(2*d1+1,2*d1+1),(d1,d1)) tmp2=cv2.getStructuringElement(t2,(2*d2+1,2*d2+1),(d2,d2)) tmp3=cv2.getStructuringElement(t3,(2*d3+1,2*d3+1),(d3,d3)) #Apply the erosion template to the image and save in different variables final1=cv2.dilate(image,tmp1) final2=cv2.dilate(image,tmp2) final3=cv2.dilate(image,tmp3) #Show all the images with different erosions cv2.namedWindow('final1', cv2.WINDOW_NORMAL) cv2.imshow('final1',final1) cv2.namedWindow('final2', cv2.WINDOW_NORMAL) cv2.imshow('final2',final2) cv2.namedWindow('final3', cv2.WINDOW_NORMAL) cv2.imshow('final3',final3) cv2.waitKey(0) cv2.destroyAllWindows()
4.6 实现图像阈值化
首先需要将图像转换成灰度格式,然后转换成二值格式——只有黑色与白色的图像。
提供一个参考值,然后将所有值大于或小于它的像素都转换成黑色或白色。
使用cv2.threshold()函数做图像阈限化。
#Import packages import cv2 #Read image image=cv2.imread("C:\\Users\\mac\\Pictures\\measure\\cat1.jpg") #Define threshold types ''' 0 - Binary 1 - Binary Inverted 2 - Truncated 3 - Threshold To Zero 4 - Threshold To Zero Inverted ''' #Apply different threshold and save in different variables _, img1=cv2.threshold(image,50,255,0) _, img2=cv2.threshold(image,50,255,1) _, img3=cv2.threshold(image,50,255,2) _, img4=cv2.threshold(image,50,255,3) _, img5=cv2.threshold(image,50,255,4) #Show the different threshold images cv2.namedWindow('img1', cv2.WINDOW_NORMAL) cv2.imshow('img1',img1) cv2.namedWindow('img2', cv2.WINDOW_NORMAL) cv2.imshow('img2',img2) cv2.namedWindow('img3', cv2.WINDOW_NORMAL) cv2.imshow('img3',img3) cv2.namedWindow('img4', cv2.WINDOW_NORMAL) cv2.imshow('img4',img4) cv2.namedWindow('img5', cv2.WINDOW_NORMAL) cv2.imshow('img5',img5) cv2.waitKey(0) cv2.destroyAllWindows()
4.7 计算梯度
在本节中,将介绍如何使用索伯导数(Sobel derivative)做边缘检测。边有两种方向:垂直方向和水平方向。。针对这种算法,我们强调只有空间频率非常高的区域才能算作边。一个区域的空间频率是指这个区域内的细节丰富程度。
在下面的代码中,我们用高斯模糊去除噪声,然后将图像转换成灰度图。我们使用cv2.cvtColor()函数将图像转换成灰度图。skimage模块里的函数也能达到同样的效果。最后,我们将灰度图输出到cv2.Sobel()函数。