1
2

More than 3 years have passed since last update.

Matクラスからデータを取り出してダンプする

Posted at
private string Dump(Array data, List<int> indices, int dimension = 0)
{
    string result = "[ ";

    for (var i = 0; i < data.GetLength(dimension); i++)
    {
        if (dimension == data.Rank - 1)
        {
            var temp = data.GetValue(indices.Concat(new int[] { i }).ToArray()).ToString();

            temp = Regex.Replace(temp, @"(Vec[2346][bdfisw] )|([xyz]:)", "");
            temp = Regex.Replace(temp, @"\(", "[");
            temp = Regex.Replace(temp, @"\)", "]");

            result += temp;

            if (i != data.GetLength(dimension) - 1) result += ", ";
        }
        else
        {
            result += Dump(data, indices.Concat(new int[] { i }).ToList(), dimension + 1);                    

            if (i != data.GetLength(dimension) - 1) result += ", ";
        }
    }

    return result += " ]";
}

private void Test()
{
    byte[,] array = { { 255, 255, 255, 255, 255, 255, 255, 255 }, 
                      { 255, 255, 255, 255, 255, 255, 255, 255 }, 
                      { 255, 255,   0,   0,   0,   0, 255, 255 }, 
                      { 255, 255, 255, 255, 255, 255, 255, 255 }, 
                      { 255, 255, 255, 255, 255, 255, 255, 255 }, 
    };

    Mat mat = InputArray.Create(array).GetMat();

    mat.GetRectangularArray(out byte[,] data);

    string dump = Dump(data, new List<int>());

    Console.WriteLine(dump);
}

結果:
[ [ 255, 255, 255, 255, 255, 255, 255, 255 ],
[ 255, 255, 255, 255, 255, 255, 255, 255 ],
[ 255, 255, 0, 0, 0, 0, 255, 255 ],
[ 255, 255, 255, 255, 255, 255, 255, 255 ],
[ 255, 255, 255, 255, 255, 255, 255, 255 ] ]

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2