📄 ikmeanspush.mex.c
字号:
/* file: ikmeanspush.c** description: MEX weighted ikmeanspush function.** author: Andrea Vedaldi**//* AUTORIGHTSCopyright (C) 2006 Regents of the University of CaliforniaAll rights reservedWritten by Andrea Vedaldi (UCLA VisionLab).Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of California, Berkeley nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANYEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED ANDON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include<mexutils.c>#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<assert.h>/* driver */voidmexFunction(int nout, mxArray *out[], int nin, const mxArray *in[]){ typedef unsigned char data_t ; typedef int acc_t ; typedef unsigned int idx_t ; idx_t* asgn_pt ; acc_t* centers_pt ; data_t* data_pt ; /* mxClassID data_class = mxINT8_CLASS ;*/ enum {IN_DATA=0,IN_CENTERS} ; enum {OUT_ASGN} ; int M,N ; int K=0 ; /** ----------------------------------------------------------------- ** Check the arguments ** -------------------------------------------------------------- */ if (nin != 2) { mexErrMsgTxt("Two arguments required.") ; } else if (nout > 2) { mexErrMsgTxt("Too many output arguments.") ; } if(mxGetClassID(in[IN_DATA]) != mxUINT8_CLASS) { mexErrMsgTxt("DATA must be of class uint8") ; } if(mxGetClassID(in[IN_CENTERS]) != mxINT32_CLASS) { mexErrMsgTxt("DATA must be of class int32") ; } M = mxGetM(in[IN_DATA]) ; /* n of components */ N = mxGetN(in[IN_DATA]) ; /* n of elements */ K = mxGetN(in[IN_CENTERS]) ; /* n of centers */ if( mxGetM(in[IN_CENTERS]) != M ) { mexErrMsgTxt("DATA and CENTERS must have the same number of columns.") ; } { int dims[2] ; dims[0] = 1 ; dims[1] = N ; out[OUT_ASGN] = mxCreateNumericArray(2,dims,mxUINT32_CLASS,mxREAL); } data_pt = (data_t*) mxGetPr(in[IN_DATA]) ; centers_pt = (acc_t*) mxGetPr(in[IN_CENTERS]) ; asgn_pt = (idx_t*) mxGetPr(out[OUT_ASGN]) ; /** ----------------------------------------------------------------- ** Go ** -------------------------------------------------------------- */ { int i,j,k ; /* assign data to centers */ for(j=0 ; j < N ; ++j) { acc_t best_dist = 0 ; idx_t best = (idx_t)-1 ; for(k = 0 ; k < K ; ++k) { acc_t dist = 0 ; /* compute distance with this center */ for(i = 0 ; i < M ; ++i) { acc_t delta = data_pt[ j*M + i ] - centers_pt[ k*M + i ] ; dist += delta*delta ; } /* compare with current best */ if( best == (idx_t)-1 || dist < best_dist ) { best = k ; best_dist = dist ; } } asgn_pt[j] = best ; } for(j = 0 ; j < N ; ++j) ++ asgn_pt[j] ; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -