• opencv绘制灰度直方图


    代码之一:

    #include <cv.h>
    #include <highgui.h>
    #pragma comment( lib, "cv.lib" )
    #pragma comment( lib, "cxcore.lib" )
    #pragma comment( lib, "highgui.lib" )
    int main()
    {
             IplImage* src=cvLoadImage("lena.jpg",0);
             int width=src->width;
             int height=src->height;
             int step=src->widthStep;
             uchar* data=(uchar *)src->imageData;
             int hist[256]={0};
             for(int i=0;i<height;i++)
             {
                       for(int j=0;j<width;j++)
                       {
                                hist[data[i*step+j]]++;
                       }
             }
             int max=0;
             for(i=0;i<256;i++)
             {
                       if(hist[i]>max)
                       {
                                max=hist[i];
                       }
             }
             IplImage* dst=cvCreateImage(cvSize(400,300),8,3);
             cvSet(dst,cvScalarAll(255),0);
             double bin_width=(double)dst->width/256;
             double bin_unith=(double)dst->height/max;
             for(i=0;i<256;i++)
             {
                       CvPoint p0=cvPoint(i*bin_width,dst->height);
                       CvPoint p1=cvPoint((i+1)*bin_width,dst->height-hist[i]*bin_unith);
                       cvRectangle(dst,p0,p1,cvScalar(0,255),-1,8,0);
             }
             cvNamedWindow("src",1);
             cvShowImage("src",src);
             cvNamedWindow("dst",1);
             cvShowImage("dst",dst);
             cvWaitKey(0);
             cvDestroyAllWindows();
             cvReleaseImage(&src);
             cvReleaseImage(&dst);
             return 0;
    }

    代码之二 :

    #include <cv.h>
    #include <highgui.h>
    #pragma comment( lib, "cv.lib" )
    #pragma comment( lib, "cxcore.lib" )
    #pragma comment( lib, "highgui.lib" )
    int main()
    {
             IplImage* src=cvLoadImage("lena.jpg",0);
             int size=256;
             float range[]={0,255};
             float* ranges[]={range};
             CvHistogram* hist=cvCreateHist(1,&size,CV_HIST_ARRAY,ranges,1);
             cvCalcHist(&src,hist,0,NULL);
             float max=0;
             cvGetMinMaxHistValue(hist,NULL,&max,NULL,NULL);
             IplImage* dst=cvCreateImage(cvSize(400,300),8,3);
             cvSet(dst,cvScalarAll(255),0);
             double bin_width=(double)dst->width/size;
             double bin_unith=(double)dst->height/max;
             for(int i=0;i<size;i++)
             {
                       CvPoint p0=cvPoint(i*bin_width,dst->height);
                       CvPoint p1=cvPoint((i+1)*bin_width,dst->height-cvGetReal1D(hist->bins,i)*bin_unith);
                       cvRectangle(dst,p0,p1,cvScalar(0,255),-1,8,0);
             }
             cvNamedWindow("src",1);
             cvShowImage("src",src);
             cvNamedWindow("dst",1);
             cvShowImage("dst",dst);
             cvWaitKey(0);
             cvDestroyAllWindows();
             cvReleaseImage(&src);
             cvReleaseImage(&dst);
             return 0;
    }

    from: http://blog.csdn.net/abcjennifer/article/details/7315650

  • 相关阅读:
    Leetcode 493.翻转对
    Leetcode 491.递增子序列
    Leetcode 488.祖玛游戏
    Leetcode 486.预测赢家
    Leetcode 483.最小好进制
    Leetcode 482.密钥格式化
    商品期货投资的那些事(八)为你的套利头寸买个保险
    商品期货投资的那些事(七)趋势套利震荡投机、直道飘移弯道超越
    商品期货投资的那些事(五)做空焦煤焦炭?你是想跟发改委作对吗?
    商品期货投资的那些事(六)期现投资这行怎样才能赚大钱?
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/5374051.html
Copyright © 2020-2023  润新知