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

    xiaoxiao2022-07-05  186

    一、调整图片亮度:

    加载需要的包:

    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()

    效果如下:

     

    最新回复(0)