Python+OpenCV+imutils的简单图片处理(放缩、翻转、旋转、灰度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
hxp.png放大:T_big.jpgT_big.jpg缩小:T_small.jpg
T_small.jpg旋转:T_rotate.jpgT_rotate.jpgRGB:RGB灰度:gray翻转:LRUDLRUD

四、结语

这次的分享到此结束,初次学习,欢迎在评论区交流学习!如果有帮助的话记得一键三连哦

猜你喜欢

转载自blog.csdn.net/weixin_42464904/article/details/112209545
今日推荐