float PerformanceUtils::NrAllDetectedPixNotNULL(IplImage *image, IplImage *ground_truth) { //Nombre de tous les pixels non nuls dans Groundthruth et dans image float Union12 = 0.0; unsigned char *pixelGT = (unsigned char*) malloc(1*sizeof(unsigned char)); unsigned char *pixelI = (unsigned char*) malloc(1*sizeof(unsigned char)); PixelUtils p; for(int y = 0; y < image->height; y++) { for(int x = 0; x < image->width; x++) { p.GetGrayPixel(ground_truth,x,y,pixelGT); p.GetGrayPixel(image,x,y,pixelI); if((pixelGT[0] != 0) || (pixelI[0] != 0)) Union12++; } } free(pixelGT); free(pixelI); return Union12; }
void FuzzyUtils::getFuzzyIntegralSugeno(IplImage* H, IplImage* Delta, int n, float *MeasureG, IplImage* OutputImage) { // MeasureG : est un vecteur contenant 3 mesure g (g1,g2,g3) tel que : g1+g2+g3=1 // n : =2 cad aggreger les 2 images "H" et "Delta" // =1 cad aggreger uniquement les valeurs des composantes couleurs de l'image "Delta" PixelUtils p; float* HTexturePixel = (float*) malloc(1*sizeof(float)); float* DeltaOhtaPixel = (float*) malloc(3*(sizeof(float))); int *Indice = (int*) malloc(3*(sizeof(int))); float *HI = (float*) malloc(3*(sizeof(float))); float *Integral = (float*) malloc(3*(sizeof(float))); float* X = (float*) malloc(1*sizeof(float)); float* XiXj = (float*) malloc(1*sizeof(float)); float IntegralFlou; *Indice = 0; *(Indice+1) = 1; *(Indice+2) = 2; *X = 1.0; for(int i = 0; i < H->width; i++) { for(int j = 0; j < H->height; j++) { p.GetGrayPixel(H,i,j,HTexturePixel); p.GetPixel(Delta,i,j,DeltaOhtaPixel); *(HI+0) = *(HTexturePixel+0); *(HI+1) = *(DeltaOhtaPixel+0); *(HI+2) = *(DeltaOhtaPixel+1); Trier(HI,3,Indice); *XiXj = *(MeasureG + (*(Indice+1))) + (*(MeasureG + (*(Indice+2)))); *(Integral+0) = min((HI + (*(Indice+0))), X); *(Integral+1) = min((HI + (*(Indice+1))), XiXj); *(Integral+2) = min((HI + (*(Indice+2))), ((MeasureG+(*(Indice+2))))); IntegralFlou = max(Integral,3); p.PutGrayPixel(OutputImage,i,j,IntegralFlou); } } free(HTexturePixel); free(DeltaOhtaPixel); free(Indice); free(HI); free(X); free(XiXj); free(Integral); }
void FuzzyUtils::SimilarityDegreesImage(IplImage* CurrentImage, IplImage* BGImage, IplImage* DeltaImage, int n, int color_space) { PixelUtils p; int i, j; if(n == 1) { float* CurrentGrayPixel = (float*) malloc (1*(sizeof(float))); float* BGGrayPixel = (float*) malloc (1*(sizeof(float))); float* DeltaGrayPixel = (float*) malloc (1*(sizeof(float))); for(i = 0; i < CurrentImage->width; i++) { for(j = 0; j < CurrentImage->height; j++) { p.GetGrayPixel(CurrentImage,i,j,CurrentGrayPixel); p.GetGrayPixel(BGImage,i,j,BGGrayPixel); RatioPixels(CurrentGrayPixel,BGGrayPixel,DeltaGrayPixel,1); p.PutGrayPixel(DeltaImage,i,j,*DeltaGrayPixel); } } free(CurrentGrayPixel); free(BGGrayPixel); free(DeltaGrayPixel); } if(n != 1) { IplImage* ConvertedCurrentImage = cvCreateImage(cvSize(CurrentImage->width, CurrentImage->height), IPL_DEPTH_32F, 3); IplImage* ConvertedBGImage = cvCreateImage(cvSize(CurrentImage->width, CurrentImage->height), IPL_DEPTH_32F, 3); float* ConvertedCurrentPixel = (float*) malloc(3*(sizeof(float))); float* ConvertedBGPixel = (float*) malloc(3*(sizeof(float))); float* DeltaConvertedPixel = (float*) malloc(3*(sizeof(float))); p.ColorConversion(CurrentImage,ConvertedCurrentImage,color_space); p.ColorConversion(BGImage,ConvertedBGImage,color_space); for(i = 0; i < CurrentImage->width; i++) { for(j = 0; j < CurrentImage->height; j++) { p.GetPixel(ConvertedCurrentImage,i,j,ConvertedCurrentPixel); p.GetPixel(ConvertedBGImage,i,j,ConvertedBGPixel); RatioPixels(ConvertedCurrentPixel,ConvertedBGPixel,DeltaConvertedPixel,3); p.PutPixel(DeltaImage,i,j,DeltaConvertedPixel); } } free(ConvertedCurrentPixel); free(ConvertedBGPixel); free(DeltaConvertedPixel); cvReleaseImage(&ConvertedCurrentImage); cvReleaseImage(&ConvertedBGImage); } }
float PerformanceUtils::NrTrueNegatives(IplImage* image, IplImage* ground_truth, bool debug) { float nTN = 0.0; unsigned char *pixelGT = (unsigned char *)malloc(1*sizeof(unsigned char)); unsigned char *pixelI = (unsigned char *)malloc(1*sizeof(unsigned char)); IplImage *TNimage = 0; if(debug) { TNimage = cvCreateImage(cvSize(image->width,image->height),image->depth,image->nChannels); cvFillImage(TNimage, 0.0); } PixelUtils p; for(int y = 0; y < image->height; y++) { for(int x = 0; x < image->width; x++) { p.GetGrayPixel(ground_truth,x,y,pixelGT); p.GetGrayPixel(image,x,y,pixelI); if((pixelGT[0] == 0) && (pixelI[0] == 0.0)) { *pixelI = 255; if(debug) p.PutGrayPixel(TNimage,x,y,*pixelI); nTN++; } } } if(debug) { cvNamedWindow("TNImage", 0); cvShowImage("TNImage", TNimage); //std::cout << "True Negatives: " << nTN << std::endl; //<< " press ENTER to continue" << std::endl; //cvWaitKey(0); cvReleaseImage(&TNimage); } free(pixelGT); free(pixelI); return nTN; }
void FuzzyUtils::AdaptativeSelectiveBackgroundModelUpdate(IplImage* CurrentImage, IplImage* BGImage, IplImage* OutputImage, IplImage* Integral, float seuil, float alpha) { PixelUtils p; float beta = 0.0; float* CurentImagePixel = (float*) malloc(3*sizeof(float)); float* BGImagePixel = (float*) malloc(3*sizeof(float)); float* OutputImagePixel = (float*) malloc(3*sizeof(float)); float* IntegralImagePixel = (float*) malloc(1*sizeof(float)); float *Maximum = (float*) malloc(1*sizeof(float)); float *Minimum = (float*) malloc(1*sizeof(float)); p.ForegroundMaximum(Integral, Maximum, 1); p.ForegroundMinimum(Integral, Minimum, 1); for(int i = 0; i < CurrentImage->width; i++) { for(int j = 0; j < CurrentImage->height; j++) { p.GetPixel(CurrentImage, i, j, CurentImagePixel); p.GetPixel(BGImage, i, j, BGImagePixel); p.GetGrayPixel(Integral, i, j, IntegralImagePixel); beta = 1 - ((*IntegralImagePixel) - ((*Minimum / (*Minimum - *Maximum)) * (*IntegralImagePixel) - (*Minimum * (*Maximum) / (*Minimum - *Maximum)))); for(int k = 0; k < 3; k++) *(OutputImagePixel + k) = beta * (*(BGImagePixel + k)) + (1 - beta) * (alpha * (*(CurentImagePixel+k)) + (1-alpha) * (*(BGImagePixel+k))); p.PutPixel(OutputImage, i, j, OutputImagePixel); } } free(CurentImagePixel); free(BGImagePixel); free(OutputImagePixel); free(IntegralImagePixel); free(Maximum); free(Minimum); }
void FuzzyUtils::LBP(IplImage* InputImage, IplImage* LBPimage) { PixelUtils p; float* neighberPixel = (float*) malloc(9*sizeof(float)); float* BinaryValue = (float*) malloc(9*sizeof(float)); float* CarreExp = (float*) malloc(9*sizeof(float)); float* valLBP = (float*) malloc(1*sizeof(float)); *valLBP = 0; int x = 0, y = 0; // on implemente les 8 valeurs puissance de 2 qui correspondent aux 8 elem. d'image voisins au elem. d'image central *(CarreExp+0)=1.0; *(CarreExp+1)=2.0; *(CarreExp+2)=4.0; *(CarreExp+3)=8.0; *(CarreExp+4)=0.0; *(CarreExp+5)=16.0; *(CarreExp+6)=32.0; *(CarreExp+7)=64.0; *(CarreExp+8)=128.0; //le calcule de LBP //pour les 4 coins /* 1.*/ if(x==0 && y==0) { p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,4,0); *valLBP=*valLBP+((*(BinaryValue+1))*(*(CarreExp+1))+(*(BinaryValue+2))*(*(CarreExp+2))+(*(BinaryValue+3))*(*(CarreExp+3)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } /* 2.*/ if(x==0 && y==InputImage->width) { *valLBP=0; p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,4,1); *valLBP=*valLBP+((*(BinaryValue))*(*(CarreExp))+(*(BinaryValue+2))*(*(CarreExp+2))+(*(BinaryValue+3))*(*(CarreExp+3)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } /* 3.*/ if(x==InputImage->height && y==0) { *valLBP=0; p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,4,2); *valLBP=*valLBP+((*(BinaryValue))*(*(CarreExp))+(*(BinaryValue+1))*(*(CarreExp+1))+(*(BinaryValue+3))*(*(CarreExp+3)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } /* 4.*/ if(x==InputImage->height && y==InputImage->width) { *valLBP=0; p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,4,3); *valLBP=*valLBP+((*(BinaryValue))*(*(CarreExp))+(*(BinaryValue+1))*(*(CarreExp+1))+(*(BinaryValue+2))*(*(CarreExp+2)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } //le calcul de LBP pour la première ligne : L(0) if(x==0 && (y!=0 && y!=InputImage->width)) { for(int y = 1; y < InputImage->width-1; y++) { p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,6,4); *valLBP=0; *valLBP=*valLBP+((*(BinaryValue))*(*(CarreExp))+(*(BinaryValue+1))*(*(CarreExp+1))+(*(BinaryValue+2))*(*(CarreExp+2))+(*(BinaryValue+3))*(*(CarreExp+3))+(*(BinaryValue+5))*(*(CarreExp+5)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } } //le calcul de LBP pour la dernière colonne : C(w) if((x!=0 && x!=InputImage->height) && y==InputImage->width) { for(int x = 1; x < InputImage->height-1; x++) { p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,6,4); *valLBP=0; *valLBP=*valLBP+((*(BinaryValue))*(*(CarreExp))+(*(BinaryValue+1))*(*(CarreExp+1))+(*(BinaryValue+2))*(*(CarreExp+2))+(*(BinaryValue+3))*(*(CarreExp+3))+(*(BinaryValue+5))*(*(CarreExp+5)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } } //le calcul de LBP pour la dernière ligne : L(h) if(x==InputImage->height && (y!=0 && y!=InputImage->width)) { for(int y = 1; y < InputImage->width-1; y++) { p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,6,1); *valLBP=0; *valLBP=*valLBP+((*(BinaryValue))*(*(CarreExp))+(*(BinaryValue+2))*(*(CarreExp+2))+(*(BinaryValue+3))*(*(CarreExp+3))+(*(BinaryValue+4))*(*(CarreExp+4))+(*(BinaryValue+5))*(*(CarreExp+5)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } } //le calcul de LBP pour la première colonne : C(0) if((x!=0 && x!=InputImage->height) && y==0) { for(int x = 1; x <InputImage->height-1; x++) { p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,6,2); *valLBP=0; *valLBP=*valLBP+((*(BinaryValue))*(*(CarreExp+5))+(*(BinaryValue+1))*(*(CarreExp+6))+(*(BinaryValue+3))*(*(CarreExp+3))+(*(BinaryValue+4))*(*(CarreExp))+(*(BinaryValue+5))*(*(CarreExp+1)))/255.0; p.PutGrayPixel(LBPimage,x,y,*valLBP); } } //pour le reste des elements d'image for(int y = 1; y < InputImage->height-1; y++) { for(int x = 1; x < InputImage->width-1; x++) { p.getNeighberhoodGrayPixel(InputImage, x,y,neighberPixel); getBinValue(neighberPixel,BinaryValue,9,4); //le calcul de la valeur du LBP pour chaque elem. d'im. *valLBP=0; for(int l = 0; l < 9; l++) *valLBP = *valLBP + ((*(BinaryValue+l)) * (*(CarreExp+l))) / 255.0; //printf("\nvalLBP(%d,%d)=%f",x,y,*valLBP); p.PutGrayPixel(LBPimage,x,y,*valLBP); } } free(neighberPixel); free(BinaryValue); free(CarreExp); free(valLBP); }
void PerformanceUtils::ImageROC(IplImage *image, IplImage* ground_truth, bool saveResults, char* filename) { unsigned char *pixelGT = (unsigned char*) malloc(1*sizeof(unsigned char)); unsigned char *pixelI = (unsigned char*) malloc(1*sizeof(unsigned char)); IplImage *ROCimage = cvCreateImage(cvSize(image->width,image->height),image->depth,image->nChannels); cvFillImage(ROCimage, 0.0); PixelUtils p; for(int y = 0; y < image->height; y++) { for(int x = 0; x < image->width; x++) { p.GetGrayPixel(ground_truth,x,y,pixelGT); p.GetGrayPixel(image,x,y,pixelI); if((pixelGT[0] != 0) && (pixelI[0] != 0)) // TP { *pixelI = 30; p.PutGrayPixel(ROCimage,x,y,*pixelI); } if((pixelGT[0] == 0) && (pixelI[0] == 0.0)) // TN { *pixelI = 0; p.PutGrayPixel(ROCimage,x,y,*pixelI); } if((pixelGT[0] == 0) && (pixelI[0] != 0)) // FP { *pixelI = 255; p.PutGrayPixel(ROCimage,x,y,*pixelI); } if((pixelGT[0] != 0) && (pixelI[0] == 0)) // FN { *pixelI = 100; p.PutGrayPixel(ROCimage,x,y,*pixelI); } } } cvNamedWindow("ROC image", 0); cvShowImage("ROC image", ROCimage); if(saveResults) { unsigned char *pixelOI = (unsigned char*) malloc(1*sizeof(unsigned char)); unsigned char *pixelROC = (unsigned char*) malloc(1*sizeof(unsigned char)); float** freq; float nTP = 0.0; float nTN = 0.0; float nFP = 0.0; float nFN = 0.0; freq = (float**) malloc(256*(sizeof(float*))); for(int i = 0; i < 256; i++) freq[i] = (float*) malloc(7 * (sizeof(float))); for(int i = 0; i < 256; i++) for(int j = 0; j < 6; j++) freq[i][j] = 0.0; for(int y = 0; y < image->height; y++) { for(int x = 0; x < image->width; x++) { for(int i = 0; i < 256; i++) { p.GetGrayPixel(image,x,y,pixelOI); p.GetGrayPixel(ROCimage,x,y,pixelROC); if((pixelOI[0] == i) && (pixelROC[0] == 30.0)) // TP { nTP++; freq[i][0] = nTP; break; } if((pixelOI[0] == i) && (pixelROC[0] == 0.0)) // TN { nTN++; freq[i][1] = nTN; break; } if((pixelOI[0] == i) && (pixelROC[0] == 255.0)) // FP { nFP++; freq[i][2] = nFP; break; } if((pixelOI[0] == i) && (pixelROC[0] == 100)) // FN { nFN++; freq[i][3] = nFN; break; } } } } //freq[i][0] = TP //freq[i][1] = TN //freq[i][2] = FP //freq[i][3] = FN //freq[i][4] = FNR //freq[i][5] = FPR std::ofstream f(filename); if(!f.is_open()) std::cout << "Failed to open file " << filename << " for writing!" << std::endl; else { f << " I TP TN FP FN FPR FNR DR \n" << std::endl; for(int i = 0; i < 256; i++) { //printf("%4d - TP:%5.0f, TN:%5.0f, FP:%5.0f, FN:%5.0f,", i, freq[i][0], freq[i][1], freq[i][2], freq[i][3]); if((freq[i][3] + freq[i][0] != 0.0) && (freq[i][2] + freq[i][1] != 0.0)) { freq[i][4] = freq[i][3] / (freq[i][3] + freq[i][0]); // FNR = FN / (TP + FN); freq[i][5] = freq[i][2] / (freq[i][2] + freq[i][1]); // FPR = FP / (FP + TN); freq[i][6] = freq[i][0] / (freq[i][0] + freq[i][3]); // DR = TP / (TP+FN); //printf(" FPR:%1.5f, FNR:%1.5f, D:%1.5f\n", freq[i][5], freq[i][4], freq[i][6]); ////fprintf(f," %4d %1.6f %1.6f\n",i,freq[i][5],freq[i][4]); ////fprintf(f," %1.6f %1.6f\n",freq[i][5],freq[i][4]); char line[255]; sprintf(line,"%3d %6.0f %6.0f %6.0f %6.0f %1.6f %1.6f %1.6f\n", i, freq[i][0], freq[i][1], freq[i][2], freq[i][3], freq[i][5], freq[i][4], freq[i][6]); f << line; } //else //printf("\n"); } std::cout << "Results saved in " << filename << std::endl; f.close(); } free(freq); free(pixelOI); free(pixelROC); } //std::cout << "press ENTER to continue" << std::endl; //cvWaitKey(0); cvReleaseImage(&ROCimage); free(pixelGT); free(pixelI); }