博客
关于我
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/

你可能感兴趣的文章
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>