コード例 #1
0
ファイル: main.cpp プロジェクト: tauprojects/final
int main(int argc, char* argv[]) {
	//Declaring and Initializing Variable and Objects
	SP_CONFIG_MSG msg; 				 //msg pointer
	SPConfig config;  				 //config struct instance
	char* imagePath = (char*)malloc(sizeof(char)*MAXLEN);
	SP_SPLIT_METHOD method;
	SPPoint* resPoints;				//temporary point array for each img
	SPPoint* totalResPoints;		//Point Array for all features
	KDArray kdarray;
	KDTreeNode root;

	int sizeOfTotalFeat;

	//Checking argument state
	if (argc == 1) {
		config = spConfigCreate(DEFAULT_FILE, &msg);
		if (msg == SP_CONFIG_CANNOT_OPEN_FILE) {
			printf(DEFAULT_ERROR_FILE_OPEN_MSG);
			return 1; //exit(1)
		} else if (msg != SP_CONFIG_SUCCESS)
			return 1; //exit(1)
	} else if (argc == 3 && strcmp(argv[1], "-c") == 0) {
		config = spConfigCreate(argv[2], &msg);
		if (msg == SP_CONFIG_CANNOT_OPEN_FILE) {
			printf(ERROR_FILE_OPEN_MSG, argv[2]);
			return 1; //exit(1)
		} else if (msg != SP_CONFIG_SUCCESS)
			return 1; //exit(1)
	} else {
		printf(INVALID_CMD_LINE_ERROR_MSG);
		return 1; //exit(1)
	}

	//Declaring and Initializing Variable from configuration
	int numOfImg = spConfigGetNumOfImages(config, &msg);
	int numSimilarImg = spConfigGetSimilarImages(config, &msg);
	int spKNN = spConfigGetKNN(config, &msg);
	int dim = spConfigGetPCADim(config, &msg);

	//Init Logger
	char* loggerFile = (char*)malloc(sizeof(char)*MAXLEN);
	SP_LOGGER_LEVEL loglevel = spConfigGetLoggerLevel(config);
	msg = spConfigGetLoggerFilename(loggerFile,config);
	if(msg==SP_CONFIG_INVALID_ARGUMENT){
		spLoggerPrintError(CONFIG_FAIL_MSG,__FILE__,__func__,__LINE__);
	}
	if (strcmp(loggerFile, "stdout") == 0){
		//Creating logger with  std-output printing
		spLoggerCreate(NULL,loglevel);
	}
	else
	spLoggerCreate(loggerFile,loglevel);

	//Init and allocate after logger created.
	int* numOfFeats = (int*) malloc(sizeof(int)); //Temporary int pointer for the creating of .feat files
	if(numOfFeats==NULL){
		spLoggerPrintError(FAIL_ALOC_MSG,__FILE__,__func__,__LINE__);
		return 1; //exit(1)
	}
	spLoggerPrintInfo(CONFIG_INIT_SUCCESS);

	//Creating new ImageProc instance by using configuration.
	ImageProc imageProc = ImageProc(config);
	spLoggerPrintInfo(IMGPROC_INIT_SUCCESS_MSG);

    /**
    * Checking Extraction Mode State:
    * if extractionMode=true-->creating .feat files
    * else -->use feat files DB.
    */
	if (spConfigIsExtractionMode(config, &msg)) {
		if(msg != SP_CONFIG_SUCCESS){
			spLoggerPrintError(INVALID_EXTRAC_ARG_MSG,__FILE__,__func__,__LINE__);
			return 1; //exit(1)
		}
		for (int i = 0; i < numOfImg; i++) {
			msg = spConfigGetImagePath(imagePath, config, i);  //generate img path
			if (msg != SP_CONFIG_SUCCESS){
				spLoggerPrintError(INVALID_EXTRAC_ARG_MSG,__FILE__,__func__,__LINE__);
				return 1; //exit(1)
			}
		   //Extracting Image features for each image into SPPoint Array.
			resPoints = imageProc.getImageFeatures(imagePath, i, numOfFeats); //get img feat for the feat file
			if(resPoints==NULL){
				spLoggerPrintError(FAIL_ALOC_MSG,__FILE__,__func__,__LINE__);
				return 1; //exit(1)
			}

			//Auxilary function - extracting features values to .feat files.
			msg = createFeatFiles(config, imagePath, i, numOfFeats, resPoints); //CHECKK HERE/ LIRON GET CALM
			if(msg!=SP_CONFIG_SUCCESS){
				spLoggerPrintError(FEAT_FIlLES_INIT_FAIL_MSG,__FILE__,__func__,__LINE__);
				return 1; //exit(1)
			}
			//Destroying the SPPoint Array for reusing.
			for (int j = 0; j < *numOfFeats; j++)
				spPointDestroy(resPoints[j]);
			free(resPoints);
		}
	}
//	spLoggerPrintInfo(EXIT_MSG);

	//	Creation of point array with features of all images.
	totalResPoints = createTotalFeatArray(config, numOfImg, dim,&sizeOfTotalFeat); //CHECKK HERE/ LIRON GET CALM
	if(totalResPoints==NULL){
		spLoggerPrintError(FEAT_ARRAY_INIT_FAIL_MSG,__FILE__,__func__,__LINE__);
		return 1; //exit(1)
	}
	//Creation of KDArray with all point features - with image attribute
	kdarray = KdArrayInit(totalResPoints, sizeOfTotalFeat);

	//Destroying the SPPoint Total features Array.
	for (int i = 0; i < KDArrayGetSize(kdarray); i++)
		spPointDestroy(totalResPoints[i]);
	free(totalResPoints);

	//Checking Split Method from configuration file
	spConfigGetKDSplitMethod(&method, config);

	//Initializing KDTree DB
	root = kdTreeInit(kdarray, method, -1);

	//Search By Query
	puts(ENTER_IMG_PATH);
	fflush(NULL);
	scanf("%s", imagePath);
	fflush(NULL);
	//Declaring countHits for count similar features per image.
	struct featHits* countHits = (featHits*) malloc(sizeof(featHits) * numOfImg);
	if(countHits==NULL){
		spLoggerPrintError(FAIL_ALOC_MSG,__FILE__,__func__,__LINE__);
		return 1; //exit(1)
	}

	//Request Query Image Path until 'exit' enter
	while (strcmp(imagePath, "<>") != 0) {
		spLoggerPrintInfo(LOG_CURRENT_IMAGE_MSG);
		spLoggerPrintInfo(imagePath);
		for (int i = 0; i < numOfImg; i++) {
			countHits[i].hits = 0;
			countHits[i].index = i;
		}
		//Extracting Image features for query image into SPPoint Array.
		resPoints = imageProc.getImageFeatures(imagePath, BAD_INDEX,numOfFeats);
		if(resPoints==NULL){
			spLoggerPrintError(FAIL_ALOC_MSG,__FILE__,__func__,__LINE__);
			return 1; //exit(1)
		}

		//Searching for nearest neighbors of each feature
		for (int i = 0; i < *numOfFeats; i++) {
			//Creating new BPQueue MaxSize spKNN
			SPBPQueue bpq = spBPQueueCreate(spKNN);
			if(bpq==NULL){
				spLoggerPrintError(BPQUEUE_FAILURE,__FILE__,__func__,__LINE__);
				return 1; //exit(1)
			}
			KD_TREE_MSG treeMsg = kNearestNeighbors(bpq, root, resPoints[i]);
			if(treeMsg!=KD_TREE_SUCCESS){
				spLoggerPrintError(KD_TREE_FAIL_MSG,__FILE__,__func__,__LINE__);
				return 1; //exit(1)
			}
			for (int j = 0; j < spKNN; j++) {
				SP_BPQUEUE_MSG bpqMsg;
				int index = spBPQueuePeekIndex(bpq);
				countHits[index].hits++;
				bpqMsg = spBPQueueDequeue(bpq);
				if(bpqMsg!=SP_BPQUEUE_SUCCESS){
					spLoggerPrintError(BPQUEUE_FAILURE,__FILE__,__func__,__LINE__);
					return 1; //exit(1)
				}
			}
			spBPQueueDestroy(bpq);
		}

		//Sorting and getting the K best hits
		qsort((void*) countHits, numOfImg, sizeof(featHits), hitsComp);

		//Checking Show Image State
		if (spConfigMinimalGui(config, &msg)) {
			for (int i = 0; i < numSimilarImg; i++) {
				msg = spConfigGetImagePath(imagePath, config,
						countHits[i].index);
				if(msg!=SP_CONFIG_SUCCESS){
					spLoggerPrintError(CONFIG_FAIL_MSG,__FILE__,__func__,__LINE__);
					return 1; //exit(1)
				}
				imageProc.showImage(imagePath);
			}
		} else {
			printf("Best candidates for - %s - are:\n", imagePath);
			for (int i = 0; i < numSimilarImg; i++){
				spConfigGetImagePath(imagePath,config,countHits[i].index);
				printf("%s\n",imagePath);
			}
		}

		spLoggerPrintInfo("Wating for another query image path");
		puts(ENTER_IMG_PATH);
		fflush(NULL);
		scanf("%s", imagePath);
	}
	spLoggerPrintInfo(EXIT_MSG);
	free(loggerFile);
	spLoggerDestroy();
	free(countHits);
	KDTreeDestroy(root);
	free(imagePath);
	free(numOfFeats);
	return 0;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: yuvalailer/softPro
int main(int argc,char* argv[]){

	SPConfig config;
	char configpath[1024] = ""; // asked moab can assume that not more than 1024 char
	if(argc > 1){ // if inserted command line arguments
		if ((argc == 3)&&(!(strcmp(argv[1],"-c")))){ // checks if a path was inserted in the correct format
			strcpy(configpath,argv[2]);
		}else{
			printf("Invalid command line : use -c <config_filename>\n");
			return 0;
		}
	}
	if (!strcmp(configpath,"")){ //if no path was inserted
		strcpy(configpath,"spcbir.config");
	}
	SP_CONFIG_MSG msg;
	config = spConfigCreate(configpath,&msg);
	if (config == NULL){ // if there was an error
		if (msg == SP_CONFIG_CANNOT_OPEN_FILE){
			if(!strcmp(configpath,"spcbir.config")){
				printf("The default configuration file spcbir.config couldn’t be open\n");
				return 0;
			}else{
				printf("The configuration file %s couldn’t be open\n",configpath);
				return 0;
			}
		}
	}
	// finished creating configuration file

	spLoggerCreate(spConfigGetLoggerFilename(config),spConfigGetLoggerLevel(config));
	//finished creating SPLogger

	ImageProc proc = ImageProc(config);
	spLoggerPrintInfo("Creating imageproc success");
	// finished creating image processing instance

	int i,j,k;
	int n=0;
	int numofimages = spConfigGetNumOfImages(config,&msg);
	int* tempdir = (int*)malloc(sizeof(int)*numofimages);
	if(tempdir == NULL){spLoggerPrintError("tempdir is null!",__FILE__,__func__,__LINE__);}
	else{spLoggerPrintDebug("tempdir allocation is success!!",__FILE__,__func__,__LINE__);}
	char temppath[1024];
	int tempnumOfFeatsextracted;
	SPPoint** directory = (SPPoint**)malloc(sizeof(SPPoint*)*numofimages); // allocating matrix of feats per image
	if(directory == NULL){spLoggerPrintError("directory is null!",__FILE__,__func__,__LINE__);return 0;}
	else{spLoggerPrintDebug("directory allocation is success!!",__FILE__,__func__,__LINE__);}
	SPPoint* finaldir;

	if (spConfigGetExtractionMode(config)){ // if we are in extraction mode
		FILE* fw;
		for (i = 0; i < numofimages; i++) { //for each image in image directory
			msg = spConfigGetImagePath (temppath,config,i);
			if(msg != SP_CONFIG_SUCCESS){spLoggerPrintError(spConfigMsgToString(msg),__FILE__,__func__,__LINE__);return 0;}
			else{spLoggerPrintDebug(spConfigMsgToString(msg),__FILE__,__func__,__LINE__);}
			directory[i] = proc.getImageFeatures(temppath,i,&tempnumOfFeatsextracted);
			if(directory[i]==NULL){spLoggerPrintError("error extracting image features",__FILE__,__func__,__LINE__);return 0;}
			else{spLoggerPrintDebug("extracted features from image successfully",__FILE__,__func__,__LINE__);}
			n+=tempnumOfFeatsextracted;
			tempdir[i] = tempnumOfFeatsextracted;
			msg = spConfigGetImagePathfeats(temppath,config,i); //get the file to write to
			if(msg != SP_CONFIG_SUCCESS){spLoggerPrintError(spConfigMsgToString(msg),__FILE__,__func__,__LINE__);return 0;}
			else{spLoggerPrintDebug("successfully extracted image path to write to",__FILE__,__func__,__LINE__);}
			fw = fopen(temppath,"w");//open file for writing
			writefeats(fw,directory[i],tempnumOfFeatsextracted);
		}
		fclose(fw);
		finaldir = (SPPoint*)malloc(sizeof(SPPoint)*n); //making final dir
		if (finaldir == NULL){spLoggerPrintError("error initializing finaldir in extraction mode",__FILE__,__func__,__LINE__);return 0;}
		else{spLoggerPrintDebug("initialized finaldir successfully",__FILE__,__func__,__LINE__);}
		k=0;
		for (i = 0; i < numofimages; ++i){
			for (j = 0; j < tempdir[i]; ++j) {
				finaldir[k] = spPointCopy(directory[i][j]);
				k++;
			}
		}
		DirectoryDestroy(directory,tempdir,numofimages);
	}
	else{ // non - extraction mode

		FILE* fr;
		for (i = 0; i < numofimages; i++){
			msg = spConfigGetImagePathfeats(temppath,config,i); //get path to feats file to read from
			if (msg !=SP_CONFIG_SUCCESS){spLoggerPrintError("error extracting feats path from config",__FILE__,__func__,__LINE__);return 0;}
			else{spLoggerPrintDebug("successfully extracted feats path from config",__FILE__,__func__,__LINE__);}
			fr = fopen(temppath,"r");
			if(fr ==NULL){spLoggerPrintError("error opening .feats file to extract",__FILE__,__func__,__LINE__);return 0;}
			else{spLoggerPrintDebug("opend .feats file successfully",__FILE__,__func__,__LINE__);}
			directory[i] = getfeats(fr,(tempdir+i));
			n+=tempdir[i];
		}
		fclose(fr);
		finaldir = (SPPoint*)malloc(sizeof(SPPoint)*n); //making final dir
		if (finaldir == NULL){spLoggerPrintError("error initializing finaldir in extraction mode",__FILE__,__func__,__LINE__);return 0;}
		else{spLoggerPrintDebug("initialized finaldir successfully",__FILE__,__func__,__LINE__);}
		k=0;
		for (i = 0; i < numofimages; ++i){
			for (j = 0; j < tempdir[i]; ++j) {
				finaldir[k] = spPointCopy(directory[i][j]);
				k++;
			}
		}
		DirectoryDestroy(directory,tempdir,numofimages);
	}

	//after extraction / non - extraction and we have our finaldir containing all the feats(SPPoints)!
	spLoggerPrintInfo("success after extraction / non - extraction phase");

	bool out = false;
	int numofsimilarimages = spConfigGetNumSimilarImages(config);
	KDTreeNode* head = InitTree(finaldir,n,config);
	if(head ==NULL){spLoggerPrintError("failure building tree",__FILE__,__func__,__LINE__);return 0;}
	else{spLoggerPrintDebug("tree build complete successfully",__FILE__,__func__,__LINE__);}
	/*initialization of KDTree & free of finaldir complexity: O(d X nlogn)*/
	while(!out){
		printf("Please enter an image path:\n");
		fflush(stdout);
		char quarypath[1024]; //= (char*)malloc(sizeof(char)*1024); //moab said can assume quary path is at most 1024
		scanf("%s",quarypath);
		if(!strcmp(quarypath,"<>")){ //if chose to exit the program
			printf("Exiting…\n");
			out = true;
			break;
		}

		int* hits = (int*)malloc(sizeof(int)*numofimages);
		for(i=0;i<numofimages;i++){//initialize to -1 hits per image
			hits[i] = -1;
		}
		//an array to keep track of how many times an image feature was selected to be k-nearest feature
		int* winners = (int*)malloc(sizeof(int)*numofsimilarimages);
		//an array contains indexes of winners ordered by numbered of hits example: winner[0] - the index of the most closest image
		SPPoint* quaryfeatures = proc.getImageFeatures(quarypath,numofimages,&tempnumOfFeatsextracted);
		SPBPQueue tempbpq;
		for (i = 0; i < tempnumOfFeatsextracted; ++i) { //count hits per image
			tempbpq = KDTreeSearch(head,quaryfeatures[i],numofsimilarimages);
			while(!spBPQueueIsEmpty(tempbpq)){
				hits[spListElementGetIndex(spBPQueuePeek(tempbpq))]++;
				spBPQueueDequeue(tempbpq);

			}
			spBPQueueDestroy(tempbpq);
		}
		calculatewinners(winners,hits,numofimages,numofsimilarimages);
		SPPointArrayDestroy(quaryfeatures,tempnumOfFeatsextracted);

		//showing results
		spLoggerPrintInfo("showing results");

		if(spConfigMinimalGui(config,&msg)){ // if we are in minimal-GUI mode
			for (i = 0; i <numofsimilarimages ; ++i) {
				spConfigGetImagePath(temppath,config,winners[i]);
				proc.showImage(temppath);
			}
			free(hits);
			free(winners);
		}
		else{ // not in minimal-GUI mode
			printf("Best candidates for - %s - are:\n",quarypath);
			for (i = 0; i < numofsimilarimages; ++i) {
				spConfigGetImagePath(temppath,config,winners[i]);
				printf("%s\n",temppath);
			}
			free(hits);
			free(winners);
		}
	}
	KDTreeDestroy(head);
	spConfigDestroy(config);
	free(tempdir);
	spLoggerDestroy();
	spLoggerPrintInfo("finished free of all memory successfully");
	return 1;
}