使用 C# 和 OxyPlot 类创建脚本。该脚本将学习分辨向日葵图像和蒲公英图像之间的区别。
我遇到的问题是 PlotModel 的 PlotView,我添加了一个 Barseries,不显示任何数据/条形线。它确实显示了 PlotView 屏幕,但它是空的。只是一个白屏。没有轴线,我不知道如何找出问题所在。我认为这与我调用 PlotView 的方式有关。
没有数据的完全白屏表明我没有调用正确的表格。
我查看了 BarSeries 变量并确认它附加了 100 个项目。该数据的属性是“Color”,它有一个 oxyPlot 颜色值,“Value”它有一个双精度数据类型的百分比值(范围从 0.0 到最高 2.5)和一个“CategoryIndex”,它有一个int值范围从1到100.
我确认 plotModel 附有一个系列计数,并且系列中有 100 个项目。
到目前为止我所做的是收集了一个小的位图图像样本帧。然后全部调整为 128 x 128。将所有像素颜色提取为字典格式,这使我能够在所有位图图像中找到相同颜色类型的百分比/比例值。然后我将这些值传递给 Barseries(每个条形的“颜色”,作为双数的“值”,从 1 到 100 的“CategoryIndex”)。 BarSeries 被添加到 PlotModel,PlotModel 被添加到 PlotView。
我的“CategoryIndex”沿 x 轴流动,“Value”比例/百分比沿 x 轴流动(最初尝试使用 ColumnSeries,但已不存在)。因此,我的 y 轴和 x 轴标签与标准相反。
尝试解决这个问题,但没有运气。
Main 方法是 'public partial class Form1 : Form' 并包含以下代码
public void HistogramPlot()
{
Histogram histogram = new Histogram();
Dictionary<Color, Double> colorProportionAllImages = histogram.Compute(_resizedbitmapFlowersArray);
histogram.DisplayColorHistogram(colorProportionAllImages);
histogram.Show();
}
这调用具有以下代码的类“公共类直方图:窗口”
using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OxyPlot.WindowsForms;
using System.Windows;
using System.Windows.Controls;
using Accord.Statistics.Visualizations;
namespace Sunflower_Recognition
{
public class Histogram : Window
{
public Histogram()
{
// Leave Empty
}
/// <summary>
/// Creating a proportional value of how often the same color value occurred thoughout all bitmap flower images
/// </summary>
/// <param name="colorImagesResized">Bitmap array</param>
/// <returns>Dictionary of all the 'key' color values and a porportional datum of those key color values</returns>
public Dictionary<Color, double> Compute(Bitmap[] colorImagesResized)
{
// Create a Dictionary that will hold a color value 'key' and
// a proportion representation of the color value (number of times that color value has occurred)
Dictionary<Color, double> histogramDictionary = new Dictionary<Color, double>();
// To create a proportion range between 0 to 1 we need to know how many pixels, in total, we will be analysing
int numPixels = colorImagesResized[0].Width * colorImagesResized[0].Height * colorImagesResized.Length;
// Extracting each Bitmap image
foreach (Bitmap image in colorImagesResized)
{
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
// Extracting each pixel color
Color pixelColor = image.GetPixel(x, y);
// No 'duplicate key values' allowed so we first check for duplicates by
// comparing an actual pixel color value against the existing keys
if (!histogramDictionary.ContainsKey(pixelColor))
{
// No duplicate key exists so we add a new key with default datum of 0.0
histogramDictionary[pixelColor] = 0.0;
}
// Increasing the datum count for that 'key color value' by 1
histogramDictionary[pixelColor] += 1;
}
}
}
// Create a new dictionary to hold the normalized/proportioned histogram data
Dictionary<Color, double> histogramColorProportions = new Dictionary<Color, double>();
// Normalize the histogram by dividing each frequency count by the total number of pixels analysed
foreach (KeyValuePair<Color,double> keyValue in histogramDictionary)
{
// We now get each datum value from each key and alter the number to a proportional percentage value
double proportionValue = (keyValue.Value / numPixels) * 100;
histogramColorProportions.Add(keyValue.Key, proportionValue);
}
return histogramColorProportions;
}
public void DisplayColorHistogram(Dictionary<Color, double> histogramColorProportions)
{
// Create a new plot model
PlotModel plotHistogramModel = new OxyPlot.PlotModel();
// ***************** Axis formation
// Create a new 'category' x-axis for displaying the color keys
// We will display these key 'datum' values on the left side of the plot graph, as a range from 0.0 to 1.0
CategoryAxis colorProportionAxis = new CategoryAxis();
colorProportionAxis.Position = AxisPosition.Bottom;
colorProportionAxis.Title = "Proportion (0 to 1)";
// Create a new value y-axis for the key color values (frequencies) which will be a range from 0 to 255
LinearAxis colorRangeAxis = new LinearAxis();
colorRangeAxis.Position = AxisPosition.Left;
colorRangeAxis.Title = "Color range (0 to 255)";
colorRangeAxis.MinimumPadding = 0;
colorRangeAxis.MaximumPadding = 0.1;
colorRangeAxis.Minimum = 0;
colorRangeAxis.Maximum = 3.0;
// Add the axes to the plot model
plotHistogramModel.Axes.Add(colorProportionAxis);
plotHistogramModel.Axes.Add(colorRangeAxis);
// Create a new column series to represent the color histogram
BarSeries histogramSeries = new BarSeries();
histogramSeries.Title = "Flower colors by proportion";
// Is wil ensure that the columns are stacked side by side
histogramSeries.IsStacked = false;
int indexRange = 0;
// Add data points from this histogram dictionary to the histogram barseries
foreach (KeyValuePair<Color, double> keyValue in histogramColorProportions)
{
// Convert the System.Drawing.Color value (A=255, R=58, G=58, B=58) to OxyColor (ff3a3a3a)
OxyColor oxyColor = OxyColor.FromRgb(keyValue.Key.R, keyValue.Key.G, keyValue.Key.B);
// Add a new data point to the histogram series with the summation as the category index and the frequency as the value
histogramSeries.Items.Add(new BarItem { Color = oxyColor, Value = keyValue.Value, CategoryIndex = indexRange++});
}
System.Windows.MessageBox.Show("Number of Items in 'histogramSeries: " + histogramSeries.Items.Count); // !00 Items
// Add the histogram series to the plot model
plotHistogramModel.Series.Add(histogramSeries);
System.Windows.MessageBox.Show("Number of Series in the PlotModel: " + plotHistogramModel.Series.Count); // 1 series in the plotmodel
// Create a new plot view and set its model to the plot model
PlotView plotView = new OxyPlot.WindowsForms.PlotView();
plotView.Width = 10;
plotView.Height = 800;
plotView.Model = plotHistogramModel;
}
}
}