浏览代码

Increasing color variety in instance segmentation. (#261)

* Increasing color variety in instance segmentation.

* Fixing duplicated color issues in InstanceIdToColorMapping introduced after expanding range.

* Using other constants in constant for cleanliness
/main
GitHub 4 年前
当前提交
7eba5c39
共有 3 个文件被更改,包括 50 次插入47 次删除
  1. 1
      com.unity.perception/CHANGELOG.md
  2. 45
      com.unity.perception/Runtime/GroundTruth/InstanceIdToColorMapping.cs
  3. 51
      com.unity.perception/Tests/Runtime/GroundTruthTests/InstanceIdToColorMappingTests.cs

1
com.unity.perception/CHANGELOG.md


### Added
### Changed
Increased color variety in instance segmentation images
### Deprecated

45
com.unity.perception/Runtime/GroundTruth/InstanceIdToColorMapping.cs


using System;
using System.Collections.Generic;
using Unity.Profiling;
namespace UnityEngine.Perception.GroundTruth
{

/// </summary>
public const uint maxId = uint.MaxValue - ((256 * 256 * 256) * 2) + k_HslCount;
static Dictionary<uint, uint> s_IdToColorCache;
static uint[] s_IdToColorCache;
const uint k_HslCount = 64;
const uint k_HslCount = 1024;
const int k_HuesInEachValue = 30;
const int k_HuesInEachValue = 64;
const uint k_Values = k_HslCount / k_HuesInEachValue;
static void InitializeMaps()
private static ProfilerMarker k_InitializeMapsMarker = new ProfilerMarker(nameof(InitializeMaps));
internal static void InitializeMaps()
using (k_InitializeMapsMarker.Auto())
{
s_IdToColorCache = new uint[k_HslCount + 1];
s_ColorToIdCache = new Dictionary<uint, uint>();
s_IdToColorCache = new Dictionary<uint, uint>();
s_ColorToIdCache = new Dictionary<uint, uint>();
s_IdToColorCache[0] = k_InvalidPackedColor;
s_ColorToIdCache[k_InvalidPackedColor] = 0;
s_IdToColorCache[0] = k_InvalidPackedColor;
s_IdToColorCache[k_InvalidPackedColor] = 0;
for (uint i = 1; i <= k_HslCount; i++)
{
var color = GenerateHSLValueForId(i);
s_IdToColorCache[i] = color;
s_ColorToIdCache[color] = i;
for (uint i = 1; i <= k_HslCount; i++)
{
var color = GenerateHSLValueForId(i);
s_IdToColorCache[i] = color;
s_ColorToIdCache.Add(color, i);
}
}
}

var ratio = count * k_GoldenRatio;
// assign hue based on golden ratio
var hueId = count % k_HuesInEachValue;
var ratio = hueId * k_GoldenRatio;
count /= k_HuesInEachValue;
ratio = count * k_GoldenRatio;
var value = 1 - (ratio - Mathf.Floor(ratio));
var valueId = count / k_HuesInEachValue;
// avoid value 0
var value = 1 - (float)valueId / (k_Values + 1);
var color = (Color32)Color.HSVToRGB(hue, 1f, value);
color.a = 255;

51
com.unity.perception/Tests/Runtime/GroundTruthTests/InstanceIdToColorMappingTests.cs


public class InstanceIdToColorMappingTests
{
[Test]
public void InitializeMaps_DoesNotThrow()
{
Assert.DoesNotThrow(InstanceIdToColorMapping.InitializeMaps);
}
[Test]
for (var i = 1u; i <= 64u; i++)
for (var i = 1u; i <= 1024u; i++)
Assert.IsTrue(InstanceIdToColorMapping.TryGetColorFromInstanceId(i, out var color));
Assert.IsTrue(InstanceIdToColorMapping.TryGetInstanceIdFromColor(color, out var id));
Assert.IsTrue(InstanceIdToColorMapping.TryGetColorFromInstanceId(i, out var color), $"Failed TryGetColorFromInstanceId on id {i}");
Assert.IsTrue(InstanceIdToColorMapping.TryGetInstanceIdFromColor(color, out var id), $"Failed TryGetInstanceIdFromColor on id {i}");
Assert.AreEqual(i, id);
color = InstanceIdToColorMapping.GetColorFromInstanceId(i);

[TestCase(4u,255,0,223,255)]
[TestCase(5u,0,255,212,255)]
[TestCase(6u,255,138,0,255)]
[TestCase(64u,195,0,75,255)]
[TestCase(65u,0,0,1,254)]
[TestCase(66u,0,0,2,254)]
[TestCase(64u + 256u,0,1,0,254)]
[TestCase(65u + 256u,0,1,1,254)]
[TestCase(64u + 65536u,1,0,0,254)]
[TestCase(16777216u,255,255,192,254)]
[TestCase(64u + 16777216u,0,0,0,253)]
[TestCase(64u + (16777216u * 2),0,0,0,252)]
[TestCase(1024u,30, 0, 11,255)]
[TestCase(1025u,0,0,1,254)]
[TestCase(1026u,0,0,2,254)]
[TestCase(1024u + 256u,0,1,0,254)]
[TestCase(1025u + 256u,0,1,1,254)]
[TestCase(1024u + 65536u,1,0,0,254)]
[TestCase(1024u + 16777216u,0,0,0,253)]
[TestCase(1024u + (16777216u * 2),0,0,0,252)]
Assert.AreEqual(color.r, r);
Assert.AreEqual(color.g, g);
Assert.AreEqual(color.b, b);
Assert.AreEqual(color.a, a);
var expected = new Color32(r, g, b, a);
Assert.AreEqual(expected, color);
Assert.AreEqual(color.r, r);
Assert.AreEqual(color.g, g);
Assert.AreEqual(color.b, b);
Assert.AreEqual(color.a, a);
Assert.AreEqual(expected, color);
id2 = InstanceIdToColorMapping.GetInstanceIdFromColor(color);
Assert.AreEqual(id, id2);

public void InstanceIdToColorMappingTests_GetCorrectValuesFor255()
{
var expectedColor = new Color(0, 0, 191, 254);
var expectedColor = new Color32(19, 210, 0, 255);
Assert.AreEqual(color.r, expectedColor.r);
Assert.AreEqual(color.g, expectedColor.g);
Assert.AreEqual(color.b, expectedColor.b);
Assert.AreEqual(color.a, expectedColor.a);
Assert.AreEqual(expectedColor, color);
Assert.AreEqual(color.r, expectedColor.r);
Assert.AreEqual(color.g, expectedColor.g);
Assert.AreEqual(color.b, expectedColor.b);
Assert.AreEqual(color.a, expectedColor.a);
Assert.AreEqual(expectedColor, color);
id2 = InstanceIdToColorMapping.GetInstanceIdFromColor(color);
Assert.AreEqual(255u, id2);
}

正在加载...
取消
保存