OpenCV-Python 对图像的基本操作代码
程序员文章站
2022-04-18 15:45:28
import cv2 as cvimport numpy as npimport matplotlib.pyplot as plt# 设置兼容中文plt.rcparams['font.family']...
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图像操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!