图像的读取、缩放、平移、旋转、翻转、仿射、透射

  1 import cv2
  2 import numpy as np
  3 import matplotlib.pyplot as plt
  4 
  5 # 在读取图片中,imread(‘图片地址’,‘模式参数’)函数可以来控制所读取图片的模式。
  6 # 模式参数:
  7 # 0:读入的为灰度图像(即使读入的为彩色图像也将转化为灰度图像)
  8 # 1:读入的为彩色图像(默认)
  9 
 10 # img=cv2.imread('girl.jpg',0)
 11 # 数组
 12 # print(img)
 13 # 图片大小
 14 # print(np.shape(img))
 15 
 16 # 先创建一个窗口
 17 # cv2.namedWindow('Image')
 18 # # 在窗口中显示图像
 19 # cv2.imshow('Image',img)
 20 # # 使窗口始终保持住
 21 # cv2.waitKey(0)
 22 #
 23 # # 复制图片
 24 # img1=img.copy()
 25 # 保存图像
 26 # cv2.imwrite('123.jpg',img1)
 27 
 28 # 第一个参数是保存图像的地址以及文件的名字,第二个参数是所要保存的图像数组。
 29 # 其实它还有第三个参数,针对特定的图像格式,对于JPEG,其表示的是图片的quality,用0-100的整数表示,默认为95。
 30 # 当然,你如果把参数设置的超过100也不会出错,但到100已经达到图片本身的最高质量了。
 31 # cv2.IMWRITE_JPEG_QUALITY的类型为int类型,符合图像数组为整数的要求,不用再更改类型。
 32 
 33 # cv2.imwrite('rose_copy1.jpg', img1, [cv2.IMWRITE_JPEG_QUALITY, 100])
 34 # # 释放窗口
 35 # cv2.destroyAllWindows()
 36 
 37 
 38 
 39 
 40 
 41 # img = np.array([
 42 #     [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
 43 #     [[255, 255, 0], [255, 0, 255], [0, 255, 255]],
 44 #     [[255, 255, 255], [128, 128, 128], [0, 0, 0]],
 45 # ], dtype=np.uint8)
 46 
 47 # 对于常见的RGB彩色图像,opencv使用的是BGR格式,如下例:
 48 # 用matplotlib存储
 49 # plt.imsave('img_pyplot.png', img)
 50 # 用OpenCV存储
 51 # cv2.imwrite('img_cv2.png', img)
 52 
 53 
 54 img=cv2.imread('b.jpg')
 55 # print(img.shape)  #(333, 500, 3)
 56 
 57 # 平移
 58 # M=np.float32([[1,0,100],
 59 #               [0,1,50]])
 60 # rows,cols=img.shape[:2]  #    333 500
 61 # translation=cv2.warpAffine(img,M,(cols,rows))   # 平移    cv2.warpAffine(要变换的图像,平移矩阵,变换后大小)
 62 # plt.subplot(121)
 63 # plt.imshow(img)
 64 # plt.subplot(122)
 65 # plt.imshow(translation)
 66 # plt.show()
 67 
 68 # 缩放
 69 # 插值:interpolation   None本应该是放图像大小的位置的,后面设置了缩放比例,所以就不要了
 70 # res1 = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC) # 缩放    cv2.resize(变换的图像,缩放后大小,缩放比例因子,插值方法)
 71 # #直接规定缩放大小,这个时候就不需要缩放因子
 72 # height,width = img.shape[:2]
 73 # res2 = cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)
 74 # plt.subplot(131)
 75 # plt.imshow(img)
 76 # plt.subplot(132)
 77 # plt.imshow(res1)
 78 # plt.subplot(133)
 79 # plt.imshow(res2)
 80 # plt.show()
 81 
 82 # 旋转
 83 # rows, cols = img.shape[:2]
 84 # # cv2.getRotationMatrix2D(旋转中心,旋转角度,缩放比例)
 85 # M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1/2)
 86 # # 第三个参数:变换后的图像大小
 87 # res = cv2.warpAffine(img, M, (cols, rows))   # 平移
 88 # plt.subplot(121)
 89 # plt.imshow(img)
 90 # plt.subplot(122)
 91 # plt.imshow(res)
 92 # plt.show()
 93 
 94 # 翻转    cv2.flip(para) para=0:纵向翻转 para=1:横向翻转 para=-1:同时都翻
 95 # res1 = cv2.flip(img,1)
 96 # #纵向翻转
 97 # res2 = cv2.flip(img,0)
 98 # #同时翻转
 99 # res3 = cv2.flip(img,-1)
100 # plt.subplot(141)
101 # plt.imshow(img)
102 # plt.subplot(142)
103 # # para=1:横向翻转
104 # plt.imshow(res1)
105 # plt.subplot(143)
106 # # 翻转    cv2.flip(para) para=0:纵向翻转
107 # plt.imshow(res2)
108 # plt.subplot(144)
109 # # para=-1:同时都翻
110 # plt.imshow(res3)
111 # plt.show()
112 
113 # 仿射(旋转并拉伸)    cv2.getAffineTransform(pos1,pos2)3个点前后位置  +  cv2.warpAffine(变换的图像,平移矩阵,变换后大小)
114 # rows,cols = img.shape[:2]
115 # pts1 = np.float32([[50,50],[200,50],[50,200]])
116 # pts2 = np.float32([[10,100],[200,50],[100,250]])
117 # M = cv2.getAffineTransform(pts1,pts2)
118 # #第三个参数:变换后的图像大小
119 # res = cv2.warpAffine(img,M,(cols,rows))
120 # plt.subplot(121)
121 # plt.imshow(img)
122 # plt.subplot(122)
123 # plt.imshow(res)
124 # plt.show()
125 
126 # 透射    cv2.getPerspectiveTransform(pts1,pts2)4个点前后位置 + cv2.warpAffine()
127 # rows,cols = img.shape[:2]
128 # pts1 = np.float32([[56,65],[238,52],[28,237],[239,240]])
129 # pts2 = np.float32([[0,0],[200,0],[0,200],[200,200]])
130 # M = cv2.getPerspectiveTransform(pts1,pts2)
131 # res = cv2.warpPerspective(img,M,(200,150))
132 # plt.subplot(121)
133 # plt.imshow(img)
134 # plt.subplot(122)
135 # plt.imshow(res)
136 # plt.show()

猜你喜欢

转载自www.cnblogs.com/zpdbkshangshanluoshuo/p/10415986.html