opencv:直方图与匹配

一、直方图

1·直方图的基本结构

typedef struct CvHistogram
{
     int type;
     CvArr* bins;
     float thresh[CV_MAX_DIM][2];
     float** thresh2;
     CvMatND mat;
} CvHistogram;

2.创建一个直方图

CvHistogram* cvCreateHist(int dims,int* sizes,int type,float** ranges = NULL,int uniform = 1);
//相关函数
void cvSetHistBinRanges(CvHistogram* hist,float** ranges,int uniform = 1);
void cvClearHist(CvHistogram* hist);
void cvReleaseHist(CvHistogram** hist);
CvHistogram* cvMakeHistHeaderForArray(int dims,int* sizes,CvHistogram* hist,float* data,float** ranges = NULL,int uniform = 1);

3.访问直方图

double cvQueryHistValue_1D(CvHistogram* hist,int idx0);
double cvQueryHistValue_2D(CvHistogram* hist,int idx0,int idx1);
double cvQueryHistValue_3D(CvHistogram* hist,int idx0,int idx1,int idx2);
double cvQueryHistValue_ND(CvHistogram* hist,int idx0,int idxN);

float* cvGetHistValue_1D(CvHistogram* hist,int idx0);
float* cvGetHistValue_2D(CvHistogram* hist,int idx0,int idx1);
float* cvGetHistValue_3D(CvHistogram* hist,int idx0,int idx1,int idx2);
float* cvGetHistValue_ND(CvHistogram* hist,int idxN);

4.直方图的基本操作

//归一化
cvNormalizeHist(CvHistogram* hist,double factor);
//阈值函数
void cvThreshHist(CvHistogram* hist,double factor);
//复制直方图 
void cvCopyHist(const CvHistogram* src,CvHistogram** dst);
//直方图中的最值 
void cvGetMinMaxHistValue(const CvHistogram* hist,float* min_value,float* max_value,int* min_idx = NULL,int* max_idx = NULL );
//从图像中计算直方图 
void cvCalcHist(IplImage** image,CvHistogram* hist,int accumulate = 0const CvArr* mask = NULL);

//对比两个直方图 
double cvCompareHist(const CvHistogram* hsit1,const CvHistogram hist2,int method);
#include "cv.h"
#include "highgui.h"

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

    if ((src = cvLoadImage("E:/opencv/lena.png")) != 0)
    {
         IplImage* hsv = cvCreateImage(cvGetSize(src),8,1);
         cvCvtColor(src,hsv,CV_BGR2HSV);

         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;

         {
             int hist_size[] = {h_bins,s_bins};
             float h_ranges[] = {0,180};
             float s_ranges[] = {0,255};
             float* ranges[] = {h_ranges,s_ranges};

             hist = cvCreateHist(2,hist_size,CV_HIST_ARRAY,ranges,1);
         }
         cvCalcHist(planes,hist,0,0);
         cvNormalizeHist(hist,1.0);
         int scale = 10;
         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 i = 0; i < h_bins; i++)
         {
             for (int j = 0; j < s_bins; j++)
             {
                 float bin_val = cvQueryHistValue_2D(hist,i,j);
                 int intensity = cvRound(bin_val*255/max_value);
                 cvRectangle(hist_img,cvPoint(i*scale,j*scale),cvPoint((i+1)*scale -1,(j+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);
    }
}

二、匹配

1·模版匹配

void cvMatchTemplate(const CvArr* image,const CvArr* templ,CvArr* result,int method);
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int main(int argc,char** argv)
{
    IplImage *src,*templ,*ftmp[6];

    if((src = cvLoadImage("E:/opencv/src/1.jpg")) == 0)return 0;
    if((templ = cvLoadImage("E:/opencv/src/2.jpg")) == 0) return 0;

    int iwidth = src->width - templ->width + 1;
    int iheight = src->height - templ->height + 1 ;
    int i;
    for ( i = 0; i < 6; i++)
    {
         ftmp[i] = cvCreateImage(cvSize(iwidth,iheight),32,1);
    }

    for ( i = 0; i < 6; i++)
    {
         cvMatchTemplate(src,templ,ftmp[i],i);
         cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX);
    }

    cvNamedWindow("Templete",0);
    cvShowImage("Templete",templ);

    cvNamedWindow("Image",0);
    cvShowImage("Image",src);

    cvNamedWindow("SQDIFF",0);
    cvShowImage("SQDIFF",ftmp[0]);

    cvNamedWindow("SQDIFF_NORMED",0);
    cvShowImage("SQDIFF_NORMED",ftmp[1]);

    cvNamedWindow("CCORR",0);
    cvShowImage("CCORR",ftmp[2]);

    cvNamedWindow("CCORR_NORMED",0);
    cvShowImage("CCORR_NORMED",ftmp[3]);

    cvNamedWindow("CCOEFF",0);
    cvShowImage("CCOEFF",ftmp[4]);

    cvNamedWindow("CCOEFF_NORMED",0);
    cvShowImage("CCOEFF_NORMED",ftmp[5]);

    cvWaitKey(0);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值