Python使用scikit-image,opencv等库调整图片亮度和颜色空间/色调

一、调整图片亮度:

加载需要的包:

from skimage import data,exposure,img_as_float
from PIL import Image,ImageFilter

使用pillow.Image加载图片之后需要转换格式才能应用函数:

加载图片,转换格式(这个函数只能处理numpy数组格式所以可以使用下面函数转换,也可以转换成numpy数组格式):

figure=Image.open(path)  #读取图片
image=img_as_float(figure)   #转换成指定格式

调整图片明亮度:

exposure.adjust_gamma(image,系数)   当系数大于一是图像变暗,图像小于一时图像变亮
image=img_as_float(figure)
figure_adjust_low=exposure.adjust_gamma(image,2)   #图片调暗
figure_adjust_high=exposure.adjust_gamma(image,0.5)  #图片调亮
plt.figure()
plt.subplot(311)
plt.imshow(figure_adjust_low)
plt.subplot(312)
plt.imshow(figure_adjust_high)
plt.subplot(313)
plt.imshow(figure)
plt.show()

效果如下所示:

二、转换图片颜色空间/色调

1.使用opencv(opencv处理后的图片数据为0-255更方便处理,scikit-image处理后数据不方便初始化)

  • 将RGB图片转换为HSV图片

import cv2
figure=Image.open(path)
figure=np.array(figure)          #opencv无法处理Image的格式需要转换为numpy格式
figure_pre=cv2.cvtColor(figure,cv2.COLOR_RGB2HSV)

效果如下:

  • 将RGB转换为HLS

figure=Image.open(path)
figure=np.array(figure)
figure_pre=cv2.cvtColor(figure,cv2.COLOR_RGB2HLS)

plt.figure()
plt.subplot(211)
plt.imshow(figure)
plt.subplot(212)
plt.imshow(figure_pre)
plt.show()

效果如下:

将RGB转换为LUV

figure=Image.open(path)
figure=np.array(figure)
figure_pre=cv2.cvtColor(figure,cv2.COLOR_RGB2LUV)

效果如下:

其他转换空间方法:

2.使用scikit_image

  • 一个公式用于多种情况:

其中colorspace可以是下述其中之一

['hsv', 'xyz', 'rgb cie', 'yiq', 'ydbdr', 'rgb', 'yuv', 'ypbpr', 'ycbcr']
image=np.array(figure)
figure_adjust=color.convert_colorspace(image,'RGB','HSV')    #其中RGB为初始图像空间,HSV为需 
                                                             #要转换的空间 如:HSV,GREY,LAB
plt.figure()
plt.subplot(211)
plt.imshow(figure_adjust)
plt.subplot(212)
plt.imshow(figure)
plt.show()

效果如下:

  • 将rgb转换为gray:

from skimage import color
image=np.array(figure)
figure_adjust=color.rgb2gray(image)           #转换函数
plt.figure()
plt.subplot(211)
plt.imshow(figure_adjust)
plt.subplot(212)
plt.imshow(figure)
plt.show()

效果如下:

  • rgb转换为hsv:

image=np.array(figure)
figure_adjust=color.rgb2hsv(image)     #转换公式
plt.figure()
plt.subplot(211)
plt.imshow(figure_adjust)
plt.subplot(212)
plt.imshow(figure)
plt.show()

  • rgb转换为lab:

image=np.array(figure)
figure_adjust=color.rgb2lab(image)  #转换公式
plt.figure()
plt.subplot(211)
plt.imshow(figure_adjust)
plt.subplot(212)
plt.imshow(figure)
plt.show()

效果如下:

 

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值