我目前正在尝试使用Accord.NET框架中的KMeans算法。
继承我的代码:
private Bitmap runKMeans(Bitmap image)
{
// Retrieve the number of clusters
int k = 5;
// Create converters
ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);
ArrayToImage arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);
// Transform the image into an array of pixel values
double[][] pixels;
imageToArray.Convert(image, out pixels);
// Create a K-Means algorithm using given k and a
// square Euclidean distance as distance metric.
KMeans kmeans = new KMeans(k, Distance.SquareEuclidean);
// Compute the K-Means algorithm until the difference in
// cluster centroids between two iterations is below 0.05
int[] idx = kmeans.Compute(pixels, 0.05);
// Replace every pixel with its corresponding centroid
pixels.ApplyInPlace((x, i) => kmeans.Clusters.Centroids[idx[i]]);
//Create a Bitmap and return it
Bitmap result; arrayToImage.Convert(pixels, out result);
return result;
}
当我运行程序时,此行会抛出异常:
int[] idx = kmeans.Compute(pixels, 0.05);
经过一些研究后我发现KMeans算法的随机数生成器抛出了这个异常。有谁知道如何解决这个问题?键入:" System.InvalidOperationException"生成的值不在0和1之间。