博客
关于我
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中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>
MySql中给视图添加注释怎么添加_默认不支持_可以这样取巧---MySql工作笔记002
查看>>
Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
查看>>
Mysql中视图的使用以及常见运算符的使用示例和优先级
查看>>
Mysql中触发器的使用示例
查看>>
Mysql中设置只允许指定ip能连接访问(可视化工具的方式)
查看>>
mysql中还有窗口函数?这是什么东西?
查看>>
mysql中间件
查看>>