#include <math.h>
#include "mex.h"
/* Sum-Product Belief Propagation on General Graphs
*
* Usage:
* [nodeBel niter nodeMsgs] = BP_General_C(edgePot,nodePot,maximize,maxIter,
optTol,starEdge_V,starEdge_E);
*
* Input:
* nodePot(n,k) - Potential at node n for state k
* edgePot(k1,k2,e) - Potential on edge e for states k1 and k2
* maxmize - Should be 0, since this code only supports sum-product
* maxIter - Maximum number of iterations
* opTol - Optimality Tolerance
* starEdge_V - Vertex vector in star edge representation
* starEdge_E - Edge vector in star edge representation
*
* Output:
* nodeBel(n,k) - Belief at node i for state k
* niter - Number of iterations
* nodeMsgs(e,k) - Message for state k at edge e
*
*/
int DEBUG = 0;
/* Function Declarations */
void checkInput(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs!=7)
mexErrMsgTxt("BP_General_C requires SEVEN Inputs");
if (mxIsChar(prhs[0])||mxIsClass(prhs[0], "sparse")||!mxIsComplex(prhs[0])||mxIsChar(prhs[1])||mxIsClass(prhs[1], "sparse")||!mxIsComplex(prhs[1]))
mexErrMsgTxt("Inputs must be COMPLEX, full, and nonstring");
if (mxGetNumberOfDimensions(prhs[0])!=3)
mexErrMsgTxt("Edge evidence must be a three dimensional array");
if (mxGetNumberOfDimensions(prhs[1])!=2)
mexErrMsgTxt("local evidence must be a two dimensional array");
if (!mxIsClass(prhs[5],"int32")||!mxIsClass(prhs[6],"int32"))
mexErrMsgTxt("edge numberings must be int32");
}
void printVectorInt(int* X,int nElem)
{
int i;
printf("< ");
for(i = 0; i < nElem; i++) {
printf("%d ",X[i]);}
printf(">\n");
}
void printVectorDouble(double* X,int nElem)
{
int i;
printf("< ");
for(i = 0; i < nElem; i++) {
printf("%f ",X[i]);}
printf(">\n");
}
void printVectorDoubleComplex(double* X,double* XI,int nElem)
{
int i;
printf("< ");
for(i = 0; i < nElem; i++) {
printf("%f+i%f ",X[i],XI[i]);}
printf(">\n");
}
void printMatrixDouble(double* X,int nRows, int nCols)
{
int i,j;
for(i = 0; i < nRows; i++) {
printf("< ");
for(j = 0; j < nCols; j++) {
printf("%f ",X[i+nRows*j]);}
printf(">\n");}
}
void printMatrixDoubleComplex(double* X,double* XI,int nRows, int nCols)
{
int i,j;
for(i = 0; i < nRows; i++) {
printf("< ");
for(j = 0; j < nCols; j++) {
printf("%f+%fi ",X[i+nRows*j],XI[i+nRows*j]);}
printf(">\n");}
}
void print2DArrayDouble(double** X,int nRows,int nCols)
{
int i,j;
for(i=0;i < nRows;i++)
{
printf("< ");
for(j=0;j<nCols;j++)
{
printf("%f ",X[i][j]);
}
printf(">\n");
}
}
void print2DArrayDoubleComplex(double** X,double** XI,int nRows,int nCols)
{
int i,j;
for(i=0;i < nRows;i++)
{
printf("< ");
for(j=0;j<nCols;j++)
{
printf("%f+%fi ",X[i][j],XI[i][j]);
}
printf(">\n");
}
}
void print3DMatrixDouble(double*X,int nRows,int nCols,int height)
{
int i,j,k;
for(k = 0; k < height; k++)
{
printf("(%d,:,:) = \n",k);
for(i = 0; i < nRows; i++)
{
printf("< ");
for(j = 0; j < nCols; j++)
{
printf("%f ",X[i+nRows*(j+nCols*k)]);
}
printf(">\n");
}
}
}
int edgeNum(int i,int j,int* starV,int* starE)
{
int E;
if(DEBUG)printf("Looking for edge between %d and %d\n",i,j);
for(E=starV[i];E<starV[i+1];E++)
{
if(DEBUG)printf("Checking %d: Value = %d vs. %d\n",E,starE[E],j);
if(j==starE[E])
return E;
}
mexErrMsgTxt("Could not find Edge Number!");
return 0;
}
double absoluteDif(double n1,double n2)
{
if (n1 > n2)
return n1-n2;
else return n2-n1;
}
/* return the real part of the multiplication (x+yi)*(u+vi) */
double multiplyR(double x, double y, double u, double v)
{
return x*u - y*v;
}
/* return the complex part of the multiplication (x+yi)*(u+vi) */
double multiplyI(double x, double y, double u, double v)
{
return x*v + y*u;
}
/* return the real part of the division (x+yi)/(u+vi) */
double divideR(double x, double y, double u, double v)
{
return (x*u + y*v)/(u*u + v*v);
}
/* return the complex part of the division (x+yi)/(u+vi) */
double divideI(double x, double y, double u, double v)
{
/*printf("[x,y,u,v] = [%.3f, %.3f, %.3f, %.3f], result = %.3f\n",x,y,u,v,(-x*v+y*u)/(u*u+v*v));*/
return (-x*v+y*u)/(u*u + v*v);
}
/* return the absolute value (squared) of (x+yi) */
double absSquared(double x, double y)
{
return x*x+y*y;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* Declare Variables */
int
i,j,k,ii,jj,iii,nbrsInd,nbrsInd2,E,iter,n,n2,E2,k_i,k_j,
nNodes,nEdges,nStates,maximize,maxIter,converged,nNbrs,nNbrs2,
lhs1_dims[2],lhs2_dims[2],lhs3_dims[2],lhs4_dims[3],
*starV,*starE,
*nbrs,*nbrs2;
/* Real Coefficent Variables */
double
optTol,sum,realPart,temp2,temp3,temp4,
*nodePot,*edgePot,
*nodeBel,*nIter,*msgs,*edgeBel,
*pot_ij,*temp,*newm,
**prod_of_msgs,**old_bel,**new_bel,**old_msg,**new_msg;
/* Complex Coefficient Variables */
double
sumI,temp2I,temp3I,temp4I,
*nodePotI,*edgePotI,
*nodeBelI,*msgsI,*edgeBelI,
*pot_ijI,*tempI,*newmI,
**prod_of_msgsI,**old_belI,**new_belI,**old_msgI,**new_msgI;
/* Check Input */
checkInput(nlhs,plhs,nrhs,prhs);
/* Get Input Pointers */
nodePot = mxGetPr(prhs[1]);
edgePot = mxGetPr(prhs[0]);
maximize = mxGetPr(prhs[2])[0];
maxIter = mxGetPr(prhs[3])[0];
optTol = mxGetPr(prhs[4])[0];
starV = mxGetPr(prhs[5]);
starE = mxGetPr(prhs[6]);
nNodes = mxGetDimensions(prhs[1])[0];
nEdges = mxGetDimensions(prhs[0])[2];
nStates = mxGetDimensions(prhs[1])[1];
nodePotI = mxGetPi(prhs[1]);
edgePotI = mxGetPi(prhs[0]);
/* Avoid indexing confusion by decrementing the Star arrays */
for(i = 0; i <= nNodes;i++)
starV[i]=starV[i]-1;
for(j = 0; j < nEdges;j++)
starE[j]=starE[j]-1;
/* Set-up Output Arrays */
lhs1_dims[0] = nNodes;
lhs1_dims[1] = nStates;
lhs2_dims[0] = 1;
lhs2_dims[1] = 1;
lhs3_dims[0] = nEdges;
lhs3_dims[1] = nStates;
lhs4_dims[0] = nStates;
lhs4_dims[1] = nStates;
lhs4_dims[2] = nEdges;
plhs[0] = mxCreateNumericArray(2,lhs1_dims,mxDOUBLE_CLASS,mxCOMPLEX);
plhs[1] = mxCreateNumericArray(2,lhs2_dims,mxDOUBLE_CLASS,mxREAL);
plhs[2] = mxCreateNumericArray(2,lhs3_dims,mxDOUBLE_CLASS,mxCOMPLEX);
plhs[3] = mxCreateNumericArray(3,lhs4_dims,mxDOUBLE_CLASS,mxCOMPLEX);
nodeBel = mxGetPr(plhs[0]);
nIter = mxGetPr(plhs[1]);
msgs = mxGetPr(plhs[2]);
nodeBelI = mxGetPi(plhs[0]);
msgsI = mxGetPi(plhs[2]);
edgeBel = mxGetPr(plhs[3]);
edgeBelI = mxGetPi(plhs[3]);
/* Allocate Memory for Auxiliary Arrays */
prod_of_msgs = mxCalloc(nNodes,sizeof(double*));
old_bel = mxCalloc(nNodes,sizeof(double*));
new_bel = mxCalloc(nNodes,sizeof(double*));
old_msg = mxCalloc(nEdges,sizeof(double*));
new_msg = mxCalloc(nEdges,sizeof(double*));
newm = mxCalloc(nStates,sizeof(double));
pot_ij = mxCalloc(nStates*nStates,sizeof(double));
temp = mxCalloc(nStates,sizeof(double));
prod_of_msgsI = mxCalloc(nNodes,sizeof(double*));
old_belI = mxCalloc(nNodes,sizeof(double*));
new_belI = mxCalloc(nNodes
没有合适的资源?快使用搜索试试~ 我知道了~
条件随机场在二维图像中的应用
共153个文件
m:58个
old:24个
repository:12个
需积分: 10 27 下载量 174 浏览量
2009-09-18
14:42:38
上传
评论
收藏 140KB ZIP 举报
温馨提示
条件随机场Conditional random fields 在二维图像处理上的应用,欢迎交流!
资源推荐
资源详情
资源评论
收起资源包目录
条件随机场在二维图像中的应用 (153个子文件)
BP_GeneralComplex_C.c 24KB
BP_General_C.c 17KB
MF_GeneralComplex_C.c 17KB
repmatC.c 4KB
expandFeatureVec.c 2KB
expandFeatureVec.dll 40KB
BP_GeneralComplex_C.dll 16KB
MF_GeneralComplex_C.dll 14KB
BP_General_C.dll 12KB
repmatC.dll 8KB
Entries 857B
Entries 588B
Entries 437B
Entries 330B
Entries 320B
Entries 236B
Entries 236B
Entries 232B
Entries 213B
Entries 181B
Entries 134B
Entries 130B
Entries.Extra 469B
Entries.Extra 309B
Entries.Extra 238B
Entries.Extra 183B
Entries.Extra 179B
Entries.Extra 151B
Entries.Extra 137B
Entries.Extra 124B
Entries.Extra 124B
Entries.Extra 112B
Entries.Extra 83B
Entries.Extra 62B
process_options.m 4KB
stochgrad.m 3KB
demoMulti.m 3KB
demoIsing.m 3KB
assign_edge_nums_lattice.m 3KB
demoSingle.m 3KB
smd.m 2KB
suptitle.m 2KB
plGradient.m 2KB
plGradientBroken.m 2KB
scrfGradient.m 2KB
computeEdgeBel.m 2KB
latticeFeatures.m 2KB
infer.m 2KB
MF_General.m 2KB
StarEdge_MakeEdgeNums_Lattice2.m 2KB
subv2ind.m 2KB
betheMRF2log0.m 2KB
infer.m 1KB
MFGibbsFreeEnergy.m 1KB
pseudoLikGradient.m 1016B
normalize.m 861B
mkPotentials.m 831B
latticeInferBP.m 821B
computeEdgeBel.m 816B
latticeInferMF.m 813B
mkObsEdgeFeatures.m 760B
mkEdgeFeatures.m 748B
logistDeriv.m 746B
enterEvidence.m 734B
mkExpectedEdgeFeatures.m 690B
getMu.m 647B
mkNodeFeatures.m 640B
scrfGradientWeights.m 619B
pseudoLikGradientWeights.m 616B
approxeq.m 577B
StarEdge_EdgeNum.m 537B
classifPerformance.m 490B
mkExpectedNodeFeatures.m 376B
logist2Apply.m 365B
mkMinibatches.m 336B
StarEdge_FindNeighbors.m 322B
mkObsNodeFeatures.m 258B
compareLabels.m 244B
assert.m 214B
StarEdge_GetNodes.m 172B
splitWeights.m 162B
displayIter.m 132B
initWeights.m 129B
display.m 86B
display.m 86B
display.m 86B
subsasgn.m 69B
subsasgn.m 69B
subsasgn.m 69B
subsref.m 61B
subsref.m 61B
subsref.m 61B
#demoMulti.m# 3KB
#demoIsing.m# 3KB
#demoSingle.m# 2KB
repmatC.mexglx 20KB
MF_GeneralComplex_C.mexglx 18KB
BP_GeneralComplex_C.mexglx 18KB
BP_General_C.mexglx 14KB
expandFeatureVec.mexglx 7KB
共 153 条
- 1
- 2
资源评论
floppysongruan
- 粉丝: 0
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功