將影像左右鏡像翻轉,生成翻轉后的影像
python3代碼
import numpy as np
import cv2
import matplotlib.pyplot as plt
#圖片鏡像翻轉
#多圖必須是同寬的
#多張圖片命名方式必須是1.png,2.png,...,n.png等格式,與代碼在同目錄下
#引數為多少張圖片
def left2right(img_path):
im=cv2.imread(img_path)
h,w,_=im.shape
result=np.zeros(im.shape,dtype=np.uint8)
for i in range(h):
for j in range(w):
#每一行倒序翻轉
result[i][j]=im[i][np.abs(j-w+1)]
return result
def main():
img_path1='./1.jpg'
#保存圖片路徑
img_path2='./result'+img_path1.split('/')[1]
#得到鏡像翻轉后的圖片資料
result=left2right(img_path1)
#保存為圖片
cv2.imwrite(img_path2,result)
#結果展示
#opencv的顏色通道順序為[B,G,R],而matplotlib顏色通道順序為[R,G,B],所以需要調換一下通道位置
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文亂碼
#子圖1,原始影像
plt.subplot(121)
#imshow()對影像進行處理,畫出影像,show()進行影像顯示
plt.imshow(cv2.imread(img_path1)[:,:,(2,1,0)])
plt.title('原圖')
#不顯示坐標軸
plt.axis('off')
#子圖2,鏡像翻轉后影像
plt.subplot(122)
plt.imshow(result[:,:,(2,1,0)])
plt.title('鏡像翻轉后影像')
plt.axis('off')
plt.show()
if __name__ =='__main__':
main()
實驗結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/379459.html
標籤:其他
