博客
关于我
008 对比度调整-直方图均衡
阅读量:677 次
发布时间:2019-03-15

本文共 2325 字,大约阅读时间需要 7 分钟。

介绍

YUV:Y分量确定颜色的亮度(称为亮度或亮度),而U和V分量确定颜色本身(色度)

在这里插入图片描述
YUV分通道显示

import cv2 as cvimport matplotlib.pyplot as pltimg = cv.imread('../images/scene_001.jpg')yuv = cv.cvtColor(img, cv.COLOR_BGR2YUV)y_img = yuv.copy()y_const = 255y_img[:, :, 1] = y_consty_img[:, :, 2] = y_constu_img = yuv.copy()u_img[:, :, 0] = y_constu_img[:, :, 2] = y_constv_img = yuv.copy()v_img[:, :, 0] = y_constv_img[:, :, 1] = y_constfig, ax = plt.subplots(2, 2, figsize=(8, 8))ax[0][0].set_title('origin')ax[0][0].imshow(cv.cvtColor(yuv, cv.COLOR_YUV2RGB))ax[0][1].set_title('y channel')ax[0][1].imshow(y_img[:, :, 0], cmap='gray')ax[1][0].set_title('u channel(y=' + str(y_const) + ')')ax[1][0].imshow(cv.cvtColor(u_img, cv.COLOR_YUV2RGB))ax[1][1].set_title('v channel(y=' + str(y_const) + ')')ax[1][1].imshow(cv.cvtColor(v_img, cv.COLOR_YUV2RGB))[axi.axis('off') for axi in ax.ravel()]plt.show()

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
将RGB转成YUV,直方图均衡化Y,重新合成图片

代码实现(Python)

import cv2 as cvimport numpy as npimport matplotlib.pyplot as pltimg = cv.imread('../images/scene_001.jpg')yuv = cv.cvtColor(img, cv.COLOR_BGR2YUV)hist_size = 256"""calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist"""y_hist = np.array(cv.calcHist(img, [0], None, [hist_size], [0, hist_size - 1])).reshape(hist_size)fig, ax = plt.subplots(5, 2, figsize=(8, 8))ax[0][0].set_title('origin')ax[0][0].imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))ax[0][0].axis('off')ax[0][1].set_title('y hist')ax[0][1].hist(y_hist, 50)# Equalize histdef equalize(adjust_ratio, ii):    rows, cols, _ = img.shape    y_hist_equal = y_hist    total = 0    for i in range(len(y_hist)):        total += y_hist[i]        tmp = total * 255 / rows / cols        y_hist_equal[i] = int(min(max((tmp - i) * adjust_ratio + i, 0), 255))    yuv_equal = yuv.copy()    for row in yuv_equal:        for col in row:            col[0] = y_hist_equal[int(col[0])]    y_hist_equal = np.array(cv.calcHist(yuv_equal, [0], None, [hist_size], [0, hist_size - 1])).reshape(hist_size)    ax[ii][0].set_title('Equalize(ratio=' + str(adjust_ratio) + ')')    ax[ii][0].imshow(cv.cvtColor(yuv_equal, cv.COLOR_YUV2RGB))    ax[ii][0].axis('off')    ax[ii][1].set_title('Equalized y hist')    ax[ii][1].hist(y_hist_equal, 50)equalize(0, 1)equalize(0.33, 2)equalize(0.66, 3)equalize(1, 4)plt.show()

所谓的直方图均衡化,其实就是让y值多的点越来越多,y值少的点越来越少,穷者越穷,富者越富,对比度就会越来越大,区别就越大

在这里插入图片描述
在这里插入图片描述

转载地址:http://zhfqz.baihongyu.com/

你可能感兴趣的文章
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>