@@ -3,23 +3,26 @@
#include <cstring>
#include "main_aux.h"
extern "C" {
#include "spConfig.h"
#include "SPConfig.h"
#include "SPBPriorityQueue.h"
}

using namespace sp;
int scoreComp(const void* x, const void* y)
{
return (((siftScore*)y)->score-((siftScore*)x)->score);
}

SPConfig initConfig(char* cfgPath)
SPConfig initConfig(const char* cfgPath)
{
SP_CONFIG_MSG configMsg;
SPConfig cfg;
cfg = spConfigCreate(cfgPath, &configMsg);
if(configMsg == SP_CONFIG_CANNOT_OPEN_FILE)
{
printf(ERR_CFG_OPEN, (argc>1)?ERR_CFG_OPEN_D:ERR_CFG_OPEN_C, cfgFileName);
printf(ERR_CFG_OPEN, (!strcmp(cfgPath, DEF_CFG_FILE))
?ERR_CFG_OPEN_D
:ERR_CFG_OPEN_C, cfgPath);
}
return cfg;
}
@@ -28,10 +31,13 @@ SP_LOGGER_MSG initLog(SPConfig config)
int lvlInt;
char* filePath;
SP_CONFIG_MSG configMsg;
SP_LOGGER_LEVEL lvl = NULL;
SP_LOGGER_LEVEL lvl;
lvlInt = spConfigGetLogLevel(config, &configMsg);
filePath = spConfigGetLogFile(config, &configMsg);
switch lvlInt
if(!(strcmp(filePath, DEFAULT_LOG_FILE))){
filePath = NULL;
}
switch(lvlInt)
{
case 1:
lvl = SP_LOGGER_ERROR_LEVEL;
@@ -53,24 +59,20 @@ SP_LOGGER_MSG initLog(SPConfig config)

}

SPPoint** getFeatures(SPConfig config, int* totalLen)
SPPoint** getFeatures(SPConfig config, ImageProc* imPr, int* totalLen)
{
int iNum, *lengths, i, j, c;
int iNum, *lengths = NULL, i, j, c;
SP_CONFIG_MSG configMsg;
SPPoint*** dbFeats;
SPPoint** dbFeatsMerged;
SPPoint** dbFeatsMerged = NULL;
bool eMode;
iNum = spConfigGetNumOfImages(config, &configMsg);
/* TODO check message? */
emode = spConfigIsExtractionMode(config, &configMsg);
eMode = spConfigIsExtractionMode(config, &configMsg);
/* TODO check message? */
if(!(*lengths=(int*)malloc(iNum*sizeof(int))))
{
return NULL;
}
if(eMode)
{
if(!(dbFeats = extractDatabaseFeaturesI(config, lengths)))
if(!(dbFeats = extractDatabaseFeaturesI(config, imPr, lengths)))
{
/* TODO handle error */
}
@@ -100,29 +102,29 @@ SPPoint** getFeatures(SPConfig config, int* totalLen)
c++;
}
free(lengths);
return dbFeatsMerged
return dbFeatsMerged;
}
SPPoint*** extractDatabaseFeaturesI(SPConfig config, int dbFeatsLens[])
SPPoint*** extractDatabaseFeaturesI(SPConfig config, ImageProc* imPr, int dbFeatsLens[])
{
int i, iNum, j;
int i, iNum;
char imgPath[MAX_PATH_LENGTH+1];
SP_CONFIG_MSG configMsg;
SPPoint*** dbFeats;
inum = spConfigGetNumOfImages(config, &configMsg);
iNum = spConfigGetNumOfImages(config, &configMsg);
/* TODO check message? */
if!((dbFeats=(SPPoint***)malloc(iNum*sizeof(SPPoint**))))
if(!(dbFeats=(SPPoint***)malloc(iNum*sizeof(SPPoint**))))
{
return NULL;
}
for(i=0; i<iNum; i++)
{
memset(imgPath, NULL_BYTE, MAX_PATH_LENGTH+1);
memset(imgPath, NULL_CHAR, MAX_PATH_LENGTH+1);
configMsg = spConfigGetImagePath(imgPath, config, i);
/* TODO check message? */
if(!(dbFeats[i] = getImageFeatures(imgPath, i, dbFeatsLens+i)))
if(!(dbFeats[i] = imPr->getImageFeatures(imgPath, i, dbFeatsLens+i)))
{
destroyPointsArrayArray(dbFeats, i, dbFeatsLens);
return NULL
return NULL;
}
}
return dbFeats;
@@ -150,17 +152,20 @@ SPPoint** getImageFeaturesF(SPConfig config, int index, int* length)
{
SPPoint** ret;
int i, toRead[2], dim;
char filePath[MAX_PATH_LENGTH+1], *sufBackup;
char filePath[MAX_PATH_LENGTH+1], *sufNew, *sufBackup;
SP_CONFIG_MSG configMsg;
FILE* file;

sufNew = (char*)malloc(strlen(FEATS_FILE_SUFFIX)*sizeof(char)+1);
strcpy(sufNew, FEATS_FILE_SUFFIX);
sufBackup = spConfigGetImgSuffix(config, &configMsg);
/* TODO check message? */
spConfigSetImgSuffix(config, FEATS_FILE_SUFFIX, &configMsg)
spConfigSetImgSuffix(config, sufNew, &configMsg);
/* TODO check message? */
spConfigGetImagePath(filePath, config, index);
spConfigSetImgSuffix(config, sufBackup, &configMsg)
spConfigSetImgSuffix(config, sufBackup, &configMsg);
/* TODO check message? */
free(sufNew);

if(!(file=(fopen(filePath, READ_MODE)))){
return NULL;
@@ -188,50 +193,54 @@ SPPoint** getImageFeaturesF(SPConfig config, int index, int* length)
SPPoint* getImageFeatureF(FILE* file, int dim, int index)
{
SPPoint* ret;
int i;
double *tempArr;
if(!(tempArr = (double*)malloc(dim*sizeof(double))))
{
return NULL;
}
if(fread((void*)tempArr, sizeof(double),dim,file)<dim){
if(fread((void*)tempArr, sizeof(double),dim,file)<(size_t)dim){
free(tempArr);
return NULL;
}
ret = spPointCreate(tempArr, dim, index);
free(tempArr);
return ret;
}
bool storeDatabaseFeaturesF(SPConfig config, SPPoint* feats[][], int lengths[])
bool storeDatabaseFeaturesF(SPConfig config, SPPoint** feats[], int lengths[])
{
int iNum, i;
SP_CONFIG_MSG configMsg;
iNum = spConfigGetNumOfImages(config, &configMsg);
/* TODO check message? */
for(i=0;i<iNum;i++)
{
if(!storeImageFeaturesF(config, feats[i], length[i])){
if(!storeImageFeaturesF(config, feats[i], lengths[i])){
return false;
}
}
return true;
}
bool storeImageFeaturesF(SPConfig config, SPPoint* feats[], int length)
{
int i, index, toWrite[2];
char filePath[MAX_PATH_LENGTH+1], *sufBackup;
int i, index, toWrite[2], dim;
char filePath[MAX_PATH_LENGTH+1], *sufNew, *sufBackup;
SP_CONFIG_MSG configMsg;
FILE* file;

toWrite[0]=lengths; toWrite[1]=dim;
dim = spPointGetDimension(feats[0]);
toWrite[0]=length; toWrite[1]=dim;
index = spPointGetIndex(feats[0]);

sufNew = (char*)malloc(strlen(FEATS_FILE_SUFFIX)*sizeof(char)+1);
strcpy(sufNew, FEATS_FILE_SUFFIX);
sufBackup = spConfigGetImgSuffix(config, &configMsg);
/* TODO check message? */
spConfigSetImgSuffix(config, FEATS_FILE_SUFFIX, &configMsg)
spConfigSetImgSuffix(config, sufNew, &configMsg);
/* TODO check message? */
spConfigGetImagePath(filePath, config, index);
spConfigSetImgSuffix(config, sufBackup, &configMsg)
spConfigSetImgSuffix(config, sufBackup, &configMsg);
/* TODO check message? */
free(sufNew);

if(!(file=(fopen(filePath, WRITE_MODE)))){
return false;
@@ -262,7 +271,7 @@ bool storeImageFeatureF(FILE* file, SPPoint* feature)
{
tempArr[i] = spPointGetAxisCoor(feature, i);
}
if(fwrite((void*)tempArr, sizeof(double), dim, file)<dim){
if(fwrite((void*)tempArr, sizeof(double), dim, file)<(size_t)dim){
free(tempArr);
return false;
}
@@ -272,30 +281,30 @@ bool storeImageFeatureF(FILE* file, SPPoint* feature)

void getQueryPath(char* queryPath)
{
printf(INST_QUERY_OR_TERM);
printf(INST_QUERY);
/* Anything after strlen should already be 0: */
memset(queryPath, NULL_CHAR, strlen(queryPath));
READ_STR(queryPath);
}

void printNearestIndexes(SPConfig config, int* indexes)
void printNearestIndexes(SPConfig config, ImageProc* imPr, char* qPath, int* indexes)
{
SP_CONFIG_MSG configMsg;
char bestPath[MAX_PATH_LENGTH+1];
bool useMinGui = spConfigMinimalGui(config, &configMsg);
/* TODO check message? */
if(!useMinGui)
{
printf(OUT_TITLE);
printf(OUT_TITLE, qPath);
}
while(*indexes>=0)
{
memset(bestPath, NULL_BYTE, MAX_PATH_LENGTH+1);
memset(bestPath, NULL_CHAR, MAX_PATH_LENGTH+1);
configMsg = spConfigGetImagePath(bestPath, config, *indexes);
/* TODO check message? */
if(useMinGui)
{
showImage(bestPath);
imPr->showImage(bestPath);
}else{
printf("%s\n", bestPath);
}
@@ -314,7 +323,7 @@ void destroyPointsArray(SPPoint** pointArray, int arrayLength)
}

void destroyPointsArrayArray(SPPoint*** arraysArray, int arrayLength,
int isFeaturesArrays, int* varyingLengths)
int* varyingLengths)
{
int i;
for(i = 0; i<arrayLength; i++)
@@ -1,17 +1,16 @@
/* TODO add headers for extract mode stuff */


#include <cstring>
#include "SPImageProc.h"
extern "C" {
#include "SPPoint.h"
#include "SPConfig.h"
}
/* Error messages and instructions */
/* Messages */
#define ERR_CMD_ARGS "Invalid command line: use -c <config_filename>\n"
#define ERR_CFG_OPEN "The%sconfiguration file %s couldn't be open\n"
#define ERR_CFG_OPEN_D " default "
#define ERR_CFG_OPEN_C " "
#define INST_QUERY "Please enter an image path:\n"
#define MSG_EXIT "Exiting...\n"
#define OUT_TITLE "Best candidates for - %s - are:\n"
/* Defaults */
#define DEF_CFG_FILE "spcbir.config"
@@ -50,8 +49,8 @@ typedef struct sp_img_sift_score{
int scoreComp(const void* x, const void* y);

/**
* Given a path to a configuration file, this method will initialize a config struct from the data
* read from said file.
* Given a path to a configuration file and a boolean flag this method will initialize a config struct
* from the data read from said file.
* if the configuration file couldn't be opened, the function prints an error message. Additionally,
* error messages may be printed from inside the diffrent spConfig methods used to initialize the struct.
*
@@ -61,7 +60,7 @@ int scoreComp(const void* x, const void* y);
* An initialized SPConfig struct with all the system configuration variables set according to the data in the
* given file, or NULL if an error occured.
*/
SPConfig initConfig(char* cfgPath);
SPConfig initConfig(const char* cfgPath);

/**
* Given a valid configuration SPConfig struct, the method will attempt to initialize the logger using the
@@ -87,11 +86,11 @@ SP_LOGGER_MSG initLog(SPConfig config);
* NULL if an error occured or an array of SPPoint*s that represent the extracted features, with the point's indexes
* matching the image they have been extracted from.
*/
SPPoint** getFeatures(SPConfig config, int* totalLen);
SPPoint** getFeatures(SPConfig config, sp::ImageProc* imPr, int* totalLen);

/**
* Given a valid configuration SPConfig struct and an array of integers, the method will extract all features of
* all the images (ACTUAL IMAGES) in the images folder (num. of images, folder name and naming format is all
* Given a valid configuration SPConfig struct, an image processor object and an array of integers, the method will extract
* all features of all the images (ACTUAL IMAGES) in the images folder (num. of images, folder name and naming format is all
* given by the config file) and return an array of arrays containing them, such that each array in the returned array
* holds all the features of the image of the same index. The lengths of each of the "child arrays" are stored in the
* matching indexes in the array that is the 2nd argument
@@ -103,11 +102,11 @@ SPPoint** getFeatures(SPConfig config, int* totalLen);
* NULL if an error occured or an array of arrays of SPPoint*s that represent the extracted features, with the point's indexes
* matching the image they have been extracted from, and each array is stored in that same index in the big array.
*/
SPPoint*** extractDatabaseFeaturesI(SPConfig config, int dbFeatsLens[]);
SPPoint*** extractDatabaseFeaturesI(SPConfig config, sp::ImageProc* imPr, int dbFeatsLens[]);

/**
* Given a valid configuration SPConfig struct and an array of integers, the method will extract all features of
* all the images (FROM .feats FILES) in the images folder (num. of images, folder name and naming format is all
* Given a valid configuration SPConfig struct, an image processor object and an array of integers, the method will extract
* all features of all the images (FROM .feats FILES) in the images folder (num. of images, folder name and naming format is all
* given by the config file) and return an array of arrays containing them, such that each array in the returned array
* holds all the features of the image of the same index. The lengths of each of the "child arrays" are stored in the
* matching indexes in the array that is the 2nd argument
@@ -148,7 +147,7 @@ SPPoint** getImageFeaturesF(SPConfig config, int index, int* length);
* return
* NULL if an error occured, or a pointer to SPPoint representing the extracted feature.
*/
SPPoint* getImageFeatureF(FILE* file, int dim, int index)
SPPoint* getImageFeatureF(FILE* file, int dim, int index);

/**
* Given a valid configuration SPConfig struct, an array of arrays of features and an array of integers, the method will
@@ -164,7 +163,7 @@ SPPoint* getImageFeatureF(FILE* file, int dim, int index)
* return
* false if an error occured, true otherwise.
*/
bool storeDatabaseFeaturesF(SPConfig config, SPPoint* feats[][], int lengths[]);
bool storeDatabaseFeaturesF(SPConfig config, SPPoint** feats[], int lengths[]);

/**
* Given a valid configuration SPConfig struct, an arrays of features and an integer, the method will * store the all the featuresin one
@@ -202,14 +201,16 @@ bool storeImageFeatureF(FILE* file, SPPoint* feature);
void getQueryPath(char* queryPath);

/**
* Given a valid configuration SPConfig struct and an array of non-negative integers terminated with -1, the method
* will display the images who's indexes are in the integer array, or print their paths, in the order at which they are
* listed in the array. The output type depends on the information inside the SPConfig object.
* Given a valid configuration SPConfig struct, an image processor object, a string representing a path and an array
* of non-negative integers terminated with -1, the method will display the images who's indexes are in the integer array,
* or print their paths, in the order at which they arelisted in the array. The output type depends on the information
* inside the SPConfig object.
* The 2nd argument is used for the 1st line printed (if minimal GUI is turned off) which serves as a "title"
*
* @param config - a valid SPConfig object
* @param indexes[] - an array of integers, all of which's members are non-negative except for the last one, which is -1.
*/
void printNearestIndexes(SPConfig config, int* indexes);
void printNearestIndexes(SPConfig config, sp::ImageProc* imPr, char* qPath, int* indexes);

/**
* Destroys all the points pointed at by a pointer in the array of point pointers "pointArray",
@@ -238,6 +239,6 @@ void destroyPointsArray(SPPoint** pointArray, int arrayLength);
*
* @param arraysArray - the array of arrays of pointers to the points to be destroyed
* @param arraylength - the length of the array array
* @param arraysLengths - if isFeaturesArray == 1, arraysLengths should contain the lengths of the features arrays.
* @param arraysLengths - contains the respective lengths of the features arrays.
*/
void destroyPointsArrayArray(SPPoint*** arraysArray, int arrayLength, int* arraysLengths);
@@ -1,32 +1,51 @@
CC = gcc
CPP = g++
#put all your object files here
OBJS = main.o SPImageProc.o SPPoint.o

OBJS = SPPoint.o SPBPriorityQueue.o SPLogger.o SPConfig.o SPImageProc.o \
SPKDArray.o SPKDTree.o main_aux.o main.o \

#The executabel filename
EXEC = SPCBIR
INCLUDEPATH=/usr/local/lib/opencv-3.1.0/include/
LIBPATH=/usr/local/lib/opencv-3.1.0/lib/
LIBS=-lopencv_xfeatures2d -lopencv_features2d \
-lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core


CPP_COMP_FLAG = -std=c++11 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG
-Werror -pedantic-errors -DNDEBUG -g

C_COMP_FLAG = -std=c99 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG
-Werror -pedantic-errors -DNDEBUG -g

$(EXEC): $(OBJS)
$(CPP) $(OBJS) -L$(LIBPATH) $(LIBS) -o $@
main.o: main.cpp #put dependencies here!
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c++ source file
#use g++ -MM SPImageProc.cpp to see dependencies
SPImageProc.o: SPImageProc.cpp SPImageProc.h SPConfig.h SPPoint.h SPLogger.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c source file
#use "gcc -MM SPPoint.c" to see the dependencies

SPPoint.o: SPPoint.c SPPoint.h
$(CC) $(C_COMP_FLAG) -c $*.c

SPBPriorityQueue.o: SPBPriorityQueue.c SPBPriorityQueue.h
$(CC) $(C_COMP_FLAG) -c $*.c

SPLogger.o: SPLogger.c SPLogger.h
$(CC) $(C_COMP_FLAG) -c $*.c

SPConfig.o: SPConfig.c SPConfig.h SPLogger.h
$(CC) $(C_COMP_FLAG) -c $*.c

SPImageProc.o: SPImageProc.cpp SPImageProc.h SPConfig.h SPPoint.h SPLogger.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp

SPKDArray.o: SPKDArray.c SPKDArray.h SPPoint.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.c

SPKDTree.o: SPKDTree.c SPKDTree.h SPKDArray.h SPPoint.h SPConfig.h SPLogger.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.c

main_aux.o: main_aux.cpp main_aux.h SPImageProc.h SPConfig.h SPLogger.h SPPoint.h SPBPriorityQueue.h SPKDTree.h SPKDArray.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp

main.o: main.cpp main_aux.h SPImageProc.h SPConfig.h SPLogger.h SPPoint.h SPBPriorityQueue.h SPKDTree.h SPKDArray.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp

clean:
rm -f $(OBJS) $(EXEC)