unsigned char * selectSegmentationForRGBFrame(unsigned char * source , unsigned int inputFrameWidth , unsigned int inputFrameHeight , struct SegmentationFeaturesRGB * segConf, struct calibration * calib,unsigned int * selectedPixels) { unsigned int width = inputFrameWidth; unsigned int height = inputFrameHeight; //This will be our response segmentation unsigned char * selectedRGB = (unsigned char*) malloc(width*height*sizeof(unsigned char)); if (selectedRGB==0) { fprintf(stderr,"Could not allocate memory for RGB Selection\n"); return 0; } //if we don't need to segment , conserve our CPU if (justSelectAllRGBPixels(segConf,width,height)) { fprintf(stderr,"======== Just Selecting All RGB Frame ======== \n"); *selectedPixels=width*height; memset(selectedRGB,1,width*height*sizeof(unsigned char)); return selectedRGB; } //We initially disqualify ( unselect ) the whole image , so we only PICK things that are ok *selectedPixels=0; memset(selectedRGB,0,width*height*sizeof(unsigned char)); //In case our bounds are impossible we get an unselected image..! if ( segConf->maxX > width ) { segConf->maxX = width; } if ( segConf->maxY > height ) { segConf->maxY = height; } if ( segConf->minX > segConf->maxX ) { return selectedRGB; } if ( segConf->minY > segConf->maxY ) { return selectedRGB; } unsigned char * sourceCopy = (unsigned char *) malloc( width * height * 3 * sizeof( unsigned char)); if ( sourceCopy == 0) { fprintf(stderr,"Could not allocate a buffer to copy the source..\n"); return 0; } memcpy(sourceCopy,source,width*height*3*sizeof(char)); removeFloodFillBeforeProcessing(sourceCopy,width,height,segConf); unsigned int posX = segConf->minX , posY = segConf->minY; unsigned int limX = segConf->maxX , limY = segConf->maxY; unsigned int patchWidth = limX-posX-1 , patchHeight = limY-posY-1; unsigned int sourceWidthStep = width * 3; unsigned char * sourcePixelsStart = (unsigned char*) sourceCopy + ( (posX*3) + posY * sourceWidthStep ); unsigned char * sourcePixelsLineEnd = sourcePixelsStart + ((patchWidth)*3); unsigned char * sourcePixelsEnd = sourcePixelsLineEnd + ((patchHeight-1) * sourceWidthStep ); unsigned char * sourcePixels = sourcePixelsStart; unsigned char * selectedPtrStart = selectedRGB + ( posX + (posY * width) ) ; unsigned char * selectedPtr = selectedPtrStart; register unsigned char selected; register unsigned char * R , * G , * B; while (sourcePixels<sourcePixelsEnd) { while (sourcePixels<sourcePixelsLineEnd) { R = sourcePixels++; G = sourcePixels++; B = sourcePixels++; selected=( ( (*R!=segConf->replaceR) || (*G!=segConf->replaceG) || (*B!=segConf->replaceB) ) && ( (segConf->minR <= *R) && (*R <= segConf->maxR) && (segConf->minG <= *G) && (*G <= segConf->maxG) && (segConf->minB <= *B) && (*B <= segConf->maxB) ) ); *selectedPixels+=selected; *selectedPtr=selected; ++selectedPtr; } sourcePixelsStart+=sourceWidthStep; sourcePixels=sourcePixelsStart; sourcePixelsLineEnd+=sourceWidthStep; selectedPtrStart+=width; selectedPtr=selectedPtrStart; } if (segConf->enableRGBMotionDetection) { //In case we want motion detection we should record the first frame we have so that we can use it to select pixels if (! keepFirstRGBFrame(sourceCopy , width , height , segConf) ) { selectBasedOnRGBMovement(selectedRGB, segConf->firstRGBFrame , sourceCopy , segConf->motionRThreshold , segConf->motionGThreshold , segConf->motionBThreshold , width , height ); } } else { if (segConf->firstRGBFrame!=0) { fprintf(stderr,"Freeing first frame for rgb motion detection\n"); free(segConf->firstRGBFrame); segConf->firstRGBFrame=0; } } if (segConf->invert) { invertSelection(selectedRGB , inputFrameWidth , inputFrameHeight ,selectedPixels); } free(sourceCopy); return selectedRGB; }
unsigned char * selectSegmentationForRGBFrame(unsigned char * source , unsigned int width , unsigned int height , struct SegmentationFeaturesRGB * segConf, struct calibration * calib) { unsigned char * sourceCopy = (unsigned char *) malloc( width * height * 3 * sizeof( unsigned char)); if ( sourceCopy == 0) { return 0; } memcpy(sourceCopy,source,width*height*3*sizeof(char)); char * target = (char *) malloc( width * height * 3 * sizeof(char)); if ( target == 0) { free(sourceCopy); return 0; } memset(target,0,width*height*3*sizeof(char)); removeFloodFillBeforeProcessing(sourceCopy,target,width,height,segConf); unsigned int posX = 0; unsigned int posY = 0; unsigned int sourceWidthStep = width * 3; char * sourcePixelsStart = (char*) sourceCopy + ( (posX*3) + posY * sourceWidthStep ); char * sourcePixelsLineEnd = sourcePixelsStart + (width*3); char * sourcePixelsEnd = sourcePixelsLineEnd + ((height-1) * sourceWidthStep ); char * sourcePixels = sourcePixelsStart; unsigned char * selectedRGB = (unsigned char*) malloc(width*height*sizeof(unsigned char)); if (selectedRGB==0) { fprintf(stderr,"Could not allocate memory for RGB Selection\n"); return 0; } memset(selectedRGB,0,width*height*sizeof(unsigned char)); unsigned char * selectedPtr = selectedRGB; unsigned int x=0 , y=0; unsigned char * R , * G , * B; while (sourcePixels<sourcePixelsEnd) { if ( (y<segConf->minY) || (y>segConf->maxY) ) { sourcePixels+=sourceWidthStep; selectedPtr+=width; } else { x=0; while (sourcePixels<sourcePixelsLineEnd) { R = sourcePixels++; G = sourcePixels++; B = sourcePixels++; if ( (segConf->minR <= *R) && (*R <= segConf->maxR) && (segConf->minG <= *G) && (*G <= segConf->maxG) && (segConf->minB <= *B) && (*B <= segConf->maxB) && (segConf->minX <= x) && ( x<= segConf->maxX) ) { *selectedPtr=1; } else { *selectedPtr=0; } ++selectedPtr; ++x; } } sourcePixelsLineEnd+=sourceWidthStep; ++y; } free(sourceCopy); return selectedRGB; }