void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Check for proper number of arguments if (nrhs != 2) mexErrMsgTxt("Two input arguments required."); if (nlhs != 4) mexErrMsgTxt("Four output arguments required."); // The input must be noncomplex if (mxIsComplex(prhs[0]) || mxIsComplex(prhs[1]) || !mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1])) mexErrMsgTxt("The input must be noncomplex (and numeric)."); // The second input must be an unsigned integer if (mxIsSingle(prhs[1]) || mxIsDouble(prhs[1])) mexErrMsgTxt("The second input must be an integer."); if (mxIsSingle(prhs[0])) { if (mxIsInt8(prhs[1])) compute<float, char>(plhs, prhs); else if (mxIsUint8(prhs[1])) compute<float, unsigned char>(plhs, prhs); else if (mxIsInt16(prhs[1])) compute<float, short>(plhs, prhs); else if (mxIsUint16(prhs[1])) compute<float, unsigned short>(plhs, prhs); else if (mxIsInt32(prhs[1])) compute<float, int>(plhs, prhs); else if (mxIsUint32(prhs[1])) compute<float, unsigned int>(plhs, prhs); else if (mxIsInt64(prhs[1])) compute<float, long long int>(plhs, prhs); else if (mxIsUint64(prhs[1])) compute<float, unsigned long long int>(plhs, prhs); } else if (mxIsDouble(prhs[0])) { if (mxIsInt8(prhs[1])) compute<double, char>(plhs, prhs); else if (mxIsUint8(prhs[1])) compute<double, unsigned char>(plhs, prhs); else if (mxIsInt16(prhs[1])) compute<double, short>(plhs, prhs); else if (mxIsUint16(prhs[1])) compute<double, unsigned short>(plhs, prhs); else if (mxIsInt32(prhs[1])) compute<double, int>(plhs, prhs); else if (mxIsUint32(prhs[1])) compute<double, unsigned int>(plhs, prhs); else if (mxIsInt64(prhs[1])) compute<double, long long int>(plhs, prhs); else if (mxIsUint64(prhs[1])) compute<double, unsigned long long int>(plhs, prhs); } else mexErrMsgTxt("Input types not supported."); }
/** * @brief This file implements an interface to add a Factor to an opengm model in MatLab. * * This routine accepts a function id and a subset of variables and connects them to a new factor of the model. * * @param[in] nlhs number of output arguments expected from MatLab. * (needs to be 0). * @param[out] plhs pointer to the mxArrays containing the results. * @param[in] nrhs number of input arguments provided from MatLab. * (needs to be 3) * @param[in] prhs pointer to the mxArrays containing the input data provided by * matlab. prhs[0] needs to contain the handle for the model. * prhs[1] needs to contain the function id. prhs[2] needs to contain a matrix where every column represents a subset of variables. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { //check if data is in correct format if(nrhs != 3) { mexErrMsgTxt("Wrong number of input variables specified (three needed)\n"); } if(nlhs != 0) { mexErrMsgTxt("Wrong number of output variables specified (zero needed)\n"); } // get model out of handle typedef opengm::interface::MatlabModelType::GmType GmType; GmType& gm = opengm::interface::handle<GmType>::getObject(prhs[0]); // get function id GmType::FunctionIdentifier::FunctionIndexType functionIndex; GmType::FunctionIdentifier::FunctionTypeIndexType functionType; if((mxIsUint8(prhs[1]) || mxIsUint16(prhs[1]) || mxIsUint32(prhs[1]) || mxIsUint64(prhs[1])) && (mxGetM(prhs[1]) == 1) && (mxGetN(prhs[1]) == 2)) { if(mxIsUint8(prhs[1])) { const uint8_T* data = static_cast<const uint8_T*>(mxGetData(prhs[1])); functionIndex = static_cast<GmType::FunctionIdentifier::FunctionIndexType>(data[0]); functionType = static_cast<GmType::FunctionIdentifier::FunctionTypeIndexType>(data[1]); } else if(mxIsUint16(prhs[1])) { const uint16_T* data = static_cast<const uint16_T*>(mxGetData(prhs[1])); functionIndex = static_cast<GmType::FunctionIdentifier::FunctionIndexType>(data[0]); functionType = static_cast<GmType::FunctionIdentifier::FunctionTypeIndexType>(data[1]); } else if(mxIsUint32(prhs[1])) { const uint32_T* data = static_cast<const uint32_T*>(mxGetData(prhs[1])); functionIndex = static_cast<GmType::FunctionIdentifier::FunctionIndexType>(data[0]); functionType = static_cast<GmType::FunctionIdentifier::FunctionTypeIndexType>(data[1]); } else if(mxIsUint64(prhs[1])) { const uint64_T* data = static_cast<const uint64_T*>(mxGetData(prhs[1])); functionIndex = static_cast<GmType::FunctionIdentifier::FunctionIndexType>(data[0]); functionType = static_cast<GmType::FunctionIdentifier::FunctionTypeIndexType>(data[1]); } } else { mexErrMsgTxt("Unsupported dataformat for function id!"); } GmType::FunctionIdentifier functionID(functionIndex, functionType); // get subset of variables and add factor if(mxIsNumeric(prhs[2])) { if(mxGetNumberOfDimensions(prhs[2]) != 2) { mexErrMsgTxt("variables has to be a matrix where every column represents a subset of variables!"); } size_t numFactors = mxGetN(prhs[2]); size_t numVariablesPerFactor = mxGetM(prhs[2]); typedef opengm::interface::helper::getDataFromMXArray<addFactorsFunctor> factorAdder; addFactorsFunctor functor(functionID, gm, numFactors, numVariablesPerFactor); factorAdder()(functor, prhs[2]); } else { mexErrMsgTxt("Unsupported dataformat for subset of variables!"); } }
const char *mxGetClassName(const mxArray *ptr) { if (mxIsDouble(ptr)) { return "double"; } if (mxIsChar(ptr)) { return "char"; } if (mxIsLogical(ptr)) { return "bool"; } if (mxIsSparse(ptr)) { return "sparse"; } if (mxIsInt8(ptr)) { return "int8"; } if (mxIsInt16(ptr)) { return "int16"; } if (mxIsInt32(ptr)) { return "int32"; } if (mxIsInt64(ptr)) { return "int64"; } if (mxIsUint8(ptr)) { return "uint8"; } if (mxIsUint16(ptr)) { return "uint16"; } if (mxIsUint32(ptr)) { return "uint32"; } if (mxIsUint64(ptr)) { return "uint64"; } if (mxIsCell(ptr)) { return "cell"; } if (mxIsStruct(ptr)) { return "struct"; } return "unknown"; }
/* tz_writetifstack(stack, filepath) * * Write tif stack. There is no output. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nrhs != 2) { mexErrMsgTxt("The function takes 2 argument exactly."); } if (!mxIsNumeric(prhs[0])) { mexErrMsgTxt("The first argument must be a numeric array."); } if (!mxIsUint8(prhs[0]) && !mxIsUint16(prhs[0])) { mexErrMsgTxt("The first argument must be a uint8 or uint16 array."); } if (!mxIsChar(prhs[1])) { mexErrMsgTxt("The second argument must be a string."); } char *file_path = mxArrayToString(prhs[1]); Array_Link *a = MxArray_To_Stack(prhs[0]); Stack *stack = Get_Stack_At(a); if (stack->text == NULL) { stack->text = "\0"; } Write_Stack(file_path, stack); Delete_Array_Link(a); }
/** * Inputs: filename (string), matrix */ static void _load_index(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[]) { if(nInArray != 2) { mexErrMsgTxt("Incorrect number of input arguments"); } // get the selector if(!mxIsChar(InArray[0])) { mexErrMsgTxt("'filename' should be a string"); } char filename[128]; mxGetString(InArray[0],filename,128); const mxArray* datasetMat = InArray[1]; check_allowed_type(datasetMat); int dcount = mxGetN(datasetMat); int length = mxGetM(datasetMat); TypedIndex* typedIndex = new TypedIndex(); if (mxIsSingle(datasetMat)) { float* dataset = copy_array<float>(datasetMat); typedIndex->index = flann_load_index_float(filename, dataset,dcount,length); typedIndex->type = FLANN_FLOAT32; typedIndex->dataset = dataset; } else if (mxIsDouble(datasetMat)) { double* dataset = copy_array<double>(datasetMat); typedIndex->index = flann_load_index_double(filename, dataset,dcount,length); typedIndex->type = FLANN_FLOAT64; typedIndex->dataset = dataset; } else if (mxIsUint8(datasetMat)) { unsigned char* dataset = copy_array<unsigned char>(datasetMat); typedIndex->index = flann_load_index_byte(filename, dataset,dcount,length); typedIndex->type = FLANN_UINT8; typedIndex->dataset = dataset; } else if (mxIsInt32(datasetMat)) { int* dataset = copy_array<int>(datasetMat); typedIndex->index = flann_load_index_int(filename, dataset,dcount,length); typedIndex->type = FLANN_INT32; typedIndex->dataset = dataset; } mxClassID classID; if (sizeof(flann_index_t)==4) { classID = mxUINT32_CLASS; } else if (sizeof(flann_index_t)==8) { classID = mxUINT64_CLASS; } /* Allocate memory for Output Matrix */ OutArray[0] = mxCreateNumericMatrix(1, 1, classID, mxREAL); /* Get pointer to Output matrix and store result*/ TypedIndex** pOut = (TypedIndex**)mxGetData(OutArray[0]); pOut[0] = typedIndex; }
int mxGetElementSize(const mxArray *ptr) { if (mxIsChar(ptr)) { return sizeof(wchar_t*); } else if (mxIsLogical(ptr)) { return sizeof(int); } else if (mxIsDouble(ptr)) { return sizeof(double); } else if (mxIsSparse(ptr)) { return sizeof(double); } else if (mxIsInt8(ptr)) { return sizeof(char); } else if (mxIsInt16(ptr)) { return sizeof(short); } else if (mxIsInt32(ptr)) { return sizeof(int); } else if (mxIsInt64(ptr)) { return sizeof(long long); } else if (mxIsUint8(ptr)) { return sizeof(unsigned char); } else if (mxIsUint16(ptr)) { return sizeof(unsigned short); } else if (mxIsUint32(ptr)) { return sizeof(unsigned int); } else if (mxIsUint64(ptr)) { return sizeof(unsigned long long); } else if (mxIsCell(ptr)) { return sizeof(types::InternalType*); } else if (mxIsStruct(ptr)) { return sizeof(types::SingleStruct*); } return 0; }
static MAPTYPE *get_maps_3dvol(const mxArray *ptr, int *n) { int num_dims, jj, t, dtype = 0; const mwSize *dims; MAPTYPE *maps; unsigned char *dptr; if (mxIsDouble(ptr)) dtype = SPM_DOUBLE; else if (mxIsSingle(ptr)) dtype = SPM_FLOAT; else if (mxIsInt32 (ptr)) dtype = SPM_SIGNED_INT; else if (mxIsUint32(ptr)) dtype = SPM_UNSIGNED_INT; else if (mxIsInt16 (ptr)) dtype = SPM_SIGNED_SHORT; else if (mxIsUint16(ptr)) dtype = SPM_UNSIGNED_SHORT; else if (mxIsInt8 (ptr)) dtype = SPM_SIGNED_CHAR; else if (mxIsUint8 (ptr)) dtype = SPM_UNSIGNED_CHAR; else mexErrMsgTxt("Unknown volume datatype."); maps = (MAPTYPE *)mxCalloc(1, sizeof(MAPTYPE)); num_dims = mxGetNumberOfDimensions(ptr); if (num_dims > 3) { mexErrMsgTxt("Too many dimensions."); } dims = mxGetDimensions(ptr); for(jj=0; jj<num_dims; jj++) maps->dim[jj]=dims[jj]; for(jj=num_dims; jj<3; jj++) maps->dim[jj]=1; for(jj=0; jj<16; jj++) maps->mat[jj] = 0.0; for(jj=0; jj<4; jj++) maps->mat[jj + jj*4] = 1.0; maps->dtype = dtype; maps->data = (void **)mxCalloc(maps->dim[2],sizeof(void *)); maps->scale = (double *)mxCalloc(maps->dim[2],sizeof(double)); maps->offset = (double *)mxCalloc(maps->dim[2],sizeof(double)); t = maps->dim[0]*maps->dim[1]*get_datasize(maps->dtype)/8; dptr = (unsigned char *)mxGetPr(ptr); for(jj=0; jj<maps->dim[2]; jj++) { maps->scale[jj] = 1.0; maps->offset[jj] = 0.0; maps->data[jj] = &(dptr[jj*t]); } maps->addr = 0; maps->len = 0; *n = 1; return(maps); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double x_std, y_std, w_smooth; double x_std_b, y_std_b, r_std, g_std, b_std, w_bilateral; int width, height, labels; // check input and output parameters if (nrhs < 12 ) { mexErrMsgTxt("12 inputs required (unary, color_image, x_std, y_std, w_smooth, x_std_bilateral, y_std_bilateral, r_std, g_std, b_std, w_bilateral, img_width, img_height)"); } // unary (be careful about c++ index and matlab index) if(!mxIsSingle(prhs[0])) { mexErrMsgTxt("Input matrix 1 must be float."); } float *unary = (float *) mxGetData(prhs[0]); labels = mxGetM(prhs[0]); if (!mxIsUint8(prhs[1])) { mexErrMsgTxt("Input matrix 2 must be uint8."); } if (mxGetM(prhs[1]) != 3) { mexErrMsgTxt("Input matrix 2 must have 3 channels (channel * width * height)."); } unsigned char * img = (unsigned char *) mxGetPr(prhs[1]); x_std = mxGetScalar(prhs[2]); y_std = mxGetScalar(prhs[3]); w_smooth = mxGetScalar(prhs[4]); x_std_b = mxGetScalar(prhs[5]); y_std_b = mxGetScalar(prhs[6]); r_std = mxGetScalar(prhs[7]); g_std = mxGetScalar(prhs[8]); b_std = mxGetScalar(prhs[9]); w_bilateral = mxGetScalar(prhs[10]); width = mxGetScalar(prhs[11]); height = mxGetScalar(prhs[12]); plhs[0] = mxCreateNumericMatrix(height, width, mxINT32_CLASS, mxREAL); plhs[1] = mxCreateNumericMatrix(height*width*labels, 1, mxSINGLE_CLASS, mxREAL); int *map = (int *)mxGetData(plhs[0]); float *prob = (float *)mxGetData(plhs[1]); int iter = 10; DenseCRF2D crf(width, height, labels); crf.setUnaryEnergy( unary); crf.addPairwiseGaussian( x_std, y_std, w_smooth); crf.addPairwiseBilateral( x_std_b, y_std_b, r_std, g_std, b_std, img, w_bilateral); crf.map(iter, map, prob); }
/* --- GATEWAY FUNCTION --- */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { char *action; unsigned char *IN = NULL, *OUT = NULL; size_t INlen, OUTlen; /* Check for proper number of arguments */ if (nrhs < 2) mexErrMsgTxt("Not enough input arguments."); else if (nrhs > 2) mexErrMsgTxt("Too many input arguments."); else if (nlhs > 1) mexErrMsgTxt("Too many output arguments."); /* The input ACTION must be a string */ if (!mxIsChar(prhs[0])) mexErrMsgTxt("Input ACTION must be a string."); action = mxArrayToString(prhs[0]); /* The input IN must be a real uint8 array */ if (!mxIsUint8(prhs[1]) || mxIsComplex(prhs[1])) mexErrMsgTxt("Input IN must be a real uint8 array."); INlen = mxGetNumberOfElements(prhs[1]); IN = mxGetData(prhs[1]); if (!strcmp(action,"D")) { /* Decompress data */ OUT = tinfl_decompress_mem_to_heap(IN, INlen, &OUTlen, TINFL_FLAG_PARSE_ZLIB_HEADER); if (OUT == NULL) mexErrMsgTxt("Error when decompressing data."); } else if (!strcmp(action,"C")) { /* Compress data */ OUT = tdefl_compress_mem_to_heap(IN, INlen, &OUTlen, TDEFL_WRITE_ZLIB_HEADER); if (OUT == NULL) mexErrMsgTxt("Error when compressing data."); } else { mexErrMsgTxt("Unknown ACTION type."); } /* */ plhs[0] = mxCreateNumericMatrix(OUTlen,1,mxUINT8_CLASS,mxREAL); if (plhs[0] == NULL) mexErrMsgTxt("Error when creating output variable."); memcpy(mxGetData(plhs[0]), OUT, OUTlen); mxFree(action); mz_free(OUT); }
static void check_allowed_type(const mxArray* datasetMat) { if (!mxIsSingle(datasetMat) && !mxIsDouble(datasetMat) && !mxIsUint8(datasetMat) && !mxIsInt32(datasetMat)) { mexErrMsgTxt("Data type must be floating point single precision, floating point double precision, " "8 bit unsigned integer or 32 bit signed integer"); } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { if (nlhs != 1 || nrhs < 3) { mexErrMsgTxt("Usage: [afValues] = fndllFastInterp2(a2Image, Cols (Nx1),Rows (Nx1), NaN value (optional))"); return; } float NaNValue = 0; if (nrhs == 4) { double *pNaNValue = (double *)mxGetData(prhs[3]); NaNValue = *pNaNValue; } /* Get the number of dimensions in the input argument. */ if (mxGetNumberOfDimensions(prhs[0]) != 2) { mexErrMsgTxt("Input volume must be 2D. "); return; } const int *input_dim_array = mxGetDimensions(prhs[0]); int in_rows = input_dim_array[0]; int in_cols = input_dim_array[1]; double *rows = (double *)mxGetData(prhs[2]); double *cols = (double *)mxGetData(prhs[1]); const int *tmp = mxGetDimensions(prhs[1]); int iNumPoints = MAX(tmp[0], tmp[1]); int output_dim_array[2]; output_dim_array[0] = iNumPoints; output_dim_array[1] = 1; plhs[0] = mxCreateNumericArray(2, output_dim_array, mxSINGLE_CLASS, mxREAL); float *output_vector = (float*)mxGetPr(plhs[0]); if (mxIsSingle(prhs[0])) { float *input_image = (float*)mxGetData(prhs[0]); CalcInterpolation(input_image, output_vector,iNumPoints, rows, cols, in_rows,in_cols,NaNValue); } if (mxIsDouble(prhs[0])) { double *input_image = (double*)mxGetData(prhs[0]); CalcInterpolation(input_image, output_vector,iNumPoints, rows, cols, in_rows,in_cols,NaNValue); } if (mxIsUint16(prhs[0]) || mxIsInt16(prhs[0])) { short *input_image = (short*)mxGetData(prhs[0]); CalcInterpolation(input_image, output_vector,iNumPoints, rows, cols, in_rows,in_cols,NaNValue); } if (mxIsUint8(prhs[0])) { unsigned char *input_image = (unsigned char *)mxGetData(prhs[0]); CalcInterpolation(input_image, output_vector,iNumPoints, rows, cols,in_rows,in_cols,NaNValue); } }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const int *dimsf, *dimsg; float s[3]; if (nrhs>4 || nrhs<3 || nlhs>1) mexErrMsgTxt("Incorrect usage."); if (!mxIsNumeric(prhs[0]) || !mxIsUint8(prhs[0]) || mxIsComplex(prhs[0])) mexErrMsgTxt("Wrong sort of data (1)."); if (mxGetNumberOfDimensions(prhs[0]) != 3) mexErrMsgTxt("Wrong number of dims (1).");; dimsg = mxGetDimensions(prhs[0]); if (!mxIsNumeric(prhs[1]) || !mxIsUint8(prhs[1]) || mxIsComplex(prhs[1])) mexErrMsgTxt("Wrong sort of data (2)."); if (mxGetNumberOfDimensions(prhs[0]) != 3) mexErrMsgTxt("Wrong number of dims (1).");; dimsf = mxGetDimensions(prhs[1]); if (!mxIsNumeric(prhs[2]) || !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2])) mexErrMsgTxt("Wrong sort of matrix."); if (mxGetM(prhs[2]) != 4 || mxGetN(prhs[2]) != 4) mexErrMsgTxt("Matrix must be 4x4."); if (nrhs == 4) { if (!mxIsNumeric(prhs[3]) || !mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxGetM(prhs[3])*mxGetN(prhs[3]) != 3) mexErrMsgTxt("Invalid skips."); s[0] = mxGetPr(prhs[3])[0]; s[1] = mxGetPr(prhs[3])[1]; s[2] = mxGetPr(prhs[3])[2]; } else { s[0] = s[1] = s[2] = 1.0; } plhs[0] = mxCreateDoubleMatrix(256,256,mxREAL); hist2(mxGetPr(prhs[2]), (unsigned char *)mxGetPr(prhs[0]), (unsigned char *)mxGetPr(prhs[1]), dimsg, dimsf, mxGetPr(plhs[0]), s); }
lcudaMatrix_8u mlcudaMxToMatrix_8u(const mxArray* mx) { if (!mxIsUint8(mx)) { mexErrMsgTxt("The input image must be of uint8 type"); } int width = mxGetM(mx); int height = mxGetN(mx); lcudaMatrix_8u matrix = lcudaAllocMatrix_8u(width, height); lcudaCpyToMatrix_8u((lcuda8u*)mxGetData(mx), matrix); return matrix; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int n; if (nrhs != 2) mexErrMsgTxt("Incorrect usage."); n = mxGetM(prhs[0])*mxGetN(prhs[0]); if (mxGetM(prhs[1])*mxGetN(prhs[1]) != n) mexErrMsgTxt("Incorrect usage."); if (!mxIsUint8(prhs[0])) mexErrMsgTxt("Incorrect usage."); if (!mxIsDouble(prhs[1])) mexErrMsgTxt("Incorrect usage."); plhs[0] = mxCreateDoubleMatrix(256,1,mxREAL); create_hist(n, (unsigned char *)mxGetPr(prhs[0]), mxGetPr(prhs[1]),mxGetPr(plhs[0])); }
BitMatrix* toBitMatrix(BitMatrix *m,mxArray *zeroplaces){ /* Dispatcher for converting matlab matrices to bitmatrix. Calls the two functions above, depending on the type of matlab matrix */ if(mxIsUint8(zeroplaces)){ /*printf("using bytes\n");*/ return uint8ToBitMatrix(m,zeroplaces); }else{ /*printf("using doubles\n");*/ return doubleToBitMatrix(m,zeroplaces); } }
void mxSetImagData(mxArray *array_ptr, void *data_ptr) { if (mxIsChar(array_ptr)) { ((String *)array_ptr)->setImg((wchar_t **)data_ptr); } else if (mxIsDouble(array_ptr)) { ((Double *)array_ptr)->setImg((double *)data_ptr); } else if (mxIsInt8(array_ptr)) { ((Int8 *)array_ptr)->setImg((char *)data_ptr); } else if (mxIsInt16(array_ptr)) { ((Int16 *)array_ptr)->setImg((short *)data_ptr); } else if (mxIsInt32(array_ptr)) { ((Int32 *)array_ptr)->setImg((int *)data_ptr); } else if (mxIsInt64(array_ptr)) { ((Int64 *)array_ptr)->setImg((long long *)data_ptr); } else if (mxIsLogical(array_ptr)) { ((Bool *)array_ptr)->setImg((int *)data_ptr); } // else if (mxIsSingle(array_ptr)) { // ((Float *) array_ptr)->setImg((float *) data_ptr); // } else if (mxIsUint8(array_ptr)) { ((UInt8 *)array_ptr)->setImg((unsigned char *)data_ptr); } else if (mxIsUint16(array_ptr)) { ((UInt16 *)array_ptr)->setImg((unsigned short *)data_ptr); } else if (mxIsUint32(array_ptr)) { ((UInt32 *)array_ptr)->setImg((unsigned int *)data_ptr); } else if (mxIsUint64(array_ptr)) { ((UInt64 *)array_ptr)->setImg((unsigned long long *) data_ptr); } }
/* Function: getInputImageDepth * * Description: Get the depth of the input image. * * Parameters: * inputImage: The input image, stored in an mxArray. * * Returns: The enum of the depth of the input image (UINT8, UINT16, or * UNSUPPORTED_DEPTH). */ enum ImageDepth getInputImageDepth(_In_ const mxArray *inputImageData) { // check what type of data is contained in mxArray of image data if (mxIsUint8(inputImageData)) { return UINT8; } else if (mxIsUint16(inputImageData)) { return UINT16; } else { return UNSUPPORTED_DEPTH; } }
void mxSetImagData(mxArray *array_ptr, void *data_ptr) { if (mxIsChar(array_ptr)) { ((types::String *)array_ptr)->setImg((wchar_t **)data_ptr); } else if (mxIsDouble(array_ptr)) { ((types::Double *)array_ptr)->setImg((double *)data_ptr); } else if (mxIsInt8(array_ptr)) { ((types::Int8 *)array_ptr)->setImg((char *)data_ptr); } else if (mxIsInt16(array_ptr)) { ((types::Int16 *)array_ptr)->setImg((short *)data_ptr); } else if (mxIsInt32(array_ptr)) { ((types::Int32 *)array_ptr)->setImg((int *)data_ptr); } else if (mxIsInt64(array_ptr)) { ((types::Int64 *)array_ptr)->setImg((long long *)data_ptr); } else if (mxIsLogical(array_ptr)) { ((types::Bool *)array_ptr)->setImg((int *)data_ptr); } else if (mxIsUint8(array_ptr)) { ((types::UInt8 *)array_ptr)->setImg((unsigned char *)data_ptr); } else if (mxIsUint16(array_ptr)) { ((types::UInt16 *)array_ptr)->setImg((unsigned short *)data_ptr); } else if (mxIsUint32(array_ptr)) { ((types::UInt32 *)array_ptr)->setImg((unsigned int *)data_ptr); } else if (mxIsUint64(array_ptr)) { ((types::UInt64 *)array_ptr)->setImg((unsigned long long *) data_ptr); } }
static void newImageOverSegmentation( MEX_ARGS ) { if( nlhs == 0 ) { mexErrMsgTxt("newImageOverSegmentation expected one return argument"); return; } if( nrhs < 1 ) { mexErrMsgTxt( "Expected arguments: Image:HxWx3 uint8-array [,N_SPIX:int]" ); return ; } // Read the image const mwSize * dims = mxGetDimensions(prhs[0]); if( mxGetNumberOfDimensions(prhs[0])!=3 || !mxIsUint8(prhs[0]) || dims[2]<3 ){ mexErrMsgTxt( "HxWx3 uint8 image required" ); return ; } int W = dims[1], H = dims[0]; Image8u im( W, H, 3 ); // For some reason MATLAB likes the fortran order uint8_t* pim = (uint8_t*)mxGetData(prhs[0]); for( int j=0; j<H; j++ ) for( int i=0; i<W; i++ ) for( int c=0; c<3; c++ ) im(j,i,c) = pim[c*W*H+i*H+j]; // Read the number of superpixels int n_spix=1000; if( nrhs > 1 && mxIsNumeric(prhs[1]) ) n_spix = mxGetScalar(prhs[1]); if( nrhs > 3 ) { RMatrixXf bnd[2]; for( int i=2; i<4; i++ ) { const mwSize * dims2 = mxGetDimensions(prhs[i]); if( mxGetNumberOfDimensions(prhs[i])!=2 || !mxIsSingle(prhs[i]) || dims2[0]!=dims[0] || dims2[1]!=dims[1] ) mexErrMsgTxt( "Expected arguments: Image:HxWx3 uint8-array N_SPIX:int ThickBoundryMap:HxW single-array ThinBoundryMap:HxW single-array" ); float * pBnd = (float *)mxGetData(prhs[i]); bnd[i-2] = MatrixXf::Map( pBnd, H, W ); } plhs[0] = ptr2Mat( geodesicKMeans( im, bnd[0], bnd[1], n_spix ) ); } else { if( !detector ) detector = detectorFromString("MultiScaleStructuredForest(\"../data/sf.dat\")"); // Create the OverSegmentation plhs[0] = ptr2Mat( geodesicKMeans( im, *detector, n_spix ) ); } }
static void ImageOverSegmentation_unserialize( MEX_ARGS ) { if( nrhs != 2 ) { mexErrMsgTxt( "Expected a ImageOverSegmentation and a buffer" ); return ; } if( nlhs > 0 ) { mexErrMsgTxt( "Expected no return argument" ); return ; } if( !mxIsUint8(prhs[1]) ) { mexErrMsgTxt( "Can only unserialize an 8-bit unsigned int array" ); return ; } std::shared_ptr<ImageOverSegmentation> os = mat2Ptr<ImageOverSegmentation>( prhs[0] ); std::stringstream ss( std::string( (char*) mxGetData(prhs[1]), mxGetM(prhs[1]) ) ); os->load( ss ); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { nifti_1_header *NH; int size; if (nrhs!=1 || !mxIsUint8(prhs[0])) mexErrMsgTxt("This function needs exactly one (uint8) argument."); size = mxGetNumberOfElements(prhs[0]); if (size < sizeof(nifti_1_header)) { plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL); return; } NH = (nifti_1_header *) mxGetData(prhs[0]); if (strcmp(NH->magic, "ni1")==0 || strcmp(NH->magic,"n+1")==0) { plhs[0] = createFromNifti1(NH); return; } plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL); }
mxArray * mexthinmain(const mxArray *input) { mxArray * input_cube; mxArray * fout; mxArray * mx_ma_loc; mwSize nx,ny,nz,number_of_dims; const mwSize * pind; unsigned char *ma_bno; UINT32 nloc, *ma_loc; input_cube = mxGetCell(input,0); number_of_dims = mxGetNumberOfDimensions(input_cube); if (number_of_dims==2) { pind = mxGetDimensions(input_cube); nx=pind[0]; ny=pind[1]; nz=1; } else if (number_of_dims==3) { pind = mxGetDimensions(input_cube); nx=pind[0]; ny=pind[1]; nz=pind[2]; } else mexErrMsgTxt("Input must be 2 or 3 dimensional"); if (mxIsUint8(input_cube) == 0 ) mexErrMsgTxt("Input must be uint8 type."); thin_driver(nx,ny,nz,mxGetData(input_cube), &nloc, &ma_loc, &ma_bno); mx_ma_loc = mxCreateNumericMatrix(nloc, 1, mxUINT32_CLASS, mxREAL); mxSetData(mx_ma_loc,ma_loc); fout = mxCreateCellMatrix(1, 1); mxSetCell(fout,0,mx_ma_loc); return fout; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nrhs != 3) { mexErrMsgTxt("Must have 3 inputs"); } if (nlhs != 0) { mexErrMsgTxt("Must have 0 outputs"); } // Read images if (!mxIsUint8(prhs[0]) || mxGetNumberOfDimensions(prhs[0]) != 4) { mexErrMsgTxt("images must be a 4D uint8 array"); } uint8_t* images = (uint8_t*) mxGetData(prhs[0]); const mwSize* images_size = mxGetDimensions(prhs[0]); int width = images_size[0]; int height = images_size[1]; int num_channels = images_size[2]; if (num_channels != 3) { mexErrMsgTxt("Expected 3 channels for images"); } int num_images = images_size[3]; int img_length = width * height * num_channels; std::cout << "Image info: width: " << width << " height: " << height << " channels: " << num_channels << " num: " << num_images << std::endl; // Read serialized triplets if (!mxIsInt32(prhs[1]) || mxGetN(prhs[1]) != 1) { mexErrMsgTxt("triplets must have type int32 and have 1 column"); } int32_t* serialized_triplets = (int32_t*) mxGetData(prhs[1]); int num_examples = mxGetM(prhs[1]); // Read db filename char* db_filename = mxArrayToString(prhs[2]); printf("%s\n", db_filename); google::InitGoogleLogging("triplets_to_caffedb"); process(images, num_images, width, height, serialized_triplets, num_examples, db_filename); mxFree(db_filename); }
static void check_argin(int nrhs, const mxArray *prhs[], int *option) { if ((nrhs < 1) || (nrhs > 2)) { mexErrMsgTxt("tz_stacklocmax takes 1~2 argument."); } if (!mxIsUint8(prhs[0]) && !mxIsUint16(prhs[0]) && !mxIsSingle(prhs[0]) && !mxIsDouble(prhs[0])) mexErrMsgTxt("The first argument must be an uint8, uint16, single or double array."); mwSize nd = mxGetNumberOfDimensions(prhs[0]); if (nd > 3) { mexErrMsgTxt("The first argument must have no greater than 3 dimensions."); } if (nrhs == 2) { if (!mxIsChar(prhs[1])) { mexErrMsgTxt("The second argument must be a string."); } else { int strlen = tz_mxGetL(prhs[1]) + 1; char str[10]; mxGetString(prhs[1], str, strlen); if (strcmp(str, "center") == 0) { *option = STACK_LOCMAX_CENTER; } else if (strcmp(str, "neighbor") == 0) { *option = STACK_LOCMAX_NEIGHBOR; } else if (strcmp(str, "nonflat") == 0) { *option = STACK_LOCMAX_NONFLAT; } else if (strcmp(str, "alter1") == 0) { *option = STACK_LOCMAX_ALTER1; } else if (strcmp(str, "alter2") == 0) { *option = STACK_LOCMAX_ALTER2; } else { mexErrMsgTxt("The second argument has an invalid value."); } } } else { *option = STACK_LOCMAX_ALTER1; } }
// ********************************************************************* // * Deserialize * // ********************************************************************* // data = classifier_deserialize (id, stream) static void classifier_deserialize (int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs) { // Validate arguments if (nrhs != 2) { mexErrMsgTxt("Command requires two input arguments!"); } if (mxGetNumberOfElements(prhs[0]) != 1 || !mxIsNumeric(prhs[0])) { mexErrMsgTxt("First input argument needs to be a numeric ID!"); } if (!mxIsUint8(prhs[1])) { mexErrMsgTxt("Second input argument needs to be uint8 array!"); } if (nlhs != 0) { mexErrMsgTxt("Command requires no output arguments!"); } // Get handle ID int id = static_cast<int>(mxGetScalar(prhs[0])); // Try to find the classifier auto iterator = objects.find(id); if (iterator == objects.end()) { mexErrMsgTxt("Invalid handle ID!"); } auto &classifier = iterator->second; // Copy input data into stream std::stringstream stream; stream.write(static_cast<char *>(mxGetData(prhs[1])), mxGetNumberOfElements(prhs[1])); // Deserialize stream classifier->loadFromStream(stream); }
static void get_map_dat(int i, const mxArray *ptr, MAPTYPE *maps) { mxArray *tmp; double *pr; int num_dims, j, t, dtype = 0; const mwSize *dims; unsigned char *dptr; tmp=mxGetField(ptr,i,"dat"); if (tmp == (mxArray *)0) { free_maps(maps,i); mexErrMsgTxt("Cant find dat."); } if (mxIsDouble(tmp)) dtype = SPM_DOUBLE; else if (mxIsSingle(tmp)) dtype = SPM_FLOAT; else if (mxIsInt32 (tmp)) dtype = SPM_SIGNED_INT; else if (mxIsUint32(tmp)) dtype = SPM_UNSIGNED_INT; else if (mxIsInt16 (tmp)) dtype = SPM_SIGNED_SHORT; else if (mxIsUint16(tmp)) dtype = SPM_UNSIGNED_SHORT; else if (mxIsInt8 (tmp)) dtype = SPM_SIGNED_CHAR; else if (mxIsUint8 (tmp)) dtype = SPM_UNSIGNED_CHAR; else { free_maps(maps,i); mexErrMsgTxt("Unknown volume datatype."); } dptr = (unsigned char *)mxGetPr(tmp); num_dims = mxGetNumberOfDimensions(tmp); if (num_dims > 3) { free_maps(maps,i); mexErrMsgTxt("Too many dimensions."); } dims = mxGetDimensions(tmp); for(j=0; j<num_dims; j++) maps[i].dim[j]=dims[j]; for(j=num_dims; j<3; j++) maps[i].dim[j]=1; tmp=mxGetField(ptr,i,"dim"); if (tmp != (mxArray *)0) { if (mxGetM(tmp)*mxGetN(tmp) != 3) { free_maps(maps,i); mexErrMsgTxt("Wrong sized dim."); } pr = mxGetPr(tmp); if (maps[i].dim[0] != (mwSize)fabs(pr[0]) || maps[i].dim[1] != (mwSize)fabs(pr[1]) || maps[i].dim[2] != (mwSize)fabs(pr[2])) { free_maps(maps,i); mexErrMsgTxt("Incompatible volume dimensions in dim."); } } tmp=mxGetField(ptr,i,"dt"); if (tmp != (mxArray *)0) { if (mxGetM(tmp)*mxGetN(tmp) != 1 && mxGetM(tmp)*mxGetN(tmp) != 2) { free_maps(maps,i); mexErrMsgTxt("Wrong sized dt."); } pr = mxGetPr(tmp); if (dtype != (int)fabs(pr[0])) { free_maps(maps,i); mexErrMsgTxt("Incompatible datatype in dt."); } } maps[i].addr = 0; maps[i].len = 0; maps[i].dtype = dtype; maps[i].data = (void **)mxCalloc(maps[i].dim[2],sizeof(void *)); maps[i].scale = (double *)mxCalloc(maps[i].dim[2],sizeof(double)); maps[i].offset = (double *)mxCalloc(maps[i].dim[2],sizeof(double)); t = maps[i].dim[0]*maps[i].dim[1]*get_datasize(maps[i].dtype)/8; tmp = mxGetField(ptr,i,"pinfo"); if (tmp != (mxArray *)0) { if ((mxGetM(tmp) != 2 && mxGetM(tmp) != 3) || (mxGetN(tmp) != 1 && mxGetN(tmp) != maps[i].dim[2])) { free_maps(maps,i+1); mexErrMsgTxt("Wrong sized pinfo."); } if (mxGetM(tmp) == 3 && mxGetPr(tmp)[2] != 0) { free_maps(maps,i+1); mexErrMsgTxt("pinfo(3) must equal 0 to read dat field."); } pr = mxGetPr(tmp); if (mxGetN(tmp) == 1) for(j=0; j<maps[i].dim[2]; j++) { maps[i].scale[j] = pr[0]; maps[i].offset[j] = pr[1]; maps[i].data[j] = &(dptr[j*t]); } else for(j=0; j<maps[i].dim[2]; j++) { maps[i].scale[j] = pr[0+j*2]; maps[i].offset[j] = pr[1+j*2]; maps[i].data[j] = &(dptr[j*t]); } } else for(j=0; j<maps[i].dim[2]; j++) { maps[i].scale[j] = 1.0; maps[i].offset[j] = 0.0; maps[i].data[j] = &(dptr[j*t]); } tmp=mxGetField(ptr,i,"mat"); if (tmp != (mxArray *)0) { if (mxGetM(tmp) != 4 || mxGetN(tmp) != 4) { free_maps(maps,i+1); mexErrMsgTxt("Wrong sized mat."); } pr = mxGetPr(tmp); for(j=0; j<16; j++) maps[i].mat[j] = pr[j]; } else { for(j=0; j<16; j++) maps[i].mat[j] = 0.0; for(j=0; j<4; j++) maps[i].mat[j + j*4] = 1.0; } }
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { unsigned char *X8, *B8; double *X, *B; int N; int k; double *y; //-- // UINT8 image //-- if (mxIsUint8(prhs[0])) { //-- // INPUT //-- // input image X8 = (unsigned char *) mxGetPr(prhs[0]); N = mxGetM(prhs[0]) * mxGetN(prhs[0]); //-- // OUTPUT //-- // median y = mxGetPr(plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL)); //-- // COMPUTATION //-- // buffer image B8 = mxCalloc(N,sizeof(unsigned char)); for (k = 0; k < N; k++) { *(B8 + k) = *(X8 + k); } // compute median if (N % 2) { *y = (double) median_uint8(B8,N); } else { *y = ((double) kth_smallest_uint8(B8,N,N/2 - 1) + (double) kth_smallest_uint8(B8,N,N/2)) / 2.0; } } else { //-- // INPUT //-- // input image X = mxGetPr(prhs[0]); N = mxGetM(prhs[0]) * mxGetN(prhs[0]); //-- // OUTPUT //-- // median y = mxGetPr(plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL)); //-- // COMPUTATION //-- // buffer image B = mxCalloc(N,sizeof(double)); for (k = 0; k < N; k++) { *(B + k) = *(X + k); } // compute median if (N % 2) { *y = median_double(B,N); } else { *y = (kth_smallest_double(B,N,N/2 - 1) + kth_smallest_double(B,N,N/2)) / 2.0; } } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // **************** PARSE THE INPUTS ******************* // myAssert(nrhs==4,"fitStumpUINT8_c: bad nrhs"); const mxArray* mxX = prhs[0]; myAssert(mxIsUint8(mxX), "fitStumpUINT8_c: X must be uint8"); unsigned char* X = (unsigned char*) mxGetPr(mxX); int N = mxGetM(mxX); int p = mxGetN(mxX); const mxArray* mxWWY = prhs[1]; myAssert(mxIsDouble(prhs[1]), "fitStumpUINT8_c: wwy must be double"); double* wwy = (double*) mxGetPr(mxWWY); myAssert(mxGetM(mxWWY)==2, "fitStumpUINT8_c: wwy must be 2 x N"); myAssert(mxGetN(mxWWY)==N, "fitStumpUINT8_c: wwy must be 2 x N"); const mxArray* mxCandVars = prhs[2]; myAssert(mxIsUint32(mxCandVars), "fitStumpUINT8_c: mxCandVars must be uint32"); unsigned int* candVars = (unsigned int*) mxGetPr(mxCandVars); int nCand = mxGetNumberOfElements(mxCandVars); const mxArray* mxGoodInd = prhs[3]; int nGd = mxGetNumberOfElements(mxGoodInd); myAssert(nGd==0 || mxIsUint32(mxGoodInd), "fitStumpUINT8_c: mxGoodInd must be uint32"); unsigned int* goodInd = (unsigned int*) mxGetPr(mxGoodInd); // ****************** SET UP THE OUTPUTS ******************* // plhs[0] = mxCreateNumericMatrix(1,nCand,mxINT32_CLASS,mxREAL); int* cutInd = (int*) mxGetPr(plhs[0]); plhs[1] = mxCreateNumericMatrix(1,nCand,mxDOUBLE_CLASS, mxREAL); double* ssxBest = (double*) mxGetPr(plhs[1]); plhs[2] = mxCreateNumericMatrix(1,nCand,mxDOUBLE_CLASS,mxREAL); double* muL = (double*) mxGetPr(plhs[2]); plhs[3] = mxCreateNumericMatrix(1,nCand,mxDOUBLE_CLASS,mxREAL); double* muR = (double*) mxGetPr(plhs[3]); // ************** MAIN LOOP OVER ALL CANDIDATE VARS *********** // for(int m=0; m<nCand; m++) { unsigned char* x = X + N*candVars[m]; double* wwyBuck = (double *) mxMalloc(2*256*sizeof(double)); // fill weights with small epsilon for numerical stability for(int i=0; i<256; i++) { wwyBuck[i*2] = 1.0E-10; wwyBuck[i*2+1] = 0; } // make weighted histogram of w and wy buckSums(wwyBuck, wwy, N, x, goodInd, nGd); // cumsum __m128d* wwyBuck128 = (__m128d*) wwyBuck; for(int i=1; i<256; i++) wwyBuck128[i] = _mm_add_pd(wwyBuck128[i], wwyBuck128[i-1]); // compute -ssx __m128d wCumEnd = _mm_set_pd(wwyBuck[256*2-2], wwyBuck[256*2-2]); __m128d wyCumEnd = _mm_set_pd(wwyBuck[256*2-1], wwyBuck[256*2-1]); __m128d* ssx128 = (__m128d*) mxMalloc(1*256*sizeof(__m128d)); for(int i=0; i<128; i++) { __m128d wwyBuck1 = wwyBuck128[i*2]; __m128d wwyBuck2 = wwyBuck128[i*2+1]; __m128d wyBuck = _mm_unpackhi_pd(wwyBuck1,wwyBuck2); __m128d wBuck = _mm_unpacklo_pd(wwyBuck1,wwyBuck2); ssx128[i] = _mm_div_pd(_mm_mul_pd(wyBuck,wyBuck),wBuck); __m128d tmp1 = _mm_sub_pd(wyCumEnd,wyBuck); tmp1 = _mm_mul_pd(tmp1,tmp1); __m128d tmp2 = _mm_sub_pd(wCumEnd,wBuck); ssx128[i] = _mm_add_pd(ssx128[i],_mm_div_pd(tmp1,tmp2)); } // find best split location for this candidate variable double* ssx = (double*) ssx128; double mx = ssx[0]; cutInd[m] = 0; for(int i=1;i<256;i++) { if(ssx[i] > mx) { mx = ssx[i]; cutInd[m] = i; } } ssxBest[m] = -mx; muL[m] = wwyBuck[cutInd[m]*2+1] / wwyBuck[cutInd[m]*2]; muR[m] = (wwyBuck[256*2-1] - wwyBuck[cutInd[m]*2+1]) / (wwyBuck[256*2-2] - wwyBuck[cutInd[m]*2]); } }
/*-------------------------------------------------------------------------------------------------------------- */ void mexFunction( int nlhs, mxArray *plhs[] , int nrhs, const mxArray *prhs[] ) { unsigned char *I; const int *dimsI; int numdimsI; struct model detector; mxArray *mxtemp; double rect_param_default[40] = {1 , 1 , 2 , 2 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 0 , 1 , 1 , 1 , -1 , 2 , 2 , 1 , 2 , 1 , 0 , 0 , 1 , 1 , -1 , 2 , 2 , 1 , 2 , 2 , 1 , 0 , 1 , 1 , 1}; int i , Ny , Nx , N = 1; double *z; int numdimsz = 2; int *dimsz; detector.ny = 24; detector.nx = 24; detector.nR = 4; /* Input 1 */ if ((nrhs > 0) && !mxIsEmpty(prhs[0]) && mxIsUint8(prhs[0])) { dimsI = mxGetDimensions(prhs[0]); numdimsI = mxGetNumberOfDimensions(prhs[0]); I = (unsigned char *) mxGetData(prhs[0]); Ny = dimsI[0]; Nx = dimsI[1]; if(numdimsI > 2) { N = dimsI[2]; } } else { mexErrMsgTxt("I must be (Ny x Nx x N) in DOUBLE format"); } /* Input 2 */ if ((nrhs > 1) && !mxIsEmpty(prhs[1]) ) { mxtemp = mxGetField( prhs[1] , 0, "dimsItraining" ); if(mxtemp != NULL) { detector.dimsItraining = mxGetPr(mxtemp); detector.ny = (int)detector.dimsItraining[0]; detector.nx = (int)detector.dimsItraining[1]; if ((Ny < detector.ny ) || (Nx < detector.nx )) { mexErrMsgTxt("I must be at least nyxnx"); } } mxtemp = mxGetField( prhs[1] , 0, "rect_param" ); if(mxtemp != NULL) { if((mxGetM(mxtemp) != 10) || !mxIsDouble(mxtemp) ) { mexErrMsgTxt("rect_param must be (10 x nR) in DOUBLE format"); } detector.rect_param = (double *) mxGetData(mxtemp); detector.nR = mxGetN(mxtemp); } else { detector.rect_param = (double *)mxMalloc(40*sizeof(double)); for(i = 0 ; i < 40 ; i++) { detector.rect_param[i] = rect_param_default[i]; } } mxtemp = mxGetField( prhs[1] , 0, "F" ); if(mxtemp != NULL) { detector.F = (unsigned int *)mxGetData(mxtemp); detector.nF = mxGetN(mxtemp); } else { detector.nF = number_haar_features(detector.ny , detector.nx , detector.rect_param , detector.nR); detector.F = (unsigned int *)mxMalloc(6*detector.nF*sizeof(int)); haar_featlist(detector.ny , detector.nx , detector.rect_param , detector.nR , detector.F); } } else { detector.rect_param = (double *)mxMalloc(40*sizeof(double)); for(i = 0 ; i < 40 ; i++) { detector.rect_param[i] = rect_param_default[i]; } detector.nF = number_haar_features(detector.ny , detector.nx , detector.rect_param , detector.nR); detector.F = (unsigned int *)mxMalloc(6*detector.nF*sizeof(int)); haar_featlist(detector.ny , detector.nx , detector.rect_param , detector.nR , detector.F); } /*----------------------- Outputs -------------------------------*/ /* Output 1 */ dimsz = (int *)mxMalloc(2*sizeof(int)); dimsz[0] = detector.nF; dimsz[1] = N; plhs[0] = mxCreateNumericArray(numdimsz , dimsz , mxDOUBLE_CLASS , mxREAL); z = mxGetPr(plhs[0]); /*------------------------ Main Call ----------------------------*/ haar_scale(I , Ny , Nx , N , detector , z); /*----------------- Free Memory --------------------------------*/ if ((nrhs > 1) && !mxIsEmpty(prhs[1]) ) { if ( (mxGetField( prhs[1] , 0 , "rect_param" )) == NULL ) { mxFree(detector.rect_param); } if ( (mxGetField( prhs[1] , 0 , "F" )) == NULL ) { mxFree(detector.F); } } else { mxFree(detector.rect_param); mxFree(detector.F); } mxFree(dimsz); }
/** * Input arguments: dataset (matrix), params (struct) * Output arguments: index (pointer to index), params (struct), speedup(double) */ static void _build_index(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[]) { /* Check the number of input arguments */ if(nInArray != 2) { mexErrMsgTxt("Incorrect number of input arguments"); } /* Check the number of output arguments */ if ((nOutArray == 0)||(nOutArray > 3)) { mexErrMsgTxt("Incorrect number of outputs."); } const mxArray* datasetMat = InArray[0]; check_allowed_type(datasetMat); int dcount = mxGetN(datasetMat); int length = mxGetM(datasetMat); const mxArray* pStruct = InArray[1]; /* get index parameters */ FLANNParameters p; matlabStructToFlannStruct(pStruct, p); float speedup = -1; TypedIndex* typedIndex = new TypedIndex(); if (mxIsSingle(datasetMat)) { float* dataset = (float*) mxGetData(datasetMat); typedIndex->index = flann_build_index_float(dataset,dcount,length, &speedup, &p); typedIndex->type = FLANN_FLOAT32; } else if (mxIsDouble(datasetMat)) { double* dataset = (double*) mxGetData(datasetMat); typedIndex->index = flann_build_index_double(dataset,dcount,length, &speedup, &p); typedIndex->type = FLANN_FLOAT64; } else if (mxIsUint8(datasetMat)) { unsigned char* dataset = (unsigned char*) mxGetData(datasetMat); typedIndex->index = flann_build_index_byte(dataset,dcount,length, &speedup, &p); typedIndex->type = FLANN_UINT8; } else if (mxIsInt32(datasetMat)) { int* dataset = (int*) mxGetData(datasetMat); typedIndex->index = flann_build_index_int(dataset,dcount,length, &speedup, &p); typedIndex->type = FLANN_INT32; } mxClassID classID; if (sizeof(flann_index_t)==4) { classID = mxUINT32_CLASS; } else if (sizeof(flann_index_t)==8) { classID = mxUINT64_CLASS; } /* Allocate memory for Output Matrix */ OutArray[0] = mxCreateNumericMatrix(1, 1, classID, mxREAL); /* Get pointer to Output matrix and store result*/ TypedIndex** pOut = (TypedIndex**)mxGetData(OutArray[0]); pOut[0] = typedIndex; if (nOutArray > 1) { OutArray[1] = flannStructToMatlabStruct(p); } if (nOutArray > 2) { OutArray[2] = mxCreateDoubleMatrix(1, 1, mxREAL); double* pSpeedup = mxGetPr(OutArray[2]); *pSpeedup = speedup; } }