OpenCV-Python 對圖像的基本操作代碼
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # 設置兼容中文 plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei']
D:\Anaconda\AZWZ\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs: D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll warnings.warn("loaded more than 1 DLL from .libs:\n%s" % cv.__version__ '4.5.1'
rain = cv.imread('img/rain.jpg')[:500,:500,:] view = cv.imread('img/view.jpg') print(rain.shape) print(view.shape) (500, 500, 3) (500, 500, 3)
plt.figure() plt.subplot(1,2,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title('rain.jpg') plt.subplot(1,2,2) m2 = plt.imshow(view[:,:,::-1]) plt.title('view.jpg')
Text(0.5, 1.0, 'view.jpg')
1.圖像的加法
# cv加法 add_img = cv.add(rain,view) plt.imshow(add_img[:,:,::-1])
<matplotlib.image.AxesImage at 0x1fdc2fed160>
# numpy 加法 add_img2 = rain + view plt.imshow(add_img2[:,:,::-1])
<matplotlib.image.AxesImage at 0x1fdc2d4d4c0>
2.圖像的混合
# 圖像的混合(按照權重) img3 = cv.addWeighted(rain,0.2,view,0.8,0) plt.imshow(img3[:,:,::-1])
<matplotlib.image.AxesImage at 0x1fdc2f01a00>
3.圖像的縮放
# 獲取絕對尺寸 行,列 row,col = rain.shape[:2] print("row:",row,",col:",col) row: 500 ,col: 500 # 圖像放大 res = cv.resize(rain,(2*row,2*col)) plt.figure() plt.subplot(1,2,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title("放大前") plt.subplot(1,2,2) m2 = plt.imshow(res[:,:,::-1]) plt.title("放大後") Text(0.5, 1.0, '放大後')
# 使用相對坐標 進行圖像縮小 res2 = cv.resize(rain,None,fx=0.5,fy=0.5) plt.figure() plt.subplot(1,2,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title("縮小前") plt.subplot(1,2,2) m2 = plt.imshow(res2[:,:,::-1]) plt.title("縮小後") Text(0.5, 1.0, '縮小後')
4.圖像的平移
# 創建平移矩陣 x方向上平移100,y方向上平移50 M = np.float32([[1,0,100],[0,1,50]]) M array([[ 1., 0., 100.], [ 0., 1., 50.]], dtype=float32) # cv.warpAffine(要平移的圖形,平移矩陣,背景畫佈大小) res = cv.warpAffine(rain,M,rain.shape[:2]) plt.figure() plt.subplot(1,2,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title("平移前") plt.subplot(1,2,2) m2 = plt.imshow(res[:,:,::-1]) plt.title("平移後") Text(0.5, 1.0, '平移後')
5.圖像的旋轉
row,col = rain.shape[:2] # 生成旋轉矩陣 getRotationMatrix2D(旋轉中心坐標,旋轉角度,縮放比例) M = cv.getRotationMatrix2D((row/2,col/2),45,1) # cv.warpAffine(要旋轉的圖形,旋轉矩陣,背景畫佈大小) res = cv.warpAffine(rain,M,(col,row)) plt.figure() plt.subplot(1,2,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title("旋轉前") plt.subplot(1,2,2) m2 = plt.imshow(res[:,:,::-1]) plt.title("旋轉後") Text(0.5, 1.0, '旋轉後')
6.圖像的仿射變換
pts1 = np.float32([[50,50],[200,50],[50,200]]) pts2 = np.float32([[100,100],[200,50],[100,250]]) M = cv.getAffineTransform(pts1,pts2) M array([[ 0.66666667, 0. , 66.66666667], [-0.33333333, 1. , 66.66666667]]) res = cv.warpAffine(rain,M,rain.shape[:2]) plt.figure() plt.subplot(1,2,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title("仿射變換前") plt.subplot(1,2,2) m2 = plt.imshow(res[:,:,::-1]) plt.title("仿射變換後") Text(0.5, 1.0, '仿射變換後')
7.圖像的透射變換
pst1 = np.float32([[56,65],[368,52],[28,387],[389,390]]) pst2 = np.float32([[100,145],[300,100],[80,290],[310,300]]) M = cv.getPerspectiveTransform(pst1,pst2) M array([[ 3.98327670e-01, -2.09876559e-02, 7.49460064e+01], [-1.92233080e-01, 4.29335771e-01, 1.21896057e+02], [-7.18774228e-04, -1.33393850e-05, 1.00000000e+00]]) res = cv.warpPerspective(rain,M,rain.shape[:2]) plt.figure() plt.subplot(1,2,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title("透射變換前") plt.subplot(1,2,2) m2 = plt.imshow(res[:,:,::-1]) plt.title("透射變換後") Text(0.5, 1.0, '透射變換後')
8.圖像金字塔
# 上采樣 img_up = cv.pyrUp(rain)
# 下采樣 img_down = cv.pyrDown(rain)
plt.figure() plt.subplot(1,3,1) m1 = plt.imshow(rain[:,:,::-1]) plt.title("原圖") plt.subplot(1,3,2) m2 = plt.imshow(img_up[:,:,::-1]) plt.title("上采樣") plt.subplot(1,3,3) m2 = plt.imshow(img_down[:,:,::-1]) plt.title("下采樣")
Text(0.5, 1.0, '下采樣')
到此這篇關於OpenCV-Python 對圖像的基本操作的文章就介紹到這瞭,更多相關OpenCV-Python圖像操作內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- python imutils包基本概念及使用
- python數據可視化plt庫實例詳解
- Python一些基本的圖像操作和處理總結
- python數字圖像處理之對比度與亮度調整示例
- python數字圖像處理實現圖像的形變與縮放