2017-04-05 120 views
0

我有一個圖像,我提取最recoverednt Web顏色,我把它們放在一張桌子。 而我有一個數據庫與圖像(藝術品)包含相同的結構。 我想知道是否有一個快速的方法(就性能而言,因爲我有很多圖像在數據庫中),以瞭解顏色發生的最接近的圖像。 我使用C#和MongoDB(用於數據)MongoDB查詢和嵌入式文檔中發生次數的順序

例如: 所測試的圖像:

{ 
    Artist : ArtistTest 
    Artworks : 
    [ 
     { 
      Title : test1, 
      MostColors : [ 
          { 
           Color : Blue, 
           Occurence : 10 
          }, 
          { 
           Color : Green, 
           Occurence : 5 
          }, 
          { 
           Color : Red, 
           Occurence : 2 
          } 
      ] 
     } 
    ] 
} 

圖像在DB:

{ 
    Artist : ArtistTrain1 
    Artworks : 
    [ 
     { 
      Title : train11, 
      MostColors : [ 
          { 
           Color : Black, 
           Occurence : 20 
          }, 
          { 
           Color : Yellow, 
           Occurence : 3 
          }, 
          { 
           Color : Green, 
           Occurence : 1 
          } 
      ] 
     }, 
     { 
      Title : train12, 
      MostColors : [ 
          { 
           Color : Red, 
           Occurence : 30 
          }, 
          { 
           Color : Green, 
           Occurence : 10 
          }, 
          { 
           Color : Purple, 
           Occurence : 5 
          } 
      ] 
     } 
    ] 
}, 
{ 
    Artist : ArtistTrain2 
    Artworks : 
    [ 
     { 
      Title : train21, 
      MostColors : [ 
          { 
           Color : Green, 
           Occurence : 15 
          }, 
          { 
           Color : Red, 
           Occurence : 5 
          }, 
          { 
           Color : Blue, 
           Occurence : 1 
          } 
      ] 
     }, 
     { 
      Title : train22, 
      MostColors : [ 
          { 
           Color : Blue, 
           Occurence : 30 
          }, 
          { 
           Color : Green, 
           Occurence : 1 
          }, 
          { 
           Color : Red, 
           Occurence : 1 
          } 
      ] 
     }, 
     { 
      Title : train23, 
      MostColors : [ 
          { 
           Color : Red, 
           Occurence : 30 
          }, 
          { 
           Color : Blue, 
           Occurence : 10 
          }, 
          { 
           Color : Green, 
           Occurence : 5 
          } 
      ] 
     } 
    ] 
} 

在理論上的結果,以便爲(第一最接近):

ArtistTrain2.train22 
ArtistTrain2.train23 
ArtistTrain2.train21 
ArtistTrain1.train12 
ArtistTrain1.train11 

您怎麼看? (我不確定訂單)

謝謝你。

回答

0

我認爲我找到了解決問題的方法。我計算每個作品發生的加權平均值(重量由測試圖像中最常用的顏色(例如:3:藍色; 2:綠色; 1:紅色)確定

通過mongo shell: :

db.ArtCollection.find({$or : [ 
     {"Artworks.MostColors.Color" : {$elemMatch : {$eq:"Blue"}}}, 
     {"Artworks.MostColors.Color" : {$elemMatch : {$eq:"Green"}}}, 
     {"Artworks.MostColors.Color" : {$elemMatch : {$eq:"Red"}}} 
    ]}).forEach(function(doc) 
    { doc.Artworks.forEach(function(art) 
     { 
     print (art.Title); 
     var sum = 0; 
     for(var ii =0; ii<art.MostColors.length;ii++) { 
      var coeff = 0; 
      if (art.MostColors[ii].Color == 'Blue') 
      { coeff = 3;} 
      else if (art.MostColors[ii].Color == 'Green') 
      { coeff = 2;} 
      else if (art.MostColors[ii].Color == 'Red')  
      { coeff = 1;} 
      sum+=art.MostColors[ii].Occurence*coeff;  
     } 
     print(' -- '+sum); 
     }) 
    }) 

我(使用LINQ)

List<string> stirngcolor = new List<string>() { "Blue", "Green", "Red" };// 
Dictionary<string, double> dicoCoeff = new Dictionary<string, double>(); 
int coeffValue = 0; 
// Determine the weight of color (by occurrence decending) 
for (int i = stirngcolor.Count; i > 0; i--) 
{ 
    dicoCoeff.Add(stirngcolor[i - 1], coeffValue++); 
} 

var collection = _database.GetCollection<Artist>(collectionDb); 
var request = from x in collection.AsQueryable() 
      where x.Artworks.Any(child => 
       child.MostColors.Any(c => stirngcolor.Contains(c.color)) 
      ) 
      select x; 

List<Artist> artistList = request.ToList(); 
#region art 
foreach (Artist artistValue in listPaletet) 
{ 
    foreach (ArtWork art in artistValue.Artworks) 
    { 
     double sum = 0; 
     for (int ii = 0; ii < art.MostColors.Count; ii++) 
     { 
      double coeff = 0; 
      #region coeff 
      if (dicoCoeff.ContainsKey(art.MostColors[ii].color)) 
      { 
       coeff = dicoCoeff[art.MostColors[ii].color]; 
      } 
      #endregion 
      sum += art.MostColors[ii].occurrence * coeff; 
     } 
     art.colorScore = sum; 
     artList.Add(art); 
    } 
} 
#endregion 
翻譯此聖多美和普林西比在C#