Esempio n. 1
0
        public List <ImageRecord> QueryImage(string queryImagePath, out string messageToLog,
                                             LocateSettings locateSetting = null)
        {
            List <ImageRecord> rtnImageList = new List <ImageRecord>();

            #region Diagnostic Region
            Stopwatch sw = new Stopwatch();
            Stopwatch sw1 = new Stopwatch();
            long      _loadingTime = 0, _queryingMAVTime = 0, _matchingTime = 0;
            #endregion Diagnostic Region

            #region Reading Cookbook
            sw1.Reset(); sw1.Start();
            //-----Reading Cookbook
            double[][] codeBook     = null;
            string     fullFileName = locateSetting.CodeBookFullPath;
            if (!File.Exists(fullFileName))
            {
                string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName);
                throw new InvalidOperationException(msg);
            }

            using (FileStream fs = new FileStream(fullFileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
                         = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                codeBook = (double[][])bf.Deserialize(fs);
                fs.Close();
            }
            #endregion

            #region Reading Image Data
            //----Read Image Data
            LoCaTeDataSet locateDS = null;
            fullFileName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "LoCATeImageRecords.bin");
            if (!File.Exists(fullFileName))
            {
                string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName);
                throw new InvalidOperationException(msg);
            }

            using (FileStream fs = new FileStream(fullFileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
                         = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                locateDS = (LoCaTeDataSet)bf.Deserialize(fs);
                fs.Close();
            }
            sw1.Stop();
            _loadingTime = sw1.ElapsedMilliseconds;
            #endregion

            #region Query Image Detection and Visual Words
            //---Reading Query Image
            sw1.Reset(); sw1.Start();
            LoCATe          descriptor       = new LoCATe();
            List <double[]> queryDescriptors = descriptor.extract(new Bitmap(queryImagePath), "SURF");
            double[]        queryVisualWord  = createVisualWord(queryDescriptors, codeBook);
            int             totalFileCount   = locateDS.AllImageRecordSet.Count;

            if (locateSetting.isLteSchemeNeedToAppy)
            {
                queryVisualWord = doMaths(queryVisualWord, locateDS.HistogramSumOfAllVisualWords, totalFileCount);
            }
            sw1.Stop();
            _queryingMAVTime = sw1.ElapsedMilliseconds;
            #endregion

            List <LoCaTeBoWRecord> AllImageRecordSet = locateDS.AllImageRecordSet;
            double[][]             AllDatas          = AllImageRecordSet.Select(rec => rec.VisaulWord).ToArray();

            #region Searching of Image
            sw1.Reset(); sw1.Start();
            if (locateSetting.isLteSchemeNeedToAppy)
            {
                //-----------Creating ltc Data
                double[][] ltcData = new double[totalFileCount][];
                for (int i = 0; i < AllDatas.Length; i++)
                {
                    ltcData[i] = doMaths((double[])(AllDatas[i]).Clone(), locateDS.HistogramSumOfAllVisualWords, totalFileCount);
                }
                AllDatas = ltcData;
            }

            double EucDistance;
            for (int i = 0; i < totalFileCount; i++)
            {
                EucDistance = Accord.Math.Distance.Euclidean(queryVisualWord, AllDatas[i]);
                AllImageRecordSet[i].Distance = EucDistance;
            }

            List <LoCaTeRanker> listofImageWRanker = new List <LoCaTeRanker>();
            if (locateSetting.IsExtendedSearch)
            {
                //Take first 25 images
                IEnumerable <LoCaTeBoWRecord> first25Image = AllImageRecordSet.OrderBy(rec => rec.Distance)
                                                             .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance)
                                                             .Take(25);

                //Map to Locate Ranker list
                Mapper.CreateMap <LoCaTeBoWRecord, LoCaTeRanker>();
                Mapper.CreateMap <ImageRecord, LoCaTeRanker>();
                listofImageWRanker = Mapper.Map <IEnumerable <LoCaTeBoWRecord>, List <LoCaTeRanker> >(first25Image);

                //Calculate locate range
                int totalImageCount = listofImageWRanker.Count;
                for (int i = 0; i < totalImageCount; i++)
                {
                    double percentage = (1 - (listofImageWRanker[i].Distance / 1)) * 100;
                    listofImageWRanker[i].LocateRank = Convert.ToInt32(percentage);
                }

                //Perform SURF query
                listofImageWRanker = PerformExtendedSurfSearch(queryImagePath, listofImageWRanker);
                int totalimageBeforeCEDD = listofImageWRanker.Count;
                //listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.SurfRank).ToList();

                //Perform CEDD query
                CEDDQuery2 queryCEDD       = new CEDDQuery2();
                var        imageListbyCEDD = queryCEDD.QueryImage(queryImagePath).Take(25).ToList();
                totalImageCount = imageListbyCEDD.Count;
                for (int i = 0; i < totalImageCount; i++)
                {
                    long   id         = imageListbyCEDD[i].Id;
                    double percentage = (1 - (imageListbyCEDD[i].Distance / 35)) * 100;
                    var    img2       = listofImageWRanker.Where(img => img.Id == id).SingleOrDefault();
                    if (img2 == null)
                    {
                        var newImage = Mapper.Map <ImageRecord, LoCaTeRanker>(imageListbyCEDD[i]);
                        newImage.CEDDRank = Convert.ToInt32(percentage);
                        listofImageWRanker.Add(newImage);
                    }
                    else
                    {
                        img2.CEDDRank = Convert.ToInt32(percentage);
                    }
                }

                totalImageCount = listofImageWRanker.Count;
                for (int i = 0; i < totalImageCount; i++)
                {
                    var imageRank = listofImageWRanker[i];
                    if (imageRank.SurfRank > 0)
                    {
                        imageRank.CombinedRank = 100 - (totalimageBeforeCEDD - imageRank.SurfRank);
                    }
                    else
                    {
                        imageRank.CombinedRank = (0.5 * imageRank.CEDDRank) + (0.4 * imageRank.LocateRank);
                    }
                    imageRank.Distance = imageRank.CombinedRank;
                }



                listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.CombinedRank).ToList();
                rtnImageList       = listofImageWRanker.ToList <ImageRecord>();
            }
            else
            {
                rtnImageList = AllImageRecordSet.OrderBy(rec => rec.Distance)
                               .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance)
                               .Take(25)
                               .ToList <ImageRecord>();
            }


            sw1.Stop();
            _matchingTime = sw1.ElapsedMilliseconds;
            #endregion

            messageToLog = string.Format("Loading time {0} ms, Query image processing {1} ms, Matching {2} ms",
                                         _loadingTime, _queryingMAVTime, _matchingTime);
            return(rtnImageList);
        }
Esempio n. 2
0
 private void QueryImage(string queryImagePath)
 {
     try
     {
         ImageList.ItemsSource = null;
         if (!string.IsNullOrEmpty(queryImagePath))
         {
             object setting = null;
             QueryImageFullPath = queryImagePath;
             IImageQuery imageQuery = null;
             switch (SelectedAlgo)
             {
                 case emAlgo.Undetermined:
                     MessageBox.Show("Please select Algorithm for indexing");
                     return;
                 case emAlgo.pHash:
                     imageQuery = new pHashQuery();
                     QueryBinaryAlgo(imageQuery, queryImagePath, setting);
                     break;
                 case emAlgo.RBGHistogram:
                     imageQuery = new RGBProjectQuery();
                     QueryBinaryAlgo(imageQuery, queryImagePath, setting);
                     break;
                 case emAlgo.bhattacharyya:
                     imageQuery = new BhattacharyyaQuery();
                     QueryBinaryAlgo(imageQuery, queryImagePath, setting);
                     break;
                 case emAlgo.CEDD:
                     emCEDDAlgo selectedCeddAlgo = GetSelectedCEDDAlgo();
                     if (selectedCeddAlgo == emCEDDAlgo.BKTree)
                         imageQuery = new CEDDQuery2();
                     else
                         imageQuery = new CEDDQuery();
                     int goodMatchDistance = nudCEDDGoodMatchThreshold.Value.Value;
                     QueryBinaryAlgo(imageQuery, queryImagePath, goodMatchDistance);
                     break;
                 case emAlgo.SURF:
                 case emAlgo.AccordSurf:
                 case emAlgo.Locate:
                     QueryBgWorker.RunWorkerAsync();
                     break;
                 default:
                     MessageBox.Show("Algorithm not currently supported");
                     return;
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString(), "Error in Image Query", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Esempio n. 3
0
        public List<ImageRecord> QueryImage(string queryImagePath, out string messageToLog,
            LocateSettings locateSetting = null)
        {
            List<ImageRecord> rtnImageList = new List<ImageRecord>();

            #region Diagnostic Region
            Stopwatch sw = new Stopwatch();
            Stopwatch sw1 = new Stopwatch();
            long _loadingTime = 0, _queryingMAVTime = 0, _matchingTime = 0;
            #endregion Diagnostic Region

            #region Reading Cookbook
            sw1.Reset(); sw1.Start();
            //-----Reading Cookbook
            double[][] codeBook = null;
            string fullFileName = locateSetting.CodeBookFullPath;
            if (!File.Exists(fullFileName))
            {
                string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName);
                throw new InvalidOperationException(msg);
            }

            using (FileStream fs = new FileStream(fullFileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
                    = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                codeBook = (double[][])bf.Deserialize(fs);
                fs.Close();
            }
            #endregion

            #region Reading Image Data
            //----Read Image Data
            LoCaTeDataSet locateDS = null;
            fullFileName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "LoCATeImageRecords.bin");
            if (!File.Exists(fullFileName))
            {
                string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName);
                throw new InvalidOperationException(msg);
            }

            using (FileStream fs = new FileStream(fullFileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
                    = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                locateDS = (LoCaTeDataSet)bf.Deserialize(fs);
                fs.Close();
            }
            sw1.Stop();
            _loadingTime = sw1.ElapsedMilliseconds;
            #endregion

            #region Query Image Detection and Visual Words
            //---Reading Query Image
            sw1.Reset(); sw1.Start();
            LoCATe descriptor = new LoCATe();
            List<double[]> queryDescriptors = descriptor.extract(new Bitmap(queryImagePath), "SURF");
            double[] queryVisualWord = createVisualWord(queryDescriptors, codeBook);
            int totalFileCount = locateDS.AllImageRecordSet.Count;

            if (locateSetting.isLteSchemeNeedToAppy)
            {
                queryVisualWord = doMaths(queryVisualWord, locateDS.HistogramSumOfAllVisualWords, totalFileCount);
            }
            sw1.Stop();
            _queryingMAVTime = sw1.ElapsedMilliseconds;
            #endregion

            List<LoCaTeBoWRecord> AllImageRecordSet = locateDS.AllImageRecordSet;
            double[][] AllDatas = AllImageRecordSet.Select(rec => rec.VisaulWord).ToArray();

            #region Searching of Image
            sw1.Reset(); sw1.Start();
            if (locateSetting.isLteSchemeNeedToAppy)
            {
                //-----------Creating ltc Data
                double[][] ltcData = new double[totalFileCount][];
                for (int i = 0; i < AllDatas.Length; i++)
                {
                    ltcData[i] = doMaths((double[])(AllDatas[i]).Clone(), locateDS.HistogramSumOfAllVisualWords, totalFileCount);
                }
                AllDatas = ltcData;
            }

            double EucDistance;
            for (int i = 0; i < totalFileCount; i++)
            {
                EucDistance = Accord.Math.Distance.Euclidean(queryVisualWord, AllDatas[i]);
                AllImageRecordSet[i].Distance = EucDistance;
            }

            List<LoCaTeRanker> listofImageWRanker = new List<LoCaTeRanker>();
            if (locateSetting.IsExtendedSearch)
            {
                //Take first 25 images
                IEnumerable<LoCaTeBoWRecord> first25Image = AllImageRecordSet.OrderBy(rec => rec.Distance)
                                    .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance)
                                    .Take(25);

                //Map to Locate Ranker list
                Mapper.CreateMap<LoCaTeBoWRecord, LoCaTeRanker>();
                Mapper.CreateMap<ImageRecord, LoCaTeRanker>();
                listofImageWRanker = Mapper.Map<IEnumerable<LoCaTeBoWRecord>, List<LoCaTeRanker>>(first25Image);

                //Calculate locate range
                int totalImageCount = listofImageWRanker.Count;
                for (int i = 0; i < totalImageCount; i++)
                {
                    double percentage = (1 -  (listofImageWRanker[i].Distance / 1)) * 100;
                    listofImageWRanker[i].LocateRank = Convert.ToInt32(percentage);
                }

                //Perform SURF query
                listofImageWRanker = PerformExtendedSurfSearch(queryImagePath, listofImageWRanker);
                int totalimageBeforeCEDD = listofImageWRanker.Count;
                //listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.SurfRank).ToList();

                //Perform CEDD query
                CEDDQuery2 queryCEDD = new CEDDQuery2();
                var imageListbyCEDD = queryCEDD.QueryImage(queryImagePath).Take(25).ToList();
                totalImageCount = imageListbyCEDD.Count;
                for (int i = 0; i < totalImageCount; i++)
                {
                    long id = imageListbyCEDD[i].Id;
                    double percentage = (1 - (imageListbyCEDD[i].Distance / 35)) * 100;
                    var img2 = listofImageWRanker.Where(img => img.Id == id).SingleOrDefault();
                    if (img2 == null)
                    {
                        var newImage = Mapper.Map<ImageRecord, LoCaTeRanker>(imageListbyCEDD[i]);
                        newImage.CEDDRank = Convert.ToInt32(percentage);
                        listofImageWRanker.Add(newImage);
                    }
                    else
                    {
                        img2.CEDDRank = Convert.ToInt32(percentage);
                    }

                }

                totalImageCount = listofImageWRanker.Count;
                for (int i = 0; i < totalImageCount; i++ )
                {
                    var imageRank = listofImageWRanker[i];
                    if (imageRank.SurfRank > 0 )
                    {
                        imageRank.CombinedRank = 100 - (totalimageBeforeCEDD - imageRank.SurfRank);
                    }
                    else
                    {
                        imageRank.CombinedRank = (0.5 * imageRank.CEDDRank) + (0.4 * imageRank.LocateRank);
                    }
                    imageRank.Distance = imageRank.CombinedRank;
                }

                listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.CombinedRank).ToList();
                rtnImageList = listofImageWRanker.ToList<ImageRecord>();
            }
            else
            {
                rtnImageList = AllImageRecordSet.OrderBy(rec => rec.Distance)
                                    .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance)
                                    .Take(25)
                                    .ToList<ImageRecord>();
            }

            sw1.Stop();
            _matchingTime = sw1.ElapsedMilliseconds;
            #endregion

            messageToLog = string.Format("Loading time {0} ms, Query image processing {1} ms, Matching {2} ms",
                _loadingTime, _queryingMAVTime, _matchingTime);
            return rtnImageList;
        }