opencv:直方图与匹配

来源:互联网 发布:mysql 性能监控软件 编辑:程序博客网 时间:2024/04/29 14:17

一、直方图

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
原创粉丝点击