首页 > 其他分享 >opencv equalizeHist

opencv equalizeHist

时间:2022-10-16 19:47:03浏览次数:41  
标签:src int histSize hist opencv equalizeHist accumulate cv

'''
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()

  

标签:src,int,histSize,hist,opencv,equalizeHist,accumulate,cv
From: https://www.cnblogs.com/jianyingzhou/p/16796882.html

相关文章

  • opencv-python人脸识别
    scaleFactor、ninNeighbors的动态调整按键:“+”“-”“<”“>”,“q”退出!importcv2print("hello")#camera=cv2.VideoCapture('./腾讯云.mp4')camera=cv2.VideoCaptu......
  • 实战 | 用Python和OpenCV搭建一个老人跌倒智能监测系统 (步骤 + 源码)
    导读     本文将使用Python、OpenCV和MediaPipe搭建一个老人跌倒智能监测系统。背景介绍   老人监测系统是一种智能检测系统,可以检测老人是否躺在床上或是否跌倒......
  • python+selenium+opencv验证滑块
    我们在使用selenium爬虫的时候在登录时经常会遇到滑块验证码问题,导致登录受阻,正所谓万事开头难。登录就登录不进去更别提往后的操作的。今天以登录京东后台来演示下如何破......
  • C++ openCV 相关
    1.opencv的Mat矩阵Mat是opencv在C++中的一个图像容器类,可以使用Mat进行图像矩阵的定义Mat矩阵的定义#include<iostream>#include<opencv2/opencv.hpp>//定义图像......
  • 安装和调用opencv清楚又明白,只看这一篇就够了!
    现在很多人都要学习opencv,但是网上安装教程五花八门,有的还需要安装VS(VisualStudio),搞得初学者一头雾水,因此这篇文章介绍最简单便捷的对opencv的安装,以及如何调用。笔者这......
  • opencv中sift函数的使用
    importcv2importnumpyasnpimg=cv2.imread("qqhuman.jpeg")#将图片转化为灰度图gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#得到特征点sift=cv2.SIFT_create()kp=sift.......
  • OpenCV时钟
    #NoEnv#Includeopencv_ahk_lib.ahkSendModeInputSetWorkingDir%A_ScriptDir%hOpencv:=DllCall("LoadLibrary","str","opencv_world455.dll","ptr")hOpencvCo......
  • AHK调用opencv(二十)opencv中的轮廓 – ahk_v2_beta3
    什么是轮廓?轮廓可以简单地解释为连接具有相同颜色或强度的所有连续点(沿边界)的曲线。轮廓是用于形状分析以及对象检测和识别的有用工具。为了获得更高的准确性,请使用......
  • AHK调用opencv(十八)Canny 边缘检测 – ahk_v2_beta3
    Canny边缘检测于1986年由JOHNCANNY首次在论文《AComputationalApproachtoEdgeDetection》中提出,就此拉开了Canny边缘检测算法的序幕。Canny边缘检测是从不同视觉对......
  • AHK调用opencv(十九)图像金字塔 – ahk_v2_beta3
    图像金字塔是指一组图像且不同分辨率的子图集合,它是图像多尺度表达的一种,以多分辨率来解释图像的结构,主要用于图像的分割或压缩。一幅图像的金字塔是一系列以金字塔性质排......