/******************************************************************************************************************************** * Author: Umang Agrawal * * Code: reteieveImage.cpp * * Funtion Call: [ Index, Score, ImageWord ] = retrieveImage( QureyImage, IndexImage, OptionalArgument) * * Optional Argument: Name Value * * NumResults Any Value greater than 0 * * ROI [ x_coordinate y_coordinate width height ] * ********************************************************************************************************************************/ #include #include #include #include #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/nonfree/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/ml/ml.hpp" using namespace cv; using namespace std; extern "C" { #include "api_scilab.h" #include "Scierror.h" #include "BOOL.h" #include #include "sciprint.h" #include "../common.h" bool count_based(const vector& a, const vector& b) { return a[1] > b[1]; } int opencv_retrieveImages(char *fname, unsigned long fname_len) { SciErr sciErr; int *piAddr = NULL; int *piAddr2 = NULL; int *piAddr3 = NULL; int *piAddr4 = NULL; int *piChild = NULL; int *piGrandChild = NULL; int *piLen = NULL; char **pstData = NULL; int iRows, iCols; int no_item_list = 0; int no_item_list_2 = 0; int inp_params = 0; char **arg = NULL; char ***location_img = NULL; double **WordIndex = NULL; double **Location = NULL; double **Vocab_size = NULL; double **Count = NULL; double *wordFrequency = NULL; char *bagOfFeaturesLocation = NULL; int count_res = 0, count_roi = 0; double numRes = 0; double *roi_arg = NULL; double x, y, width, height; Mat image; Mat feature_des; Mat dictionary; Mat des_matched; Mat hist; vector keypoints; vector valid_key; vector< vector > clusterID; vector WordIndex_size; int indx; int search_ind; double *Index_Out = NULL; double *Score = NULL; double **ImageWords = NULL; CheckInputArgument(pvApiCtx, 2, 6); //Check on Number of Input Arguments CheckOutputArgument(pvApiCtx, 1, 3); //Check on Number of Output Arguments retrieveImage(image, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if(!isListType(pvApiCtx, piAddr2)) { Scierror(999, "Error: Invalid Second argument. List Expected.\n"); return 0; } sciErr = getListItemNumber(pvApiCtx, piAddr2, &no_item_list); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getMatrixOfStringInList(pvApiCtx, piAddr2, 1, &iRows, &iCols, NULL, NULL); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } piLen = (int*)malloc(sizeof(int) * iRows * iCols); sciErr = getMatrixOfStringInList(pvApiCtx, piAddr2, 1, &iRows, &iCols, piLen, NULL); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } pstData = (char**)malloc(sizeof(char*) * iRows * iCols); for(int iter = 0 ; iter < iRows * iCols ; iter++) { pstData[iter] = (char*)malloc(sizeof(char) * (piLen[iter] + 1));//+ 1 for null termination } sciErr = getMatrixOfStringInList(pvApiCtx, piAddr2, 1, &iRows, &iCols, piLen, pstData); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } if(!(strcmp(pstData[0],"invertedImageIndex")==0)) { Scierror(999, "Error: The input argument 2 is not of type invertedImageIndex.\n"); return 0; } sciErr = getListItemAddress(pvApiCtx, piAddr2, 2, &piChild); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getListItemNumber(pvApiCtx, piChild, &no_item_list_2); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } WordIndex = (double**)malloc(sizeof(double*)*no_item_list_2); Location = (double**)malloc(sizeof(double*)*no_item_list_2); Vocab_size = (double**)malloc(sizeof(double*)*no_item_list_2); Count = (double**)malloc(sizeof(double*)*no_item_list_2); for( int i=0; i> dictionary; fs.release(); SurfFeatureDetector detector(100, 4, 2, 1, 1); Ptr matcher = DescriptorMatcher::create("FlannBased"); Ptr extractor = new SurfDescriptorExtractor(100, 4, 2, 1, 1); BOWImgDescriptorExtractor bowDE(extractor, matcher); bowDE.setVocabulary(dictionary); detector.detect(image, keypoints); bowDE.compute(image, keypoints, hist, &clusterID, &des_matched); ImageWords = (double**)malloc(sizeof(double*)*4*1); ImageWords[0] = (double*)malloc(sizeof(double)*keypoints.size()*1); ImageWords[1] = (double*)malloc(sizeof(double)*keypoints.size()*2); ImageWords[2] = (double*)malloc(sizeof(double)*1); ImageWords[3] = (double*)malloc(sizeof(double)*1); ImageWords[2][0] = Vocab_size[0][0]; ImageWords[3][0] = keypoints.size(); vector > count_index(no_item_list_2 , vector(3)); for( int i=0; i