Skip to content

Commit 379ef24

Browse files
author
Kabuto_W
committed
closes #3; Fix wrong outputs coming from asynchronous operation.
1 parent f6901c7 commit 379ef24

File tree

5 files changed

+10
-8
lines changed

5 files changed

+10
-8
lines changed

SimilarImages/SimilarImages/Form1.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ private void bgw_Calculate_DoWork(object sender, DoWorkEventArgs e)
180180
Stopwatch watch = new Stopwatch();
181181
watch.Start();
182182

183+
tuples = null;
183184
tuples = ImageHash.GetSimilarity(folderPathes, out int count,
184185
precision, interpolationMode, hashEnum, threshold);
185186
lb_Count.Invoke((Action)(() => { lb_Count.Text = count.ToString(); }));

SimilarImages/SimilarImages/ImageHash.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static List<Tuple<string, string, double>> GetSimilarity(
3333
{
3434
throw new DirectoryNotFoundException("Directory not found.");
3535
}
36-
Debug.WriteLine($"Hash Algorithm: {hashEnum}\nPrecision: {precision}\n" +
37-
$"Interpolation Mode: {interpolationMode}\nThreshold: {threshold}%");
36+
Debug.WriteLine($"HashAlgorithm: {hashEnum} Precision: {precision}\n" +
37+
$"InterpolationMode: {interpolationMode} Threshold: {threshold}%");
3838

3939
watch.Restart();
4040

@@ -62,7 +62,7 @@ public static List<Tuple<string, string, double>> GetSimilarity(
6262
watch.Restart();
6363

6464
// Compare hashes
65-
var tuples = new List<Tuple<string, string, double>>();
65+
var tuples = new ConcurrentBag<Tuple<string, string, double>>();
6666
Parallel.For(0, imageHashPairs.Length, i =>
6767
{
6868
for (int j = imageHashPairs.Length - 1; j > i; j--)
@@ -81,9 +81,10 @@ public static List<Tuple<string, string, double>> GetSimilarity(
8181
watch.Stop();
8282
long compareTime = watch.ElapsedMilliseconds;
8383
Debug.WriteLine($"GetHash: {hashTime}ms; CompareHash: {compareTime}ms");
84+
Debug.WriteLine("TuplesCount:" + tuples.Count);
8485

8586
// Sort by similarity
86-
return tuples.OrderByDescending(u => u.Item3).ToList(); // TODO: seldom u=null.
87+
return tuples.OrderByDescending(u => u.Item3).ToList();
8788
}
8889

8990
public enum HashEnum
@@ -100,7 +101,7 @@ private static KeyValuePair<string, string>[] GetImageHashes(string folderPath,
100101
var imageNames = from file in di.GetFiles()
101102
where imageExtensions.Contains(file.Extension)
102103
select file.FullName;
103-
Debug.WriteLine($"Directory: {folderPath}\nImage count: {imageNames.Count()}");
104+
Debug.WriteLine($"Directory: {folderPath}; ImageCount: {imageNames.Count()}");
104105
if (imageNames.Count() < 2) { return null; }
105106

106107
// Get hash algorithm
@@ -137,7 +138,7 @@ where imageExtensions.Contains(file.Extension)
137138
});
138139

139140
watch.Stop();
140-
Debug.WriteLine($"Valid count: {imageHashPairs.Count} elapsed time: {watch.ElapsedMilliseconds}ms");
141+
Debug.WriteLine($"ValidCount: {imageHashPairs.Count}; ElapsedTime: {watch.ElapsedMilliseconds}ms");
141142
return imageHashPairs.ToArray();
142143
}
143144

SimilarImages/SimilarImages/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.3.0.0")]
36-
[assembly: AssemblyFileVersion("1.3.0.0")]
35+
[assembly: AssemblyVersion("1.3.1.0")]
36+
[assembly: AssemblyFileVersion("1.3.1.0")]

_Output/SimilarImages.exe

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)