OpenCV 直方图和归一化

发布时间 2023-11-07 07:44:16作者: harrychinese

直方图可以反映图片的整体统计信息, 使用函数 CalcHist() 实现.
但CalcHist() 统计出的数量信息和图像大小相关, 如果要剔除图像大小因素, 需要做归一化处理, 归一化处理后的信息, 反映出各个颜色值得占比情况, 这样更方便不同size图像做对比, 归一化的函数为 Normalize().

        /// <summary>
        /// computes the joint dense histogram for a set of images.
        /// </summary>
        /// <param name="images">要统计直方图的Mat</param>
        /// <param name="channels">需要统计的通道Id, 为了理解方便, 一般仅统计一个通道</param>
        /// <param name="mask">掩码Mat, 一般传null</param>
        /// <param name="hist">统计后的hist mat</param>
        /// <param name="dims">输出直方图的维度, 灰度为1, 彩色为3</param>
        /// <param name="histSize">直方图横坐标的区间数, 即直方图每一维数组的大小</param>
        /// <param name="ranges">执直方图每个bin上校浮动的数值范围</param>
        /// <param name="uniform">直方图是否均匀, 一般取值为true</param>
        /// <param name="accumulate">累计标志, 多次进行直方图统计时是否需要累计, 一般取值为false</param>
        public static void CalcHist(Mat[] images, 
            int[] channels, InputArray? mask,
            OutputArray hist, int dims, int[] histSize,
            Rangef[] ranges, bool uniform = true, bool accumulate = false)
        {}  
        /// <summary>
        /// scales and shifts array elements so that either the specified norm (alpha) 
        /// or the minimum (alpha) and maximum (beta) array values get the specified values
        /// </summary>
        /// <param name="src">The source array</param>
        /// <param name="dst">The destination array; will have the same size as src</param>
        /// <param name="alpha">The norm value to normalize to or the lower range boundary 
        /// in the case of range normalization</param>
        /// <param name="beta">The upper range boundary in the case of range normalization; 
        /// not used for norm normalization</param>
        /// <param name="normType">The normalization type</param>
        /// <param name="dtype">When the parameter is negative, 
        /// the destination array will have the same type as src, 
        /// otherwise it will have the same number of channels as src and the depth =CV_MAT_DEPTH(rtype)</param>
        /// <param name="mask">The optional operation mask</param>
        public static void Normalize(InputArray src, InputOutputArray dst, double alpha = 1, double beta = 0,
            NormTypes normType = NormTypes.L2, int dtype = -1, InputArray? mask = null)

参考:

https://zhuanlan.zhihu.com/p/258118645