OPENCV(5) —— 图像直方图

新版本对直方图不再使用之前的histogram的形式,而是用统一的Mat或者MatND的格式来存储直方图,可见新版本Mat数据结构的优势。

C++: void calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, intdims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )

计算直方图

Parameters:

  • images – Source arrays. They all should have the same depth, CV_8U or CV_32F , and the same size. Each of them can have an arbitrary number of channels.

  • nimages – Number of source images.
  • channels – List of the dims channels used to compute the histogram. The first array channels are numerated from 0 to images[0].channels()-1 , the second array channels are counted fromimages[0].channels() to images[0].channels() + images[1].channels()-1, and so on.
  • mask – Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size asimages[i] . The non-zero mask elements mark the array elements counted in the histogram.
  • hist – Output histogram, which is a dense or sparse dims -dimensional array.
  • dims – Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS (equal to 32 in the current OpenCV version).
  • histSize – Array of histogram sizes in each dimension.
  • ranges – Array of the dims arrays of the histogram bin boundaries in each dimension. When the histogram is uniform ( uniform =true), then for each dimension i it is enough to specify the lower (inclusive) boundary of the 0-th histogram bin and the upper (exclusive) boundary for the last histogram bin histSize[i]-1 . That is, in case of a uniform histogram each ofranges[i] is an array of 2 elements. When the histogram is not uniform ( uniform=false ), then each of ranges[i] contains histSize[i]+1 elements: . The array elements, that are not between and , are not counted in the histogram.
  • uniform – Flag indicating whether the histogram is uniform or not (see above).
  • accumulate – Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated. This feature enables you to compute a single histogram from several sets of arrays, or to update the histogram in time.

void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, intshift=0)

画矩形

#include "stdafx.h"

#include <cv.h>
#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )
{
    Mat src, hsv;

    /* if( argc != 2 || !(src=imread(argv[1], 1)).data )
        return -1; */

    src=imread("zhang.jpg", 1);

    cvtColor(src, hsv, CV_BGR2HSV);

    // Quantize the hue to 30 levels
    // and the saturation to 32 levels
    int hbins = 30, sbins = 32;        // bin 步长

    int histSize[] = {hbins, sbins};
    // hue varies from 0 to 179, see cvtColor
    float hranges[] = { 0, 180 };
    // saturation varies from 0 (black-gray-white) to
    // 255 (pure spectrum color)
    float sranges[] = { 0, 256 };
    const float* ranges[] = { hranges, sranges };
    MatND hist;
    // we compute the histogram from the 0-th and 1-st channels
    int channels[] = {0, 1};   // --- hue && saturation

    calcHist( &hsv, 1, channels, Mat(), // do not use mask
        hist, 2, histSize, ranges,
        true, // the histogram is uniform
        false );
    double maxVal=0;
    minMaxLoc(hist, 0, &maxVal, 0, 0);  // Finds the global minimum and maximum in an array.
        // void minMaxLoc(InputArray src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray())

    // 直方图显示
    int scale = 10;
    Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);

    for( int h = 0; h < hbins; h++ )
        for( int s = 0; s < sbins; s++ )
        {
            float binVal = hist.at<float>(h, s);
            int intensity = cvRound(binVal*255/maxVal);
            rectangle( histImg, Point(h*scale, s*scale),
                Point( (h+1)*scale - 1, (s+1)*scale - 1),
                Scalar::all(intensity),    // 二维直方图,颜色之深浅代表出现个数之多寡
                CV_FILLED );
        }

        namedWindow( "Source", 1 );
        imshow( "Source", src );

        namedWindow( "H-S Histogram", 1 );
        imshow( "H-S Histogram", histImg );
        waitKey();
}

C++: void equalizeHist(InputArray src, OutputArray dst)

直方图均衡化

Parameters:

  • src – Source 8-bit single channel image.

  • dst – Destination image of the same size and type as src .

The function equalizes the histogram of the input image using the following algorithm:

  1. Calculate the histogram for src .

  2. Normalize the histogram so that the sum of histogram bins is 255.
  3. Compute the integral of the histogram:

  4. Transform the image using as a look-up table:

compareHist

double compareHist(const SparseMat& H1, const SparseMat& H2, int method)

直方图比较

Parameters:

  • H1 – First compared histogram.

  • H2 – Second compared histogram of the same size as H1 .
  • method

    Comparison method that could be one of the following:

    • CV_COMP_CORREL Correlation  相关性 相同为1,范围0<x<=1

    • CV_COMP_CHISQR Chi-Square   卡方 相同为0 [0,inf)
    • CV_COMP_INTERSECT Intersection   直方图交 ,数值越大越相似
    • CV_COMP_BHATTACHARYYA Bhattacharyya distance
    • CV_COMP_HELLINGER Synonym for CV_COMP_BHATTACHARYYA Bhattacharyya 距离,相同为0 [0,inf)

#include "stdafx.h"

#include <cv.h>
#include <highgui.h>
#include "stdio.h"

using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
    Mat src1, src2,dst;
    Mat hsv1,hsv2;
    MatND hist1,hist2;

    src1=imread("zhang.jpg", 1);
    src2=imread("zhou.jpg",1);
    cvtColor(src1,hsv1,CV_RGB2HSV);
    cvtColor(src2,hsv2,CV_RGB2HSV);

    int hbins=30,sbins=32;
    int histSize[]={hbins,sbins};

    float hranges[]={0,180};
    float sranges[]={0,256};
    const float* ranges[]={hranges,sranges};

    int channels[]={0,1};

    calcHist(&hsv1,1,channels,Mat(),hist1,2,histSize,ranges,true,false);
    calcHist(&hsv2,1,channels,Mat(),hist2,2,histSize,ranges,true,false);

    double temp;
    temp=compareHist(hist1,hist2,CV_COMP_CORREL);
    cout<<"CV_COMP_CORREL  "<<temp<<endl;

    temp=compareHist(hist1,hist2,CV_COMP_CHISQR);
    cout<<"CV_COMP_CHISQR  "<<temp<<endl;

    temp=compareHist(hist1,hist2,CV_COMP_INTERSECT);
    cout<<"CV_COMP_INTERSECT  "<<temp<<endl;

    temp=compareHist(hist1,hist2,CV_COMP_BHATTACHARYYA);
    cout<<"CV_COMP_BHATTACHARYYA  "<<temp<<endl;

    namedWindow("src1");
    imshow("src1",src1);

    namedWindow("src2");
    imshow("src2",src2);

    waitKey();

    cvDestroyAllWindows();
    return 0;
}

遇到 ~ 编译器错误 C2078

初始值设定项的数目超过了要初始化的对象数。

// C2078.cpp
int main() {
   int d[2] = {1, 2, 3};   // C2078
   int e[2] = {1, 2};   // OK

   char a[]={"a", "b"};   // C2078
   char *b[]={"a", "b"};   // OK
   char c[]={‘a‘, ‘b‘};   // OK
}
 
 
  • 时间: 2024-10-04 20:30:38

    OPENCV(5) —— 图像直方图的相关文章

    opencv:图像直方图均衡化

    // 直方图均衡化 Mat gray, dst; cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, dst); imshow("gray", gray); imshow("equalizeHist", dst); 原文地址:https://www.cnblogs.com/wbyixx/p/12246837.html

    opencv:图像直方图相似性比较

    void hist_compare(Mat src1, Mat src2) { int histSize[] = { 256, 256, 256 }; int channels[] = { 0, 1, 2 }; Mat hist1, hist2; float c1[] = { 0, 255 }; float c2[] = { 0, 255 }; float c3[] = { 0, 255 }; const float* histRanges[] = { c1, c2, c3 }; calcHis

    图像直方图均衡化增强opencv与C语言版

    本文实现彩色图像的全局直方图均衡.分别对R/G/B三通道均衡,读写图片采用OpenCV.代码如下: #include <opencv2/opencv.hpp> //#include <cv.h> //#include <cxcore.h> //#include <highgui.h> #include <time.h> #include <stdio.h> #include <math.h> #include "

    OpenCV成长之路:图像直方图

    http://ronny.blog.51cto.com/8801997/1394115 2014-04-11 13:47:27 标签:opencv 直方图 统计表 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ronny.blog.51cto.com/8801997/1394115 一.图像直方图的概念 图像直方图是反映一个图像像素分布的统计表,其实横坐标代表了图像像素的种类,可以是灰度的,也可以是彩色的.纵坐标代表了每一种

    OpenCV成长之路:图像直方图的应用

    2014-04-11 13:57:03 标签:opencv 图像 直方图 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ronny.blog.51cto.com/8801997/1394118 正如第4篇文章所说的图像直方图在特征提取方面有着很重要的作用,本文将举两个实际工程中非常实用的例子来说明图像直方图的应用. 一.直方图的反向映射. 我们以人脸检测举例,在人脸检测中,我们第一步往往需要先提取图像中皮肤区域来缩小人脸的检

    OpenCV入门教程之四 图像直方图

    一.图像直方图的概念 图像直方图是反映一个图像像素分布的统计表,其实横坐标代表了图像像素的种类,可以是灰度的,也可以是彩色的.纵坐标代表了每一种颜色值在图像中的像素总数或者占所有像素个数的百分比. 图像是由像素构成,因为反映像素分布的直方图往往可以作为图像一个很重要的特征.在实际工程中,图像直方图在特征提取.图像匹配等方面都有很好的应用.  二.利用OpenCV计算图像的直方图 OpenCV中计算图像直方图像函数是calcHist,它的参数比较多,下面分析一下它的接口和用法. void calc

    OpenCV入门教程之五 图像直方图的应用

    正如第4篇文章所说的图像直方图在特征提取方面有着很重要的作用,本文将举两个实际工程中非常实用的例子来说明图像直方图的应用. 一.直方图的反向映射 我们以人脸检测举例,在人脸检测中,我们第一步往往需要先提取图像中皮肤区域来缩小人脸的检测范围,这一般获得皮肤的颜色范围还需要定义阈值并不断的调整,实际中参数太多而不容易控制. 这里我们就可以考虑用直方图的反射映射. 1,收集人脸皮肤样本. 2,拼合样本并计算其颜色直方图. 3,将得到的样本颜色直方图反射映射到待检测的图片中,然后进行阈值化即可. 这里为

    opencv图像直方图均衡化及其原理

    直方图均衡化是什么有什么用 先说什么是直方图均衡化,通俗的说,以灰度图为例,原图的某一个像素为x,经过某个函数变为y.形成新的图.新的图的灰度值的分布是均匀的,这个过程就叫直方图均衡化. 图像直方图均衡化作用:用来增强对比度. 这种方法通常用来增加许多图像的全局对比度,尤其是当图像的有用数据的对比度相当接近的时候.通过这种方法,亮度可以更好地在直方图上分布.这样就可以用于增强局部的对比度而不影响整体的对比度,直方图均衡化通过有效地扩展常用的亮度来实现这种功能. 这种方法对于背景和前景都太亮或者太

    opencv python:图像直方图 histogram

    直接用matplotlib画出直方图 def plot_demo(image): plt.hist(image.ravel(), 256, [0, 256]) # image.ravel()将图像展开,256为bins数量,[0, 256]为范围 plt.show() 图像直方图 def image_hist(image): color = ('blue', 'green', 'red') for i, color in enumerate(color): # 计算出直方图,calcHist(i