#ifndef   __RV_ANLZ_H_INCLUDED_
#define   __RV_ANLZ_H_INCLUDED_



#include <rvb\rvtypes.h>
#include <rvb\rdg.h>
#include <rvb\dsm.h> 

#define ANLZ_API  WINAPI

 




//contour 
extern "C"  RvContour ANLZ_API  rvCreateContour( RV_CONTOUR_CODE  code);
extern "C"  void  ANLZ_API      rvDestroyContour(RvContour contour);

//extern "C"   void  ANLZ_API rvContourRecycler(RV_HANDLE contour);// {     rvDestroyContour((RvContour) contour); }


//extern "C"  void*   ANLZ_API rvGetContourData(const RvContour contour);
//extern "C"  UINT     ANLZ_API rvGetContourSize(const RvContour contour);

//extern "C"  UINT   ANLZ_API rvSetContourData(  RvContour contour, void* pData, UINT size);

//添加边缘点，rvGetContourSize将返回边缘点数量
//设置基本的轮廓数据
extern "C"  void   ANLZ_API rvSetContourDataE1(  RvContour contour,   RV_CONTOUR_TYPE type,  RvPoint* pArray, int count);
extern "C"  UINT   ANLZ_API rvGetCntrPointCount(const  RvContour contour );

//返回所有轮廓的数据
extern "C"  int   ANLZ_API rvGetContourDataE1(  RvContour contour, RvPoint* pArray, int count);


//返回轮廓数据的索引,SINCE 0
//if father < 0, set as default
extern "C"  int   ANLZ_API rvAppendContourData(RvContour contour, int parent, RV_CONTOUR_TYPE type,    RvPoint* pArray, UINT count );
 

extern "C"  RvPoint*  ANLZ_API rvGetContourDataAt(const RvContour contour, int index);
extern "C"  int       ANLZ_API rvGetContourCountAt(const RvContour contour, int index);
extern "C"  int  ANLZ_API rvGetTotalContours(const RvContour contour );

//该函数相当与rvGetContourDataAt( contour, 0);
extern "C"  RvPoint*  ANLZ_API rvGetContourBaseData(const RvContour contour);

//该函数相当与rvGetContourCountAt( contour, 0);
extern "C"  int       ANLZ_API rvGetContourBaseCount(const RvContour contour);

 extern "C"  RV_CONTOUR_HIERARCHY ANLZ_API rvGetContourHierarchy(const RvContour contour);
 extern "C"  void       ANLZ_API rvSetContourHierarchy(const RvContour contour, RV_CONTOUR_HIERARCHY hierarchy);

//原始数据

//extern "C"  void       ANLZ_API rvSetContourRawSize(const RvContour contour, int size);
//extern "C"  void      ANLZ_API rvSetContourRawAt(const RvContour contour, int index, RvPoint value);
//extern "C"  RvPoint   ANLZ_API rvGetContourRawAt(const RvContour contour, int index );
//extern "C"  void      ANLZ_API rvClearContourRaw(const RvContour contour );

 //rvConvertBinToLabel这个速度有点慢
//extern "C" RvMatrix ANLZ_API rvConvertBinToLabel(const RvImage image ,int* pMaxLabel);
// extern "C" RvMatrix ANLZ_API rvConvertBinToLabelE3(const RvImage image ,BOOL  bConn8,int* pMaxLabel);
//
//extern "C"  int ANLZ_API     rvPickLabels(RvMatrix mat, int x0, int y0, int label, RvPoint* pPtArr, int size);
//
//extern "C" RvMatrix ANLZ_API rvConvertBinToLabelE2(const RvImage image , BOOL n8, int* pMaxLabel);
//extern "C" RV_LABEL_LIST* ANLZ_API     rvPickLabelsE2(RvMatrix mat, BOOL bEdgeLabel,  BOOL n8, int* pLabelCount);
//
//
//
////转成LABEL链表，单向, 8连通或4连通
//extern "C" RV_LABEL_LIST* ANLZ_API rvConvertBinToLabelE1(const RvImage image, BOOL n8, int* pMaxLabel);
//extern "C" void  ANLZ_API rvDestroyLabelList(RV_LABEL_LIST* pLabelList);
// 

extern "C"  RvRect  ANLZ_API     rvGetContourRect(const RvContour contour);
extern "C"  RvBox2D ANLZ_API     rvGetContourBoundBox(const RvContour contour);

//extern "C"  double ANLZ_API    rvGetContourFeature(const RvContour contour, int feature);
extern "C"  int   ANLZ_API     rvGetContourType(const RvContour contour, int index );
//extern "C"  int   ANLZ_API     rvGetContourCount(const RvContour contour );

extern "C"  int   ANLZ_API     rvGetContourCoder(const RvContour contour ); //obsolete

extern "C"  RV_CONTOUR_CODE   ANLZ_API     rvGetContourCode (const RvContour contour );
extern "C"  void   ANLZ_API    rvSetContourCode (  RvContour contour, RV_CONTOUR_CODE  code );



extern "C"  double   ANLZ_API     rvGetContourCircular(const RvContour contour );
extern "C"  double   ANLZ_API     rvGetContourArea(const RvContour contour );
extern "C"  double   ANLZ_API     rvGetContourPerim(const RvContour contour );
extern "C"  double   ANLZ_API     rvGetContourAngle(const RvContour contour );
extern "C"  RvPoint_f  ANLZ_API   rvGetContourCentroid(const RvContour contour );
extern "C"  double  ANLZ_API      rvGetContourEccentricity(const RvContour contour );

//规范化0-360, 保证一个方向,对非对称物体有效.
extern "C"  double   ANLZ_API     rvGetContourAngleE1(const RvContour contour  );



//查找排序字段
#define RV_FC_AREA       1  //面积，注意chain必需是external。否则可能会重复计算
#define RV_FC_PERIM      (1<<1)  //周长
#define RV_FC_ANGLE      (1<<2)  //角度(只能计算单个contour )
#define RV_FC_CIRCULAR   (1<<3)  //长宽比（圆度）(只能计算单个contour )
#define RV_FC_KEYPOS     (1<<4)  //关键点数量
#define RV_FC_RAWPOS     (1<<5)  //原始点数量
 
extern "C"  int ANLZ_API rvFindContours(const RvImage image,  RV_CONTOUR_CODE code/* = RV_CC_DEFAULT*/, RV_CONTOUR_FILTER filter/* = RV_CF_ANY*/,  int minLength/*=7*/, RvSequence	 seqOut );

//按照最外围轮廓进行提取边缘，然后按照面积，周长长度等排序，获取最前面的轮廓
extern "C"  int ANLZ_API rvFindContoursE1(const RvImage image,  RV_CONTOUR_CODE code ,  int maxCount , int order/* = RV_FC_AREA*/, RvSequence	 seqOut );


//#define RFC_CUT_EDGE           1
//#define RFC_WHITE_BACKGROUND  (1<<1)
 


//maxCount >0时， 使用周长选择最长的几条轮廓
//extern "C"  RvSequence ANLZ_API rvFindContours(const RvImage image,  RV_CONTOUR_CODE code = RV_CC_DEFAULT, RV_CONTOUR_FILTER filter = RV_CF_ANY,  int minLength=9, int maxCount=-1 );

//extern "C"  RvCollect ANLZ_API   rvArrangeContours(const RvSequence seq, int mode, RvCollect collect=NULL);

//extern "C"  RvContour  ANLZ_API  rvRecodeContour(RvContour  contour,  int code);
//extern "C"  RvContour ANLZ_API   rvApproxContours(RvContour  contour,  int method, RvContour pDest=NULL);

//对所有的轮廓进行重新编码
//extern "C"  void  ANLZ_API  rvEncodeContours(RvSequence seq,  int code);


//RvSequence通过rvFindContour获得，根据属性大小选择最大的几个轮廓
//extern "C"  int ANLZ_API   rvChooseContours(RvSequence  seqContour, int maxCount, int selectBy);

//RvSequence通过rvFindContour获得,保留需要的类型,多种轮廓类型组合，该函数保留升级用
//extern "C"  int ANLZ_API   rvFilterContours(RvSequence  seqContour, int types, int filterBy, int minVal);


//清除轮廓，RvSequence通过rvFindContour获得
//extern "C"  void ANLZ_API   rvClearContours(RvSequence  seqContour);

extern "C"  RvContour ANLZ_API   rvCloneContour(const RvContour  contour);

extern "C"  void ANLZ_API   rvMoveContour( RvContour  contour, int dx, int dy);


//只能比较code为default的轮廓
#define RV_CC_MATCH1    1
#define RV_CC_MATCH2    2
#define RV_CC_MATCH3    3
extern "C"  double ANLZ_API   rvCompareContours(const RvContour  contour1, const RvContour  contour2, int method);



//该函数清除轮廓，同时释放RvSequence.
//extern "C"  void ANLZ_API   rvReleaseContours(RvSequence*  pSeq);



//extern "C"  RvMask ANLZ_API rvRetrieveEdgeMask(const RvImage image, const RvMask roiMask, int grid, int strength,  RvMask egdeMask);


//计算裁片中非零像素的数量。用于计算一个对象的面积大小或长度。	面，线
extern "C"  UINT ANLZ_API rvCountPixels(const RvImage image, const RvMask mask=NULL);
extern "C"  RvReadingBank ANLZ_API rvCountPixelsEx(const RvImage image, const RvMask mask = NULL , RvReadingBank bank =NULL );


 	 
 

//统计裁片中每个灰度级的像素值的数量。 
extern "C"  int ANLZ_API rvHistogram(RvImage image, const RvMask mask, UINT* pReadingArray, int nArraySize);

//统计裁片中每个灰度级的像素值的数量。 
extern "C"  RvReadingBank ANLZ_API rvHistogramEx(RvImage image, const RvMask mask, RvReadingBank reading=NULL );

//计算图象的清晰度(如镜头没有对焦形成的图象)。GRAY_256图像
extern "C"  double ANLZ_API rvGetClearness(const RvImage image);


//查找象素为level的对象的上方，下方，左边，右边的边界点, 图像必须是二元图像 
extern "C"  RvRect ANLZ_API rvGetBoundRect(RvImage image, const RvMask mask =NULL);

//查找包围blob对象的最小面积的矩形,可能已经旋转	 ,图像必须是二元图像
extern "C"  RvBox2D ANLZ_API rvGetBoundBox(const RvImage image, const RvMask mask= NULL);

//找出图中最大部分的boundbox
//查找排序字段
#define RV_GBB_AREA       1  //面积，注意chain必需是external。否则可能会重复计算
#define RV_GBB_PERIM      (1<<1)  //周长
extern "C"  RvBox2D ANLZ_API rvGetBoundBoxE1(RvImage image, int order, BOOL bIncludeBorder);

//bin, 最大的轮廓拟合成椭圆
//extern "C" BOOL  ANLZ_API rvFitEllipse(RvImage image,  RvBox2D*  pBox2d, double* pAvgError, double* pMaxError, double* pMinError);

//该函数会将点云自动规范化，0角度。
//extern "C" BOOL  ANLZ_API rvFitEllipseE1(RvPoint* pEdgePoints, int count, RvBox2D*  pBox2d, double* pAvgError, double* pMaxError, double* pMinError );




//仅BIN有效,提取有效点的位置 
extern "C"  int ANLZ_API rvRetrievePosArray(const RvImage image, RvPoint* pPosArray,  int arrSize);


//搜索裁片中最大或最小像素的值和位置
extern "C"  int ANLZ_API rvFindPolar(const RvImage image, const RvMask mask = NULL,  RvPoint* pPos = NULL, BOOL bDark=FALSE);

//统计分析裁片的平均值，算术和,方差。	 
extern "C"  RvReadingBank ANLZ_API rvProfileEx(const RvImage image,const RvMask mask = NULL, RvReadingBank reading=NULL   );

extern "C"  int ANLZ_API rvProfile (const RvImage image, const RvMask mask,  double* pReadingArray, int nArraySize  );

//将裁片中所有像素的像素值累加。	 
extern "C"  UINT ANLZ_API  rvSummary(const RvImage image, const RvMask mask=NULL);	
extern "C"  double   ANLZ_API rvSummaryF64(const RvImage image, const RvMask mask =NULL );

//图像格式: gray, bin
extern "C"  UINT ANLZ_API  rvSummaryE1(const RvImage image, RvRect rect);	



extern "C"  RvReadingBank ANLZ_API  rvSummaryEx(const RvImage image, const RvMask mask=NULL, RvReadingBank bank=NULL);	

//计算裁片中像素的方差(标准方差的平方)	 
extern "C"  double ANLZ_API  rvVariance(const RvImage image, const RvMask mask=NULL  );	
extern "C"  RvReadingBank ANLZ_API  rvVarianceEx(const RvImage image, const RvMask mask=NULL, RvReadingBank bank=NULL);	


//计算裁片内像素的平均值。这是对整个裁片的平均亮度的测量。	 
extern "C"  double ANLZ_API rvAverage(const RvImage image, const RvMask mask=NULL );
extern "C"  RvReadingBank ANLZ_API  rvAverageEx(const RvImage image, const RvMask mask=NULL, RvReadingBank bank=NULL);	

//仅支持二元图象
extern "C"  UINT ANLZ_API rvArea(const RvImage image, const RvMask mask = NULL  );
//#define  rvCountPixels(image)   rvArea((image))

//仅支持2元图象
extern "C"  double  ANLZ_API rvCircular(const RvImage image, const RvMask mask=NULL );

//计算二元图象的方向
extern "C" double     ANLZ_API rvSlope(const RvImage image, const RvMask mask=NULL);

extern "C" RvPoint_f  ANLZ_API rvCentroid(const RvImage image ,  const RvMask mask=NULL);


//支持0-360度
extern "C" double     ANLZ_API rvSlopeE1(const RvImage image );
extern "C" RvPoint_f  ANLZ_API rvCentroidE1(const RvImage image );

//过时
//extern "C" void  ANLZ_API rvRegionFeatures(const RvImage image, UINT* pArea, RvPoint_f* pGravity, double* pAngle, double* pCircular);


//计算二元图象边界长度
extern "C" UINT ANLZ_API rvPerimeter(const RvImage image, const RvMask mask=NULL);
 
//blob dentity part
#define RV_BSP_WHOLE           0xFF
#define RV_BSP_NORTH     1
#define RV_BSP_EAST     (1<<1)
#define RV_BSP_SOUTH    (1<<2)
#define RV_BSP_WEST     (1<<3) 

#define RV_BSP_NW      (RV_BSP_NORTH | RV_BSP_WEST)
#define RV_BSP_NE      (RV_BSP_NORTH | RV_BSP_EAST)
#define RV_BSP_SW      (RV_BSP_SOUTH | RV_BSP_WEST)
#define RV_BSP_SE      (RV_BSP_SOUTH | RV_BSP_EAST)


extern "C" double ANLZ_API rvDensity(const RvImage image, const RvMask mask, int subRegion);



//计算裁片的垂直与水平倒影
extern "C"  RvReadingBank ANLZ_API rvProjectEx(RvImage image, const RvMask mask, int dir, RvReadingBank reading=NULL);

 
extern "C"  int ANLZ_API rvProject(RvImage image,const RvMask mask,  int dir, UINT *pReadingArray, int nArraySize);

 
//加权投影，如果image为多通道图象，需要输入水平或（和）垂直宽度的多通道的倍数的数据，如channel* width。 
extern "C"  int ANLZ_API rvProjectE1(RvImage image,const RvMask mask,  int dir, double *pReadingArray, int nArraySize);

#define RV_HL_SHT      0
#define RV_HL_PPHT     1
#define RV_HL_MS       2

extern "C"  int ANLZ_API rvHoughLine(RvImage image,  int method, double rho, double theta, int threshold, double option1, double option2, RvLine_f* pReadingArray, int nArraySize);
extern "C"  RvReadingBank ANLZ_API rvHoughLineEx(RvImage image,  int method, double rho, double theta, int threshold, double option1, double option2, RvReadingBank reading=NULL);

#define RV_HC_GRADIENT   0
extern "C"  int ANLZ_API rvHoughCircle(RvImage image,  int method, double dp, double minDist,  int minRadius, int maxRadius, double option1, double option2, RvEllipse_f * pReadingArray, int nArraySize);
extern "C"  RvReadingBank ANLZ_API rvHoughCircleEx(RvImage image,  int method, double dp, double minDist,  int minRadius, int maxRadius, double option1, double option2, RvReadingBank reading=NULL);


extern "C"   void ANLZ_API rvBoxVertex ( RvBox2D* pBox, RvPoint_f points[4] );

//BIN, GRAY
//计算指定点周围的像素和
extern "C"   UINT ANLZ_API rvGetPixelSum(RvImage image,  int x, int y, int radius );


//extern "C" int    ANLZ_API rvGenBoxChain(const RvImage image,   int minWidth , int minHeight, int maxWidth, int maxHeight, BOOL bExternal, RvSequence sequence );
//extern "C" void   ANLZ_API  rvRelBoxChain(RvSequence sequence);
 
//获得平均亮度,RGB图
extern "C" float ANLZ_API rvGetLum(const RvImage image );



#endif
