Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions Leiter.Tests/Algorithms/Saliency/SaliencyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using Xunit;
using Leiter.Core;
using Leiter.Pixels;
Expand All @@ -16,7 +15,7 @@ namespace Leiter.Tests.Algorithms.Saliency;
public class SaliencyTests
{
/// <summary>
/// Verifies that the global contrast saliency should compute correctly behaves correctly.
/// Verifies that the global contrast saliency should compute correctly.
/// </summary>
[Fact]
public void GlobalContrastSaliency_ShouldComputeCorrectly()
Expand Down Expand Up @@ -54,7 +53,33 @@ public void GlobalContrastSaliency_ShouldComputeCorrectly()
}

/// <summary>
/// Verifies that the regional contrast saliency should compute correctly behaves correctly.
/// Verifies that global contrast saliency of a contrasting object is correctly computed.
/// </summary>
[Fact]
public void GlobalContrastSaliency_ContrastingObject_ShouldBeMostSalient()
{
// 10x10 image with a 4x4 red square in the center (16 pixels, 16%) and black background
var img = new SequentialMatrix<Rgb8>(10, 10);
img.SetAll(new Rgb8(0, 0, 0));
for (int y = 3; y <= 6; y++)
{
for (int x = 3; x <= 6; x++)
{
img[x, y] = new Rgb8(255, 0, 0);
}
}

var saliency = GlobalContrastSaliency.ComputeSaliency(img, enableColorSpaceSmoothing: false);

// The contrasting red region should have highest saliency (1.0)
Assert.Equal(1.0, saliency[5, 5].Value, 4);

// The black background should have the lowest saliency (0.0)
Assert.Equal(0.0, saliency[0, 0].Value, 4);
}

/// <summary>
/// Verifies that the regional contrast saliency should compute correctly.
/// </summary>
[Fact]
public void RegionalContrastSaliency_ShouldComputeCorrectly()
Expand Down Expand Up @@ -87,12 +112,42 @@ public void RegionalContrastSaliency_ShouldComputeCorrectly()
Assert.Equal(0.0, minS, 4);
Assert.Equal(1.0, maxS, 4);

// Verify regional saliency is not all flat zeros/ones and varies by region
Assert.True(saliency[0, 0].Value < saliency[10, 10].Value);
// Verify regional saliency contains various levels (i.e. not all flat zeros/ones)
Assert.True(saliency.Any(p => p.Value > 0.0 && p.Value < 1.0));

// Turn off smoothing and border calculation to test the alternate branches
var saliencyRaw = RegionalContrastSaliency.ComputeSaliency(img, segmentation, enableSmoothing: false, computeBorderRegions: false);
Assert.Equal(20, saliencyRaw.Width);
Assert.True(saliencyRaw.Min(p => p.Value) >= 0.0);
}

/// <summary>
/// Verifies that regional contrast saliency of a contrasting object is correctly computed.
/// </summary>
[Fact]
public void RegionalContrastSaliency_ContrastingObject_ShouldBeMostSalient()
{
// 12x12 image with a 4x4 red square in the center (16 pixels) and black background
var img = new SequentialMatrix<Rgb8>(12, 12);
img.SetAll(new Rgb8(0, 0, 0));
for (int y = 4; y <= 7; y++)
{
for (int x = 4; x <= 7; x++)
{
img[x, y] = new Rgb8(255, 0, 0);
}
}

// Segment the image. With exact colors, the segment boundaries are completely clear.
var segmentation = EgbiSegmentation.Segment(img, kFactor: 1.0, epsilon: 1.0, minSegmentSize: 4);

// Compute regional saliency
var saliency = RegionalContrastSaliency.ComputeSaliency(img, segmentation, enableSmoothing: false, computeBorderRegions: false);

// The contrasting center red region should have highest saliency (1.0)
Assert.Equal(1.0, saliency[5, 5].Value, 4);

// The background black region should have the lowest saliency (0.0)
Assert.Equal(0.0, saliency[0, 0].Value, 4);
}
}
Loading