'''
What are histograms?
Histograms are collected counts of data organized into a set of predefined bins
When we say data we are not restricting it to be intensity values (as we saw in the previous Tutorial Histogram Equalization).
The data collected can be whatever feature you find useful to describe your image.
Let's see an example. Imagine that a Matrix contains information of an image (i.e. intensity in the range 0−255):
What happens if we want to count this data in an organized way? Since we know that the range of information value for this case is 256 values,
we can segment our range in subparts (called bins) like:
[0,255]=[0,15]∪[16,31]∪....∪[240,255]range=bin1∪bin2∪....∪binn=15
and we can keep count of the number of pixels that fall in the range of each bin_i. Applying this to the example
above we get the image below ( axis x represents the bins and axis y the number of pixels in each of them).
这里其实做histogram计算,并不一定一定是某个值,通常是用像素值。但是也不是一定要用像素值。
在对像素值进行统计时,如果是灰度图,因为是单通道的,所以相对简单。但是如果是多通道的,在计算histogram时,需要指定是某个通道。
其实也好理解,histogram就是直方图,直方图就可以统计任何值的直方图分布,就是统计一下这个指标的值分布区域范围。这在统计领域常见,
图像上能看到的值,最底层的就是像素值,所以可以用像素值。
''' '''
void cv::calcHist ( const Mat * images,
int nimages,
const int * channels,
InputArray mask,
OutputArray hist,
int dims,
const int * histSize,
const float ** ranges,
bool uniform = true,
bool accumulate = false
)
Python:
cv.calcHist( images, channels, mask, histSize, ranges[, hist[, accumulate]] ) -> hist
从函数定义可以看到,calcHist是要区分channel的。所以常常需要用到split这个函数,把图像按照channel先进行分开,然后每个通道进行histogram的计算。
'''
'''
根据官方的解释,下面的代码分为三个步骤,每个函数都可以望文生义。split最简单,就是按照channel进行拆分,把图像分成三个通道的。
而后是calcHist计算直方图,而后是normalize进行归一化。
Use the OpenCV function cv::split to divide an image into its correspondent planes.
To calculate histograms of arrays of images by using the OpenCV function cv::calcHist
To normalize an array by using the function cv::normalize
'''
# 彩色图分别计算各个通道的histogram
import cv2 as cv
import numpy as np
# import argparse
# parser = argparse.ArgumentParser(description='Code for Histogram Calculation tutorial.')
# parser.add_argument('--input', help='Path to input image.', default='lena.jpg')
# args = parser.parse_args()
# src = cv.imread('./data/lena.jpg')
# if src is None:
# print('Could not open or find the image:')
# exit(0) # bgr_planes = cv.split(src)
# histSize = 256
# histRange = (0, 256)
# accumulate = False
# b_hist = cv.calcHist(bgr_planes, [0], None, [histSize], histRange, accumulate=accumulate)
# g_hist = cv.calcHist(bgr_planes, [1], None, [histSize], histRange, accumulate=accumulate)
# r_hist = cv.calcHist(bgr_planes, [2], None, [histSize], histRange, accumulate=accumulate)
# hist_w = 512
# hist_h = 400
# bin_w = int(round( hist_w/histSize ))
# histImage = np.zeros((hist_h, hist_w, 3), dtype=np.uint8)
# cv.normalize(b_hist, b_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
# cv.normalize(g_hist, g_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
# cv.normalize(r_hist, r_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
# for i in range(1, histSize):
# cv.line(histImage, ( bin_w*(i-1), hist_h - int(b_hist[i-1]) ),
# ( bin_w*(i), hist_h - int(b_hist[i]) ),
# ( 255, 0, 0), thickness=2)
# cv.line(histImage, ( bin_w*(i-1), hist_h - int(g_hist[i-1]) ),
# ( bin_w*(i), hist_h - int(g_hist[i]) ),
# ( 0, 255, 0), thickness=2)
# cv.line(histImage, ( bin_w*(i-1), hist_h - int(r_hist[i-1]) ),
# ( bin_w*(i), hist_h - int(r_hist[i]) ),
# ( 0, 0, 255), thickness=2) # cv.imshow('Source image', src)
# cv.imshow('calcHist Demo', histImage)
# cv.waitKey() '''
equalizeHist(img)
What is Histogram Equalization?
It is a method that improves the contrast in an image, in order to stretch out the intensity range
(see also the corresponding Wikipedia entry).
直方图均衡化,其实就是让图像的像素在各个值段都有出现,这样才能形成强烈对比,不能全是某个值段的值。 void cv::equalizeHist ( InputArray src,
OutputArray dst
)
Python:
cv.equalizeHist( src[, dst] ) -> dst '''
import cv2 as cv
src = cv.imread('./data/lena.jpg')
src = cv.cvtColor(src, cv.COLOR_BGR2GRAY) #转成灰度图,对单通道进行直方图计算,方便对比。
histSize = 256
histRange = (0, 256)
accumulate = False
hist = cv.calcHist(src, [0], None, [histSize], histRange, accumulate=accumulate)
hist_h =hist_w = 512
cv.normalize(hist, hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
histImage = np.zeros((hist_h, hist_w, 3), dtype=np.uint8)
bin_w = int(round( hist_w/histSize ))
for i in range(1, histSize):
cv.line(histImage, ( bin_w*(i-1), hist_h - int(hist[i-1]) ),
( bin_w*(i), hist_h - int(hist[i]) ),
( 255, 0, 0), thickness=2) # 均衡化直方图
dst = cv.equalizeHist(src)
#####----------------------------------
###compute the histgoram after equalizehist.
#####----------------------------------
hist_2 = cv.calcHist(dst, [0], None, [histSize], histRange, accumulate=accumulate)
cv.normalize(hist_2, hist_2, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
histImage_2 = np.zeros((hist_h, hist_w, 3), dtype=np.uint8)
# 在图上画出来
for i in range(1, histSize):
cv.line(histImage_2, ( bin_w*(i-1), hist_h - int(hist_2[i-1]) ),
( bin_w*(i), hist_h - int(hist_2[i]) ),
( 255, 0, 0), thickness=2) cv.imshow('calcHist Demo', histImage)
cv.imshow('calcHist Demo_2', histImage_2) #此时的的histogram分布要比前面均匀多了,不像前面的像素值集中分布在了某一个区间内,图像上每个点的像素缺少区分度。
cv.imshow('Source image', src)
cv.imshow('Equalized Image', dst)
cv.waitKey() # import cv2 as cv # src = cv.imread('./data/lena.jpg')
# dst = src.copy()
# dst[:,:,0] = cv.equalizeHist(src[:,:,0])
# dst[:,:,1] = cv.equalizeHist(src[:,:,1])
# dst[:,:,2] = cv.equalizeHist(src[:,:,2])
# cv.imshow('Source image', src)
# cv.imshow('Equalized Image', dst)
# cv.waitKey()

  

opencv equalizeHist的更多相关文章

  1. opencv —— equalizeHist 直方图均衡化实现对比度增强

    直方图均匀化简介 从这张未经处理的灰度图可以看出,其灰度集中在非常小的一个范围内.这就导致了图片的强弱对比不强烈. 直方图均衡化的目的,就是把原始的直方图变换为在整个灰度范围(0~255)内均匀分布的 ...

  2. (二)OpenCV-Python学习—对比度增强

    ·对于部分图像,会出现整体较暗或较亮的情况,这是由于图片的灰度值范围较小,即对比度低.实际应用中,通过绘制图片的灰度直方图,可以很明显的判断图片的灰度值分布,区分其对比度高低.对于对比度较低的图片,可 ...

  3. 利用html5、websocket和opencv实现人脸检测

    最近学习人脸识别相关的东西,在MFC下使用OpenCV做了一个简单的应用.训练需要较多的数据,windows应用程序终究还是不方便,于是想着做成CS模式:检测识别都放在服务器端,视频获取和显示都放在网 ...

  4. OpenCV 人脸识别 C++实例代码

    #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include & ...

  5. atitit  opencv apiattilax总结 约500个函数 .xlsx

    atitit  opencv apiattilax总结 约500个函数 .xlsx 1.1. CxCore中文参考手册 1 1.2. 机器学习中文参考手册  knn  svm  1 1.3. CvAu ...

  6. OpenCV成长之路(4):图像直方图

    一.图像直方图的概念 图像直方图是反映一个图像像素分布的统计表,其实横坐标代表了图像像素的种类,可以是灰度的,也可以是彩色的.纵坐标代表了每一种颜色值在图像中的像素总数或者占所有像素个数的百分比. 图 ...

  7. OpenCV人脸检测demo--facedetect

    &1 问题来源 在运行官网的facedetect这个demo的时候,总是不会出来result的图形,电脑右下角提示的错误是“显示器驱动程序已停止响应,而且已恢复 windows 8(R)”. ...

  8. opencv 61篇

    (一)--安装配置.第一个程序 标签: imagebuildincludeinputpathcmd 2011-10-21 16:16 41132人阅读 评论(50) 收藏 举报  分类: OpenCV ...

  9. Opencv 3入门(毛星云)摘要

    第一章 环境搭建: 1.  环境变量path 添加  D:\Program Files\opencv\build\x86\vc11\bin 2.  VS在VC++项目中,属性管理器\属性. VC++目 ...

  10. opencv 人脸识别 (一)训练样本的处理

    本文实现基于eigenface的人脸检测与识别.给定一个图像数据库,进行以下步骤: 进行人脸检测,将检测出的人脸存入数据库2 对数据库2进行人脸建模 在测试集上进行recognition   本篇实现 ...

随机推荐

  1. 疑难杂症(已解决) | 为什么出现python中tkinter创建界面需要一闪而过才进入主窗口?

    一.具体问题 如图所示,我编写了一个主窗口的程序(如下所示,有兴趣的可以自己复制后运行),发现需要先进入第一个窗口再进入主界面,这不符合逻辑. 代码区域(完整代码): from tkinter imp ...

  2. 重磅集结!CNCF/VMware/PingCAP/网易数帆/阿里云联合出品云原生生态大会

    "云原生(Cloud Native)"这个词在2020年刷屏了.在企业积极进行数字化转型,全面提升效率的今天,云原生被认为是云计算的"下一个时代". 12月16 ...

  3. Java实现快速快速排序算法

    算法简介 快速排序(Quick Sort) 是由冒泡排序改进而得的.在冒泡排序过程中,只对相邻的两个记录进行比较,因此每次交换两个相邻记录时只能消除一个逆序.如果能通过两个(不相邻)记录的一次交换直接 ...

  4. [oeasy]python0023_[趣味拓展]Guido的简历_从ABC到python

    Guido的简历 回忆上次内容 上次 添加了 各种 符号 铭文 各种 颜色 铸造了 自己的宝剑       添加图片注释,不超过 140 字(可选)   这些都是 用python画出来的宝剑   py ...

  5. njs最详细的入门手册:Nginx JavaScript Engine

    原文链接:https://hi.imzlh.top/2024/07/08.cgi 关于njs 首先,njs似乎在国内外都不受关注,资料什么的只有 官网参考手册,出了个问题只能看到Github Issu ...

  6. 第十节 JMeter基础-初级购物车【接口关联-鉴权】

    声明:本文所记录的仅本次操作学习到的知识点,其中商城IP错误,请自行更改. 背景:商城购物车可以添加数据,也可以删除数据. 思路: 登录后添加购物车,加入成功后查看购物车列表. 购物车列表,随机删除, ...

  7. 假期小结3Hadoop学习

    学习Hadoop是一个很好的选择,因为它是大数据处理和分析领域最流行的框架之一.Hadoop提供了可靠.可扩展的分布式数据处理能力,适用于处理大规模数据和构建可靠的数据管道. 在学习Hadoop时,以 ...

  8. 映射lun

    环境 VMware1 网卡与 主机ping通 创建文件夹将文件解压进去 删除压缩包 创建虚拟机 下一步下一步,完成虚拟机 编辑虚拟机设置 否 账号密码 admin Admin@storage 等待 导 ...

  9. 【Java】三元运算符 类型提升 问题

    代码片段: @Test public void test() { Object o = true ? new Integer(1) : new Double(2); System.out.printl ...

  10. 【Kafka】02 原生集群部署

    基于大数据教程的环境: 192.168.101.11 centos7-01 192.168.101.12 centos7-02 192.168.101.13 centos7-03 搭建Kafka环境需 ...