Python簡單影像處理
- 一、影像讀取與保存
- 需要的庫
- 讀取
- 保存
- 二、簡單地處理函式
- 縮放
- 繞中心旋轉
- RGB分離、灰度提取
- 翻轉
- 三、測驗
- 代碼
- 運行結果
- 四、結語
歡迎來到海小皮的CSDN博客,今天學習了python的簡單影像處理,在這里分享給大家,話不多說,直接上代碼,依然是注釋超詳細!
一、影像讀取與保存
需要的庫
使用pip到鏡像站安裝即可
import cv2 as cv
import imutils as imt
import numpy as np
讀取
road是路徑
orf_name是檔案名
temp_image = cv.imread(road+'\\'+orf_name)
保存
很好理解不多解釋
cv.imwrite(road+'\\'+svf_name,temp_image)
二、簡單地處理函式
縮放
'''
使用默認的雙線性插值進行影像縮放與保存
你可以修改resize的引數改用其他插值方式
INTER_NEAREST
最近鄰插值
INTER_LINEAR
雙線性插值(默認設定)
INTER_AREA
使用像素區域關系進行重采樣,
INTER_CUBIC
4x4像素鄰域的雙三次插值
INTER_LANCZOS4
8x8像素鄰域的Lanczos插值
# 路徑 str
# 源檔案名 str
# 要保存的檔案名 str
# 縮放倍數 float or int
'''
def H_resize_img(road,orf_name,svf_name,multip):
# 讀取并顯示圖片
temp_image = cv.imread(road+'\\'+orf_name)
cv.imshow(orf_name,temp_image)
cv.waitKey() # 回車繼續
# 讀取大小
x,y = temp_image.shape[0:2]
# 開始放縮
temp_image = cv.resize(temp_image,(int(y*multip),int(x*multip)))
# 顯示放縮結果
cv.imshow(svf_name,temp_image)
# 保存影像
cv.imwrite(road+'\\'+svf_name,temp_image)
cv.waitKey() # 回撤繼續
cv.destroyAllWindows()
繞中心旋轉
# 按照圖片的中心旋轉、顯示并保存
def H_Rotate_img(road,orf_name,svf_name,angle):
# 讀取圖片并顯示
temp_image = cv.imread(road+'\\'+orf_name)
cv.imshow(orf_name,temp_image)
cv.waitKey()
# 旋轉圖片
temp_image = imt.rotate_bound(temp_image,angle)
# 顯示旋轉圖片
cv.imshow(svf_name,temp_image)
cv.waitKey()
# 保存圖片
cv.imwrite(road+'\\'+svf_name,temp_image)
# 關閉視窗
cv.destroyAllWindows()
RGB分離、灰度提取
def H_RGB_select(road,orf_name,svf_name,channel):
# 讀取影像
temp_image = cv.imread(road+"\\"+orf_name)
# 生成用于補全三通道的0矩陣
zero_arr = np.zeros(temp_image.shape[0:2], np.uint8)
# 提取RGB通道值
if channel == 'b':
bc = temp_image[:,:,0]
temp_image = cv.merge([bc,zero_arr,zero_arr]) # 通道補全
elif channel == 'g':
gc = temp_image[:,:,1]
temp_image = cv.merge([zero_arr,gc,zero_arr])
elif channel == 'r':
rc = temp_image[:,:,2]
temp_image = cv.merge([zero_arr,zero_arr,rc])
elif channel == 'gray':
bc = temp_image[:,:,0]
gc = temp_image[:,:,1]
rc = temp_image[:,:,2]
temp_image = 0.114 * bc + 0.587 * gc + 0.299 * rc # 加權公式計算灰度
temp_image = temp_image.astype(np.uint8) # 把float轉為uint8_t型別
# 顯示
cv.imshow(svf_name,temp_image)
cv.waitKey()
# 保存圖片
cv.imwrite(road+'\\'+svf_name,temp_image)
# 關閉視窗
cv.destroyAllWindows()
翻轉
可以沿左右、上下、上下左右翻轉
# 圖片的翻轉
'''
@direc:
LR 左右翻轉
UD 上下翻轉
LRUD 上下左右翻轉
'''
def H_flip_img(road,orf_name,svf_name,direc):
# 讀取影像并顯示
temp_image = cv.imread(road+"\\"+orf_name)
cv.imshow(orf_name,temp_image)
cv.waitKey()
if direc == 'LR':
direc = 1
elif direc == 'UD':
direc = 0
elif direc == 'LRUD':
direc = -1
# 翻轉
temp_image = cv.flip(temp_image,direc)
cv.imshow(svf_name,temp_image)
cv.waitKey()
# 保存
cv.imwrite(road+'\\'+svf_name,temp_image)
# 關閉圖片
cv.destroyAllWindows()
三、測驗
代碼
# 縮小倍數
S_multip = 0.5
# 放大倍數
B_multip = 1.5
# 旋轉角度
R_angle = 45
# 圖片名
Im_name = 'hxp.png'
# 存放路徑
img_road = 'D:\Project_Python\AI_Work_5'
# 縮小
H_resize_img(img_road,Im_name,'T_small.jpg',S_multip)
# 放大
H_resize_img(img_road,Im_name,'T_big.jpg',B_multip)
# 旋轉
H_Rotate_img(img_road,Im_name,'T_rotate.jpg',R_angle)
# 翻轉
H_flip_img(img_road,Im_name,'T_flip_LR.jpg','LR')
H_flip_img(img_road,Im_name,'T_flip_UD.jpg','UD')
H_flip_img(img_road,Im_name,'T_flip_LRUD.jpg','LRUD')
# 提取RGB通道值和灰度圖
H_RGB_select(img_road,Im_name,'T_color_r.jpg','r')
H_RGB_select(img_road,Im_name,'T_color_g.jpg','g')
H_RGB_select(img_road,Im_name,'T_color_b.jpg','b')
H_RGB_select(img_road,Im_name,'T_color_gray.jpg','gray')
運行結果
原圖:hxp.png
放大:T_big.jpg
縮小:T_small.jpg
旋轉:T_rotate.jpg
RGB:

灰度:
翻轉:


四、結語
這次的分享到此結束,初次學習,歡迎在評論區交流學習!如果有幫助的話記得一鍵三連哦
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/244670.html
標籤:python
