void load_image(char *path, float **pImage, int *pnx, int *pny) { unsigned char *temp; float *image; char dataIn[MAXLEN], headIn[MAXLEN]; int ixy, i, j, sizeInput; int nx, ny; float min, max, scale; char *datatail=".bin", *headtail=".hdr"; FILE *fpIn; /* Read header file */ strcpy(headIn, ""); strcpy(dataIn, ""); strcat(strcat(headIn, path), headtail); fprintf(stderr, "headIn: %s\n", headIn); if ( (fpIn = fopenInputFile(headIn)) == NULL ) { error(" cannot open header input file ", headIn); } else { readOneInt(fpIn, &nx); readOneInt(fpIn, &ny); readOneFloat(fpIn, &min); readOneFloat(fpIn, &max); readOneFloat(fpIn, &scale); fprintf(stderr, "nx: %d\n", nx); fprintf(stderr, "ny: %d\n", ny); } fclose(fpIn); /* Read image file */ strcat(strcat(dataIn, path), datatail); fprintf(stderr, "dataIn: %s, path: %s \n\n", dataIn, path); if ((fpIn = freopenInputFile(dataIn, fpIn)) == NULL) error("Can't open input files, load_image %s", dataIn); grabByteMemory((char **)&temp, nx*ny, "loadImage: temp"); grabFloatMemory(pImage, nx*ny, "loadImage: pImage"); image = *pImage; sizeInput = nx*ny; if (!fread((char *) temp, sizeInput, 1, fpIn)) { utilFree((void **) &temp); utilFree((void **) pImage); *pnx = *pny = 0; error("unexpected end of file on", dataIn); } *pnx = nx; *pny = ny; for(i=0;i<ny;i++){ for(j=0;j<nx;j++){ ixy = (i*nx) + j; image[ixy] = (((float) temp[ixy]) / scale) + (float) min; } } utilFree((void **) &temp); fclose(fpIn); }
int read_ppm_image(unsigned char *pimage[3], FILE *fpIn, char *fnIn, int *pnx, int *pny) { unsigned char *raw; int i0, ixy, sizeInput; /* Read image size off of pgm header, if possible */ if (stripPpmHeader(fpIn, pnx, pny) < 0) return(-1); sizeInput = (*pnx) * (*pny) * sizeof( char ); grabByteMemory((char **)pimage, sizeInput, fnIn); grabByteMemory((char **)pimage+1, sizeInput, fnIn); grabByteMemory((char **)pimage+2, sizeInput, fnIn); sizeInput = 3 * (*pnx) * (*pny) * sizeof( char ); grabByteMemory((char **)&raw, sizeInput, fnIn); if ( !fread((char *) raw, sizeInput,1,fpIn)) error("unexpected end of file on", fnIn); for(ixy=0; ixy< (*pnx) * (*pny); ixy++) { i0 = 3 * ixy; pimage[0][ixy] = raw[i0]; pimage[1][ixy] = raw[i0+1]; pimage[2][ixy] = raw[i0+2]; } utilFree((void **) &raw); return(0); }
void save_ppm_image(char *path, unsigned char *cImage[3], int nx, int ny) { unsigned char *raw; char dataOut[MAXLEN], header[MAXLEN]; int sizeOutput, nHead, ixy, k; FILE *outfile; grabByteMemory((char **) &raw, 3 * nx * ny, "rawColourImage"); strcpy(dataOut, ""); sprintf(dataOut,"%d %d\n%d\n", nx, ny, 255); strcpy(header, "P6\n"); nHead = strlen(header) + strlen(dataOut); while(nHead < minHeadSize) { strcat(header, blank); nHead++; } strcat(header, dataOut); /* Open image */ strcpy(dataOut,""); strcat(strcat(dataOut, path), ppmFileTail); if ( (outfile = fopen(dataOut, "wb")) == NULL ) error("Can't open output file", dataOut); /* Save header */ sizeOutput = strlen(header) * sizeof( char ); if ( ! fwrite(header, sizeOutput, 1, outfile)) error("Unable to write", dataOut); /* concatenate RBG byte images */ for(ixy=0; ixy<nx * ny; ixy++) { k = 3*ixy; raw[k] = cImage[0][ixy]; raw[k+1] = cImage[1][ixy]; raw[k+2] = cImage[2][ixy]; } /* write raw byte image in output image file */ sizeOutput = 3 * nx * ny * sizeof( char ); if ( ! fwrite((char *) raw, sizeOutput, 1, outfile)) error("Unable to write", dataOut); fclose(outfile); utilFree((void **) &raw); }
void save_byte_image_from_float_scaled(char *path, float *imageFlt, int nx, int ny, float crop[2], float out_of_range[2]) { char dataOut[MAXLEN], headOut[MAXLEN]; int sizeOutput; float par[2]; unsigned char *image; FILE *outfile; if (crop[1] < crop[0]) { /* Do not perform cropping */ range_float_image(imageFlt, nx, ny, crop); out_of_range[0] = 0.0; out_of_range[1] = 1.0; } quantize_crop_float_image(&image, imageFlt, nx, ny, crop, out_of_range, par); strcpy(headOut, ""); strcat(strcat(headOut, path), headerFileTail); if ((outfile = fopen(headOut, "wb")) == NULL) error("Unable to open file", headOut); fprintf(stderr, "%d %d %f %f %f %f %f\n", nx,ny, par[0], par[0]+255.0/par[1], par[1], crop[0], crop[1]); fprintf(outfile, "%d %d %f %f %f %f %f\n", nx,ny, par[0], par[0]+255.0/par[1], par[1], crop[0], crop[1]); fclose(outfile); /* Save byte image */ strcpy(dataOut,""); strcat(strcat(dataOut, path), byteFileTail); if ( (outfile = fopen(dataOut, "wb")) == NULL) error("Can't open output file", dataOut); sizeOutput = nx * ny * sizeof( char ); if (! fwrite((char *) image, sizeOutput, 1, outfile)) error("Unable to write", dataOut); fclose(outfile); utilFree((void **) &image); }
int read_pgm_image(unsigned char **pimage, FILE *fpIn, char *fnIn, int *pnx, int *pny) { int sizeInput; /* Read image size off of pgm header, if possible */ if (stripPgmHeader(fpIn, pnx, pny) < 0) return(-1); sizeInput = (*pnx) * (*pny) * sizeof( char ); grabByteMemory((char **)pimage, sizeInput, fnIn); if (!fread((char *) (*pimage), sizeInput, 1, fpIn)) { utilFree((void **) pimage); error("unexpected end of file on", fnIn); } return(0); }
void read_int_image_strip_bytes(unsigned int * image, FILE * fpIn, char * fnIn, int nx, int ny, int bytes) { char *header; int sizeInput; if (bytes > 0) { grabByteMemory((char **) &header, bytes, "strip-header"); sizeInput = bytes * sizeof( char ); if ( !fread((char *) header, sizeInput, 1, fpIn)) error("unexpected end of file on", fnIn); utilFree((void **) &header); } sizeInput = nx * ny * sizeof( unsigned int ); if ( !fread((char *) image, sizeInput, 1, fpIn)) { fprintf(stderr,"error: unexpected end of file on %s\n", fnIn); exit(1); } }
/* MEX interface function */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { FreemanPyramidCntrlStruct *cntrl = NULL; FreemanPyramidStruct *fp = NULL; FreemanCacheCntrlStruct *fpcc = NULL; float *image, *tmp; float epsAmp = 0.0; float epsC = 0.0; int nxImage, nyImage; int iChannel, iScale, iOrient; int iCell, iOut, iBasis; int k, n; mwSize dims[3]; mxArray *mxTmp; /* check for the required input arguments */ if( nrhs < 2 || nrhs > 3 ) mexErrMsgTxt( "Input expected: CNTRL, IMAGE, CACHECNTRL" ); /* decode the matlab version of the pyramid control structure and allocate the pyramid structure */ mexFreemanPyramidCntrlStruct( prhs[0], &cntrl, &epsAmp, &epsC ); fp = newFreemanPyramidStruct( cntrl ); if( nrhs == 3 ) mexFreemanCacheCntrlStruct( prhs[2], &fpcc ); /* get the image to be filtered */ mex2float( prhs[1], &image, &nxImage, &nyImage ); /* build the pyramid */ if( fpcc == NULL ) { computeFreemanPyramid_Float(fp, image, nxImage, nyImage, epsAmp, epsC); } else { recoverFreemanPyramid_Float(fp, image, nxImage, nyImage, epsAmp, epsC, fpcc ); } /* return the results to matlab in the order they appear in cntrl->storeMap */ iOut = 0; /* return the gradient maps if necessary */ if( cntrl->storeMap[isGrad] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; dims[2] = 2; plhs[iOut] = mxCreateCellArray( 3, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = 2*(cntrl->nOrient*iScale + iOrient); dims[0] = iScale; dims[1] = iOrient; float2mex( fp->gradMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 0; iCell = mxCalcSingleSubscript( plhs[iOut], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); float2mex( fp->gradMap[iChannel+1], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 1; iCell = mxCalcSingleSubscript( plhs[0], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the phase maps if necessary */ if( cntrl->storeMap[isPhase] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; plhs[iOut] = mxCreateCellArray( 2, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = cntrl->nOrient*iScale + iOrient; dims[0] = iScale; dims[1] = iOrient; float2mex( fp->phaseMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 2, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the amplitude maps if necessary */ if( cntrl->storeMap[isAmp] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; plhs[iOut] = mxCreateCellArray( 2, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = cntrl->nOrient*iScale + iOrient; dims[0] = iScale; dims[1] = iOrient; float2mex( fp->ampMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 2, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the filter maps if necessary */ if( cntrl->storeMap[isFilter] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; dims[2] = 2; plhs[iOut] = mxCreateCellArray( 3, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = 2*(cntrl->nOrient*iScale + iOrient); dims[0] = iScale; dims[1] = iOrient; float2mex( fp->filterMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 0; iCell = mxCalcSingleSubscript( plhs[iOut], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); float2mex( fp->filterMap[iChannel+1], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 1; iCell = mxCalcSingleSubscript( plhs[0], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the basis maps if necessary */ if( cntrl->storeMap[isBasis] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = NUMBASIS; plhs[iOut] = mxCreateCellArray( 2, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iBasis = 0; iBasis < NUMBASIS; iBasis++ ) { iChannel = NUMBASIS*iScale + iBasis; dims[0] = iScale; dims[1] = iBasis; float2mex( fp->basisMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 2, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the pyramid maps if necessary */ if( cntrl->storeMap[isPyr] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nPyr; plhs[iOut] = mxCreateCellArray( 1, dims ); for( iScale = 0; iScale < cntrl->nPyr; iScale++ ) { dims[0] = iScale; float2mex( fp->pyrMap[iScale], &mxTmp, fp->nxPyr[iScale], fp->nyPyr[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 1, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } iOut++; } /* free the pyramid, etc. */ freeFreemanPyramid( fp ); utilFree( (void **)&fp ); utilFree( (void **)&cntrl ); utilFree( (void **)&fpcc ); return; }
/*============================================================*/ void mexFunction_float( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { float *image, *tmp; int nX, nY; ccLabelStruct **label; int ixy; int numComp, t; float minTarget; /* 0.001 = boxes02-add.pgm */ float *target; float debug = 0; RanTimeSeed; /*mexPrintf("nlhs:%d nrhs: %d\n",nlhs,nrhs);*/ /* check for the required input arguments */ if( nrhs < 2 ) mexErrMsgTxt( "Input expected: <symmetric-positive-array-for-cca> <affty-threshold> <1-for-debug>" ); /* get the symmetric matrix */ mex2float( prhs[0], &target, &nX, &nY); /*mexPrintf("size: %d %d\n",nX,nY);*/ /* get the threshold */ minTarget = mxGetScalar(prhs[1]); if (nrhs == 3) { debug = mxGetScalar(prhs[2]); } if (debug) mexPrintf("Affty threshold: %2.3e\n",minTarget); /* set up empty labels */ grabByteMemory((char **) &label,sizeof(ccLabelStruct *) * nX,"label"); for (ixy=0;ixy < nX; ixy++) label[ixy] = NULL; if (debug) { mexPrintf("Computing connected components...."); } connectSymmetric(label, target, minTarget, nX, nY); if (debug) { mexPrintf("done\n"); } if (debug) { mexPrintf("Pruning small or weak connected components...."); } pruneLabelComp(label, nX, 1, 1, 0); if (debug) { mexPrintf("done\n"); } /* component count */ numComp = countLabelComp(label, nX, 1); if (debug) { mexPrintf("Found %d components.\n\n", numComp); } /* if (numComp > 0) printMarkovLabels(label,nX); */ /* return the labels to matlab */ if (nlhs > 0) labels2mex(label, &plhs[0], nX ); if (nlhs > 1) { double *pout; plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); pout = mxGetPr(plhs[1]); *pout = (double) numComp; } /* free up memory */ utilFree( (void **)&target ); utilFree((void **)label); /* for (ixy = 0; ixy < nX; ixy++) free(label[ixy]); */ }
/*============================================================*/ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { float *image, *tmp; int nX, nY; ccLabelStruct **label; int ixy; int numComp, t; double minTarget; /* 0.001 = boxes02-add.pgm */ double *target; float debug = 0; /* for sparse array */ mwIndex *rowPtr, *colInfo; double *data; int rows, cols; int sparseFlag = 0; RanTimeSeed; /*mexPrintf("nlhs:%d nrhs: %d\n",nlhs,nrhs);*/ /* check for the required input arguments */ if( nrhs < 2 ) mexErrMsgTxt( "Input expected: <symmetric-positive-array-for-cca> <affty-threshold> <1-for-debug>" ); /* get the symmetric matrix */ /*mex2float( prhs[0], &target, &nX, &nY);*/ nX = mxGetN(prhs[0]); /* cols */ cols = nX; nY = mxGetM(prhs[0]); /* rows */ rows = nY; mexPrintf("size: %d %d\n",nX,nY); if (mxIsSparse(prhs[0])) { sparseFlag = 1; mexPrintf("Affy matrix Sparse? : %d\n",sparseFlag); } if (sparseFlag) { mwIndex *jc; data = mxGetPr(prhs[0]); /* pointer to real data */ rowPtr = mxGetIr(prhs[0]); /* pointer to row info */ colInfo = mxGetJc(prhs[0]); /* pointer to col info */ jc = mxGetJc(prhs[0]); /* for (ixy=0; ixy < mxGetN(prhs[0]); ixy++) { mexPrintf("rowBouMain %d (%d %d) \n",ixy,jc[ixy+1],colInfo[ixy+1]); } analyze_sparse(prhs[0]); */ } else { target = (double *)mxGetPr(prhs[0]); } /* get the threshold */ minTarget = (double) mxGetScalar(prhs[1]); /* check for debug flag */ if (nrhs == 3) { debug = mxGetScalar(prhs[2]); } if (debug) mexPrintf("Affty threshold: %2.3e\n",minTarget); /* set up empty labels */ grabByteMemory((char **) &label,sizeof(ccLabelStruct *) * nX,"label"); for (ixy=0;ixy < nX; ixy++) label[ixy] = NULL; if (debug) { mexPrintf("Computing connected components...."); } if (sparseFlag) { connectSymmetricSparse(label,rowPtr,colInfo,data, minTarget, nX, nY); } else { connectSymmetricDbl(label, target, minTarget, nX, nY); } if (debug) { mexPrintf("done\n"); } /* yalla edition */ /* if (debug) { mexPrintf("Pruning small or weak connected components...."); } pruneLabelComp(label, nX, 1, 1, 0); */ if (debug) { mexPrintf("done\n"); } /* component count */ numComp = countLabelComp(label, nX, 1); if (debug) { mexPrintf("Found %d components.\n\n", numComp); } /* if (numComp > 0) printMarkovLabels(label,nX); */ /* return the labels to matlab */ if (nlhs > 0) labels2mex(label, &plhs[0], nX ); if (nlhs > 1) { double *pout; plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); pout = mxGetPr(plhs[1]); *pout = (double) numComp; } /* free up memory. */ /* target is a pointer to prhs[0]. so no need to free. */ /*utilFree( (void **)&target );*/ utilFree((void **)label); /* for (ixy = 0; ixy < nX; ixy++) free(label[ixy]); */ }