?? histogram.c
字號:
/*############################################################################# * 文件名:histogram.c * 功能: 實現(xiàn)了指紋直方圖的操作 * modified by PRTsinghua@hotmail.com#############################################################################*/#include <stdlib.h>#include <stdio.h>#include <string.h>#include "histogram.h"/* 直方圖可以快速計算位圖的一些信息,比如均值,方差等 */typedef struct iFvsHistogram_t{ FvsUint_t ptable[256]; /* 8位圖像的直方圖 */ FvsInt_t ncount; /* 直方圖中的點數(shù) */ FvsInt_t nmean; /* -1 = 還沒有計算 */ FvsInt_t nvariance; /* -1 = 還沒有計算 */} iFvsHistogram_t;/****************************************************************************** * 功能:創(chuàng)建一個新的直方圖對象 * 參數(shù):無 * 返回:失敗返回空,否則返回直方圖對象******************************************************************************/FvsHistogram_t HistogramCreate(){ iFvsHistogram_t* p = NULL; p = (FvsHistogram_t)malloc(sizeof(iFvsHistogram_t)); if (p!=NULL) { /* 重置表 */ HistogramReset(p); } return (FvsHistogram_t)p;}/****************************************************************************** * 功能:破壞一個存在的直方圖對象 * 參數(shù):histogram 直方圖對象指針 * 返回:錯誤編號******************************************************************************/void HistogramDestroy(FvsHistogram_t histogram){ iFvsHistogram_t* p = NULL; if (histogram==NULL) return; p = histogram; free(p);}/****************************************************************************** * 功能:重置一個存在的直方圖對象為0 * 參數(shù):histogram 直方圖對象指針 * 返回:錯誤編號******************************************************************************/FvsError_t HistogramReset(FvsHistogram_t hist){ iFvsHistogram_t* histogram = (iFvsHistogram_t*)hist; int i; for (i = 0; i < 256; i++) histogram->ptable[i] = 0; histogram->ncount = 0; histogram->nmean = -1; histogram->nvariance = -1; return FvsOK;}/****************************************************************************** * 功能:計算一個8-bit圖像的直方圖 * 參數(shù):histogram 直方圖對象指針 * image 圖像指針 * 返回:錯誤編號******************************************************************************/FvsError_t HistogramCompute(FvsHistogram_t hist, const FvsImage_t image){ iFvsHistogram_t* histogram = (iFvsHistogram_t*)hist; FvsError_t nRet = FvsOK; FvsInt_t w = ImageGetWidth(image); FvsInt_t h = ImageGetHeight(image); FvsInt_t pitch = ImageGetPitch(image); uint8_t* p = ImageGetBuffer(image); FvsInt_t x, y; if (histogram==NULL || p==NULL) return FvsMemory; /* 首先重置直方圖 */ nRet = HistogramReset(hist); /* 計算 */ if (nRet==FvsOK) { FvsInt_t pos; for (y=0; y<h; y++) { pos = pitch*y; for (x=0; x<w; x++) { histogram->ptable[p[pos++]]++; } } histogram->ncount = w*h; } return nRet;}/****************************************************************************** * 功能:計算一個直方圖對象的均值 * 參數(shù):histogram 直方圖對象指針 * 返回:均值******************************************************************************/FvsByte_t HistogramGetMean(const FvsHistogram_t hist){ iFvsHistogram_t* histogram = (iFvsHistogram_t*)hist; FvsInt_t val, i; val = histogram->nmean; if (val==-1) { val = 0; for (i = 1; i < 255; i++) val += i*histogram->ptable[i]; i = histogram->ncount; if (i>0) val = val/i; else val = 0; histogram->nmean = val; } return (uint8_t)val;}/****************************************************************************** * 功能:計算一個直方圖對象的方差 * 參數(shù):histogram 直方圖對象指針 * 返回:方差******************************************************************************/FvsUint_t HistogramGetVariance(const FvsHistogram_t hist){ iFvsHistogram_t* histogram = (iFvsHistogram_t*)hist; FvsInt_t val; FvsInt_t i; uint8_t mean; val = histogram->nvariance; if (val==-1) { /* 計算均值 */ mean = HistogramGetMean(hist); val = 0; for (i = 0; i < 255; i++) val += histogram->ptable[i]*(i - mean)*(i - mean); i = histogram->ncount; if (i>0) val = val/i; else val = 0; histogram->nvariance = val; } return (FvsUint_t)val;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -