So jemand mir geholfen, diesen Code zu machen, die mir am meisten verwendete Farbe in einem Foto sagen werden:zeigen am häufigsten verwendete Farbe in einem Bild
class PictureAnalysis
{
public static List<Color> TenMostUsedColors { get; private set; }
public static List<int> TenMostUsedColorIncidences { get; private set; }
public static Color MostUsedColor { get; private set; }
public static int MostUsedColorIncidence { get; private set; }
private static int pixelColor;
private static Dictionary<int, int> dctColorIncidence;
public static void GetMostUsedColor(Bitmap theBitMap)
{
TenMostUsedColors = new List<Color>();
TenMostUsedColorIncidences = new List<int>();
MostUsedColor = Color.Empty;
MostUsedColorIncidence = 0;
// does using Dictionary<int,int> here
// really pay-off compared to using
// Dictionary<Color, int> ?
// would using a SortedDictionary be much slower, or ?
dctColorIncidence = new Dictionary<int, int>();
// this is what you want to speed up with unmanaged code
for (int row = 0; row < theBitMap.Size.Width; row++)
{
for (int col = 0; col < theBitMap.Size.Height; col++)
{
pixelColor = theBitMap.GetPixel(row, col).ToArgb();
if (dctColorIncidence.Keys.Contains(pixelColor))
{
dctColorIncidence[pixelColor]++;
}
else
{
dctColorIncidence.Add(pixelColor, 1);
}
}
}
// note that there are those who argue that a
// .NET Generic Dictionary is never guaranteed
// to be sorted by methods like this
var dctSortedByValueHighToLow = dctColorIncidence.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
// this should be replaced with some elegant Linq ?
foreach (KeyValuePair<int, int> kvp in dctSortedByValueHighToLow.Take(10))
{
TenMostUsedColors.Add(Color.FromArgb(kvp.Key));
TenMostUsedColorIncidences.Add(kvp.Value);
}
MostUsedColor = Color.FromArgb(dctSortedByValueHighToLow.First().Key);
MostUsedColorIncidence = dctSortedByValueHighToLow.First().Value;
}
}
und ich versuche, so zu implementieren, aber ich weiß nicht wirklich wissen, was ich tun soll, um mir die am häufigsten verwendete Farbe zu zeigen?
string filep = @"C:\Users\User\Desktop\Gallery\image" + NumberOfClick.ToString() + "cropped.png";
Bitmap bMap = Bitmap.FromFile(filep) as Bitmap;
PictureAnalysis.GetMostUsedColor(bMap);
Ich mag das am häufigsten verwendete Farbe von einem „echten“ Foto wie dies bestimmen: I am cropping her "jacket" from the photo and I want a program that determines it as it is black
Wie Willst du es zeigen? –
als String, wie in einer Messagebox –
'MessageBox.Show (" Meist verwendete Farbe ist "+ PictureAnalysis.MostUsedColor.ToString());' ... aber ernst. Wie möchten Sie diese Farbe anzeigen? Einen neuen Dialog anzeigen, irgendwo in UI einfügen (wie ein Rechteck, das mit dieser Farbe gefüllt ist)? –