1.cv.resize()で画像サイズを変更します
元の画像(opencvは読み取り後にBGR画像になります):
import cv2 as cv
img = cv.imread('1.JPG',0)
img = cv.resize(img,(256,256)) #改变大小到(256,256)
cv.imshow('img',img)
cv.imwrite('img.jpg',img)
cv.waitKey(0)
効果画像:
2.cv.warpAffine()画像変換用
npを使用して変換行列MMを作成しますMそしてそれをwarpAffine()に渡します。
M = [1 0 tx 0 1 ty] M = \ begin {bmatrix} 1&0&t_x \\ 0&1&t_y \ end {bmatrix}M=[1001tXtそして]
import cv2 as cv
import numpy as np
img = cv.imread('1.JPG',0)
img = cv.resize(img,(256,256))
rows,cols = img.shape
M = np.float32([[1,0,50],[0,1,50]]) #转移矩阵
dst = cv.warpAffine(img,M,(cols,rows)) #参数:平移图片,转移矩阵,输出大小(width,height)
cv.imshow('img',dst)
cv.waitKey(0)
cv.destroyAllWindows()
3.cv.getRotationMatrix2D()を使用して、回転マトリックスを取得します
回転回転阵:M = [cosθ− sinθsinθcosθ ] M = \ begin {bmatrix} cos \ theta&-sin \ theta \\ sin \ theta&cos \ theta \ end {bmatrix}M=[C O S θsの私のn θ- sの私のn θC O S θ]、角度はθ\ thetaθ。
OpenCVは、スケーラブルな回転と調整可能な回転中心を提供し、任意の位置で回転できます。変更された変換マトリックスは次のとおりです。
[αβ(1 −α)⋅中心。X−β⋅中心。Y−βαβ⋅ center。x+(1 −α)⋅center。y] \ begin {bmatrix} \ alpha&\ beta&(1- \ alpha)\ cdot center.x- \ beta \ cdot center.y \\-\ beta& \ alpha&\ beta \ cdot center.x +(1- \ alpha)\ cdot center.y \ end {bmatrix}[A- BbA(1−a )⋅C E n個のT EのR 。バツ−b⋅C E n個のT EのR 。そしてb⋅C E n個のT EのR 。バツ+(1−a )⋅C E n個のT EのR 。そして]
中:α=スケール⋅cosθ、β=スケール⋅sinθ
\ begin {array} {l} \ alpha = scale \ cdot \ cos \ theta、\\ \ beta = scale \ cdot \ sin \ theta \終了{配列A=s c a l e⋅cosθ 、b=s c a l e⋅なし私
import cv2 as cv
img = cv.imread('1.JPG',0)
img = cv.resize(img,(256,256))
rows,cols = img.shape
# cols-1 和 rows-1 是坐标限制
M = cv.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),90,1) #参数:center, angle, scale
dst = cv.warpAffine(img,M,(cols,rows)) # #参数:img:平移图片,M:变换矩阵,输出大小(width,height)
cv.imshow('img',dst)
cv.imwrite('img.jpg',dst)
cv.waitKey(0)
cv.destroyAllWindows()
cv.getAffineTransform()を使用して、アフィン変換行列を取得します
アフィン変換では、元の画像のすべての平行線は、出力画像でも平行のままです。
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
img = cv.imread('1.JPG')
rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1,pts2)
dst = cv.warpAffine(img,M,(cols,rows))
plt.figure(12)
plt.subplot(121),plt.axis('off'),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.axis('off'),plt.imshow(dst,),plt.title('Output')
plt.savefig('img.jpg',)
plt.show()
cv.waitKey(0)
cv.getPerspectiveTransform()を使用して、パースペクティブ変換マトリックスを取得します
パースペクティブ変換には、3x3変換マトリックスが必要です。変換後も直線は直線のままです。この変換マトリックスを見つけるには、入力画像に4つのポイントがあり、出力画像に対応するポイントが必要です。これらの4つのポイントのうち、3つは同一直線上にあるべきではありません。次に、変換行列は関数cv.getPerspectiveTransformを介して見つけることができます。次に、サブマトリックスをcv.warpPerspectiveに送信します
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
img = cv.imread('1.JPG')
rows,cols,ch = img.shape
pts1 = np.float32([[0,0],[300,0],[0,300],[300,300]]) #源图像中四边形顶点的坐标
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]]) #目标图像中相应四边形顶点的坐标。
M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))
plt.figure(12)
plt.subplot(121),plt.axis('off'),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.axis('off'),plt.imshow(dst,),plt.title('Output')
plt.savefig('img.jpg',)
plt.show()
cv.waitKey(0)