博客
关于我
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处理千万级数据分页查询的优化方案
查看>>
mysql备份
查看>>
mysql备份与恢复
查看>>
mysql备份工具xtrabackup
查看>>
mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
查看>>
mysql复制内容到一张新表
查看>>
mysql复制表结构和数据
查看>>
mysql复杂查询,优质题目
查看>>
MySQL外键约束
查看>>
MySQL多表关联on和where速度对比实测谁更快
查看>>
MySQL多表左右连接查询
查看>>
mysql大批量删除(修改)The total number of locks exceeds the lock table size 错误的解决办法
查看>>
mysql如何做到存在就更新不存就插入_MySQL 索引及优化实战(二)
查看>>
mysql如何删除数据表,被关联的数据表如何删除呢
查看>>
MySQL如何实现ACID ?
查看>>
mysql如何记录数据库响应时间
查看>>
MySQL子查询
查看>>
Mysql字段、索引操作
查看>>
mysql字段的细节(查询自定义的字段[意义-行列转置];UNION ALL;case-when)
查看>>
mysql字段类型不一致导致的索引失效
查看>>