• OpenCV处理直方图


    直方图可以用来描述各种不同的事物,如物体的色彩分布、物体边缘梯度模板,以及表示目标位置的当前假设。

    简单的说,直方图就是对数据进行统计,将统计值组织到一系列事先定义好的bin中。bin中的数值是从数据中计算出特征的统计量,这些数据可以是诸如梯度、方向、色彩或者任何其他特征。无论如何,直方图获得的是数据分布的统计图。通常直方图的维数要低于原始数据。

    ../../../../../_images/Histogram_Calculation_Theory_Hist1.jpg

    具体可参见:

    http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html#histogram-calculation

    下面参考《Learning OpenCV》一个例子。根据输入的图像计算出一个色相饱和度的2维直方图。

    /**
      *    file:参考《learning OpenCV》P227
      *    author: Jacky_Liu
      *    date:   2013-12-06
      */
    
    #include <QtCore/QCoreApplication>
    #include <cv.h>
    #include <highgui.h>
    
    int main(int argc, char *argv[])
    {
    //    QCoreApplication a(argc, argv);
    //    return a.exec();
    
        IplImage *src = NULL;
    
        if(argc != 2 || (src = cvLoadImage(argv[1], 1)) == 0)
        {
            printf("The number of the arguments is wrong, or the fail to load image.");
            return 0;
        }
    
        //转换颜色空间
        IplImage *hsv = cvCreateImage(cvGetSize(src), 8, 3);
        cvCvtColor(src, hsv, CV_BGR2HSV);
    
        //分割到3个独立通道的图像
        IplImage *h_plane = cvCreateImage( cvGetSize(src), 8, 1);
        IplImage *s_plane = cvCreateImage( cvGetSize(src), 8, 1);
        IplImage *v_plane = cvCreateImage( cvGetSize(src), 8, 1);
    
        IplImage *planes[] = {h_plane, s_plane};
        cvCvtPixToPlane(hsv, h_plane, s_plane, v_plane, 0);
    
        //建立直方图结构,并计算
        int h_bins = 30, s_bins = 32;
        CvHistogram *hist = NULL;
    
        //数组每一个元素对应直方图对应维数的bin的个数
        int hist_size[] = {h_bins, s_bins};
        //hue范围[0,180]
        float h_ranges[] = {0, 180};
        float s_ranges[] = {0, 255};
        float *ranges[] = {h_ranges, s_ranges};
    
        hist = cvCreateHist(2,                      //直方图维数为2维
                            hist_size,              //直方图每一维对应的bin数
                            CV_HIST_ARRAY,          //稠密矩阵存储
                            ranges,                 //直方图每一维的维数
                            1);                     //均匀直方图
        //计算直方图
        cvCalcHist(planes, hist, 0, 0);
    
        //显示2维直方图
        int scale = 20;
        IplImage *hist_img = cvCreateImage(cvSize(h_bins * scale, s_bins * scale),
                                           8, 3);
        cvZero( hist_img );
    
        float max_value = 0;
        cvGetMinMaxHistValue(hist, 0, &max_value, 0, 0);
    
        for(int h = 0; h < h_bins; h++)
        {
            for(int s = 0; s < s_bins; s++)
            {
                //获取bin对应的最大值
                float bin_val = cvQueryHistValue_2D(hist, h, s);
                //颜色归一化到[0,255]显示
                int intensity = cvRound( bin_val * 255 / max_value);
                //显示
                cvRectangle(
                            hist_img,
                            cvPoint( h*scale, s*scale),
                            cvPoint( (h+1)*scale - 1, (s+1)*scale - 1),
                            CV_RGB(intensity,intensity, intensity),
                            CV_FILLED
                            );
            }
        }
        cvNamedWindow("Source", 1);
        cvShowImage("Source", src);
    
        cvNamedWindow("H-S Histogram", 1);
        cvShowImage("H-S Histogram", hist_img);
        cvWaitKey(0);
    
        return 0;
    }

    当你心中只有一个目标时,全世界都会给你让路!Read more! Write more! Practise more! 新浪微博:liu_军
  • 相关阅读:
    网段
    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    不利不理左卫门的吃拿卡要
    webAppRootKey
    011.Python的列表的相关操作
    010.Python字符串的格式化
    009.Python字符串相关函数
    008.Python循环for循环
    007.Python循环语句while循环嵌套
    006.Python循环语句while循环
  • 原文地址:https://www.cnblogs.com/liu-jun/p/3461631.html
Copyright © 2020-2023  润新知