WinRT アプリで画像のヒストグラム表現を作成する際に問題が発生しています。私が作りたいのは、画像の赤、緑、青、明度の 4 つのヒストグラム プロットで構成されています。
私の主な問題は、そのヒストグラムの画像を実際に描画して、画面に表示できるようにする方法です。これまでの私のコードはかなり... 乱雑です。このトピックをたくさん検索しました。ほとんどの結果は Java のコードで構成されていました。これをどうにかして C# に翻訳しようとしていますが、API はかなり異なります... AForgeからも試みがありましたが、それはwinformsです...
これが私の厄介なコードです。見た目が悪いことはわかっていますが、最初にこれを機能させるよう努めています。
public static WriteableBitmap CreateHistogramRepresentation(long[] histogramData, HistogramType type)
{
//I'm trying to determine a max height of a histogram bar, so
//I could determine a max height of the image that then I'll remake it
//at a lower resolution :
var max = histogramData[0];
//Determine the max value, the highest bar in the histogram, the initial height of the image.
for (int i = 0; i < histogramData.Length; i++)
{
if (histogramData[i] > max)
max = histogramData[i];
}
var bitmap = new WriteableBitmap(256, 500);
//Set a color to draw with according to the type of the histogram :
var color = Colors.White;
switch (type)
{
case HistogramType.Blue :
{
color = Colors.RoyalBlue;
break;
}
case HistogramType.Green:
{
color = Colors.OliveDrab;
break;
}
case HistogramType.Red:
{
color = Colors.Firebrick;
break;
}
case HistogramType.Luminosity:
{
color = Colors.DarkSlateGray;
break;
}
}
//Compute a scaler to scale the bars to the actual image dimensions :
var scaler = 1;
while (max/scaler > 500)
{
scaler++;
}
var stream = bitmap.PixelBuffer.AsStream();
var streamBuffer = new byte[stream.Length];
//Make a white image initially :
for (var i = 0; i < streamBuffer.Length; i++)
{
streamBuffer[i] = 255;
}
//Color the image :
for (var i = 0; i < 256; i++) // i = column
{
for (var j = 0; j < histogramData[i] / scaler; j++) // j = line
{
streamBuffer[j*256*4 + i] = color.B; //the image has a 256-pixel width
streamBuffer[j*256*4 + i + 1] = color.G;
streamBuffer[j*256*4 + i + 2] = color.R;
streamBuffer[j*256*4 + i + 2] = color.A;
}
}
//Write the Pixel Data into the Pixel Buffer of the future Histogram image :
stream.Seek(0, 0);
stream.Write(streamBuffer, 0, streamBuffer.Length);
return bitmap.Flip(WriteableBitmapExtensions.FlipMode.Horizontal);
}
これにより、かなり悪いヒストグラム表現が作成され、対応する色で色付けさえされません...正しく機能していません。修正するために取り組んでいます...
リンクで貢献できる場合は、WinRT アプリのヒストグラム表現のコードを知っているか、その他すべてが大歓迎です。