import cv2
img = cv2.imread("3.1.jpeg")
#图像按指定宽高进行缩放
#按宽100高400的大小进行缩小
dst1 = cv2.resize(img,(100,400))
dst2 = cv2.resize(img,(400,400))
cv2.imshow("img",img)
cv2.imshow("dst1",dst1)
cv2.imshow("dst2",dst2)
cv2.waitKey()
cv2.destroyAllWindows()
#图像按照指定比例进行缩放
dst3 = cv2.resize(img,None,fx=1 / 3,fy=1 / 2)
dst4 = cv2.resize(img,None,fx=2,fy=2)
cv2.imshow("dst3",dst3)
cv2.imshow("dst4",dst4)
cv2.waitKey()
cv2.destroyAllWindows()
#参数为0沿x轴翻转,参数正数沿y轴翻转,参数为负数同时沿x轴y轴翻转
dst1 = cv2.flip(img,0)
dst2 = cv2.flip(img,1)
dst3 = cv2.flip(img,-1)
cv2.imshow("dst1",dst1)
cv2.imshow("dst2",dst2)
cv2.imshow("dst3",dst3)
cv2.waitKey()
cv2.destroyAllWindows()
#图像向右下方平移
import numpy as np
#读取图像行数
rows = len(img)
#读取图像列数
cols = len(img[0])
#横坐标向右移动50像素,纵坐标向下移动100像素
#M = [[1,0,水平移动的距离],[0,1,竖直移动的距离]] 距离为正数向下向右,负数向上向左
M = np.float32([[1,0,50],[0,1,100]])
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("dst",dst)
cv2.waitKey()
cv2.destroyAllWindows()
#让图像逆时针旋转
#图像的中心点
center = (rows/2,cols/2)
#以图像为中心,逆时针旋转30°,缩放0.8倍
M = cv2.getRotationMatrix2D(center,30,0.8)
#按照M进行仿射
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("dst",dst)
cv2.waitKey()
cv2.destroyAllWindows()
dst1 = cv2.resize(img,(400,400))
dst2 = cv2.resize(img,(400,200))
dst3 = cv2.resize(img,(400,200))
fontStyle = cv2.FONT_HERSHEY_TRIPLEX
#参数依次为:图像,文字,文字左下角坐标,字体样式,大小,颜色
cv2.putText(dst2,"WQ",(20,90),fontStyle,1,(0,0,255))
cv2.putText(dst3,"20040420",(20,90),fontStyle,1,(0,0,255))
dst4 = cv2.resize(img,(200,200))
dst5 = cv2.resize(img,(200,200))
dst5 = cv2.flip(dst5,1)
dst8 = cv2.resize(img,(200,200))
dst8 = cv2.flip(dst8,0)
dst9 = cv2.resize(img,(200,200))
dst9 = cv2.flip(dst9,-1)
dst6 = cv2.resize(img,(200,200))
rows = len(dst6)
#读取图像列数
cols = len(dst6[0])
#横坐标向右移动50像素,纵坐标向下移动100像素
#M = [[1,0,水平移动的距离],[0,1,竖直移动的距离]] 距离为正数向下向右,负数向上向左
M = np.float32([[1,0,-100],[0,1,-50]])
dst6 = cv2.warpAffine(dst6,M,(cols,rows))
black_white = cv2.resize(img,(200,400))
for i in range(0,20):
for j in range(0,200):
black_white[i,j] = [255,255,255]
for i in range(40,60):
for j in range(0,200):
black_white[i,j] = [0,0,0]
for i in range(80,100):
for j in range(0,200):
black_white[i,j] = [255,255,255]
for i in range(120,140):
for j in range(0,200):
black_white[i,j] = [0,0,0]
for i in range(140,160):
for j in range(0,200):
black_white[i,j] = [255,255,255]
dst10 = cv2.resize(img,(200,200))
center = (rows/2,cols/2)
M = cv2.getRotationMatrix2D(center,30,0.5)
dst10 = cv2.warpAffine(dst10,M,(cols,rows))
img_h = np.hstack((dst1,dst1)) #水平拼接
img_v = np.vstack((dst2,dst2)) #竖直拼接
img2 = np.vstack((dst2,dst3))
img2 = np.hstack((dst1,img2))
img3 = np.hstack((dst4,dst5,dst6))
img4 = np.hstack((dst8,dst9,dst10))
img3 = np.vstack((img3,img4))
img3 = np.hstack((img3,black_white))
img3 = np.vstack((img2,img3))
cv2.imshow("20040420",img3)
cv2.waitKey()
cv2.destroyAllWindows()
图片的缩放,翻转,平移,旋转,及综合应用
最新推荐文章于 2023-06-22 22:05:01 发布