int createTemplateDevice(int devID,char * devName,unsigned int width,unsigned int height,unsigned int framerate) { if ( ( device[devID].templateWIDTH < width ) && ( device[devID].templateHEIGHT < height ) ) { device[devID].templateHEIGHT=height; device[devID].templateWIDTH=width; } if (devName==0) { strcpy(device[devID].readFromDir,""); } else { if (strlen(devName)==0) { strcpy(device[devID].readFromDir,""); } else { strncpy(device[devID].readFromDir,devName,MAX_DIR_PATH); } } unsigned int widthInternal; unsigned int heightInternal; char file_name_test[1024]; sprintf(file_name_test,"frames/%s/colorFrame_%u_%05u.pnm",device[devID].readFromDir,devID,0); char * tmpColor = ReadPPM(file_name_test,&widthInternal,&heightInternal); if ( (widthInternal!=width) || (heightInternal!=height) ) { fprintf(stderr,"Please note that the templateColor.pnm file has %ux%u resolution and the createTemplateDevice asked for %ux%u \n",widthInternal,heightInternal,width,height); } if (tmpColor!=0) { device[devID].templateColorFrame=tmpColor; } else { // if templateColorFrame is zero the next function behaves like a malloc device[devID].templateColorFrame= (char*) realloc(device[devID].templateColorFrame,device[devID].templateWIDTH*device[devID].templateHEIGHT*3*sizeof(char)); } sprintf(file_name_test,"frames/%s/depthFrame_%u_%05u.pnm",device[devID].readFromDir,devID,0); short * tmpDepth = ReadPPMD(file_name_test,&widthInternal,&heightInternal); if ( (widthInternal!=width) || (heightInternal!=height) ) { fprintf(stderr,"Please note that the templateColor.pnm file has %ux%u resolution and the createTemplateDevice asked for %ux%u \n",widthInternal,heightInternal,width,height); } if (tmpDepth!=0) { device[devID].templateDepthFrame=tmpDepth; } else { // if templateDepthFrame is zero the next function behaves like a malloc device[devID].templateDepthFrame= (short*) realloc(device[devID].templateDepthFrame,device[devID].templateWIDTH*device[devID].templateHEIGHT*1*sizeof(short)); } NullCalibration(device[devID].templateWIDTH,device[devID].templateHEIGHT,&device[devID].calibRGB); sprintf(file_name_test,"frames/%s/color.calib",device[devID].readFromDir); if ( ! ReadCalibration(file_name_test,&device[devID].calibRGB) ) { fprintf(stderr,"Could not read color calibration\n"); } NullCalibration(device[devID].templateWIDTH,device[devID].templateHEIGHT,&device[devID].calibDepth); sprintf(file_name_test,"frames/%s/depth.calib",device[devID].readFromDir); if ( ! ReadCalibration(file_name_test,&device[devID].calibDepth) ) { fprintf(stderr,"Could not read depth calibration\n"); } return ((device[devID].templateColorFrame!=0)&&(device[devID].templateDepthFrame!=0)); }
int segmentRGBAndDepthFrame ( unsigned char * RGB , unsigned short * Depth , unsigned int width , unsigned int height , struct SegmentationFeaturesRGB * segConfRGB , struct SegmentationFeaturesDepth * segConfDepth, struct calibration * calib , int combinationMode ) { struct calibration lastMomentEmptyCalibration={0}; if (RGB==0) { fprintf(stderr,"segmentRGBAndDepthFrame called with a null RGB frame \n"); return 0; } if (Depth==0) { fprintf(stderr,"segmentRGBAndDepthFrame called with a null Depth frame \n"); return 0; } if (calib==0) { fprintf(stderr,"segmentRGBAndDepthFrame called with a null Calibration , generating an ephimeral one now \n"); NullCalibration( width, height, &lastMomentEmptyCalibration ); calib=&lastMomentEmptyCalibration; } if (segConfRGB==0) { fprintf(stderr,"segmentRGBAndDepthFrame called with a null RGB Segmentation configuration \n"); } else if (!segConfRGB->isInitialized) { fprintf(stderr,"segmentRGBAndDepthFrame called with a non initialized RGB Segmentation configuration\n"); } if (segConfDepth==0) { fprintf(stderr,"segmentRGBAndDepthFrame called with a null Depth Segmentation configuration \n"); } else if (!segConfDepth->isInitialized) { fprintf(stderr,"segmentRGBAndDepthFrame called with a non initialized Depth Segmentation configuration\n"); } if (segConfDepth->autoPlaneSegmentation) { automaticPlaneSegmentation(Depth,width,height, segConfDepth->autoPlaneSegmentationMinimumDistancePoint , segConfDepth->autoPlaneSegmentationMaximumDistancePoint , segConfDepth->planeNormalOffset, segConfDepth->planeNormalSize, segConfDepth, calib); } //We have some criteria for segmentation at segConfRGB , and segConfDepth //First of all we want to use them and the original frames (RGB,Depth) , and select ( on an abstract array ) //the areas we want and the areas we don't want .. //We select the area for segmentation from RGB frame unsigned int selectedRGBCount=0; unsigned char * selectedRGB = selectSegmentationForRGBFrame(RGB , width , height , segConfRGB , calib , &selectedRGBCount); //We select the area for segmentation from Depth frame unsigned int selectedDepthCount=0; unsigned char * selectedDepth = selectSegmentationForDepthFrame(Depth , width , height , segConfDepth , calib , &selectedDepthCount); if ( (selectedDepthCount==width*height) && (selectedRGBCount==width*height) ) { fprintf(stderr,"Both RGB and Depth are fully selected , fastpath doing nothing\n"); } else { //We may chose to combine , or make a different selection for the RGB and Depth Frame if ( combinationMode == DONT_COMBINE ) { //If we dont want to combine them we just execute the selection and RGB will //now have the segmented output frame executeSegmentationRGB(RGB,selectedRGB,width,height,segConfRGB,selectedRGBCount,combinationMode); //The same goes for Depth executeSegmentationDepth(Depth,selectedDepth,width,height,selectedDepthCount,combinationMode); } else if (combinationMode == COMBINE_RGBFULL_DEPTH_USE_RGB ) { //RGB frame is unaffected , Depth Frame uses RGB Segmentation executeSegmentationDepth(Depth,selectedRGB,width,height,selectedDepthCount,combinationMode); } else if (combinationMode == COMBINE_DEPTHFULL_RGB_USE_DEPTH ) { //Depth frame is unaffected , RGB Frame uses DepthSegmentation executeSegmentationRGB(RGB,selectedDepth,width,height,segConfRGB,selectedRGBCount,combinationMode); } else if (combinationMode == COMBINE_SWAP ) { //If we want to swap RGB and Depth we just swap it executeSegmentationRGB(RGB,selectedDepth,width,height,segConfRGB,selectedRGBCount,combinationMode); //The same goes for Depth executeSegmentationDepth(Depth,selectedRGB,width,height,selectedDepthCount,combinationMode); } else { //If we do want to combine the selections "Together" , and there are many ways to do that using //the combinationMode switch ( see enum combinationModesEnumerator ) , we first make a new selection //and then execute the segmentation using it unsigned char * combinedSelection = combineRGBAndDepthToOutput(selectedRGB,selectedDepth,combinationMode,width,height); if (combinedSelection==0) { fprintf(stderr,"Failed to combine outputs using method %u\nCannot execute segmentation\n",combinationMode); } else { //We use the combinedSelection for both RGB and Depth executeSegmentationRGB(RGB,combinedSelection,width,height,segConfRGB,selectedRGBCount,combinationMode); executeSegmentationDepth(Depth,combinedSelection,width,height,selectedDepthCount,combinationMode); //And we dont forget to free our memory if (combinedSelection!=0) { free(combinedSelection); combinedSelection=0; } } } } // We do COMBINE LOGIC //Free memory from selections.. if (selectedRGB!=0) { free(selectedRGB); selectedRGB=0; } if (selectedDepth!=0) { free(selectedDepth); selectedDepth=0; } return 1; }