kas*_*kas 5 c# algorithm pixel colors
所以基本上,我需要一个可以传递红色、绿色和蓝色值的方法,并且该方法应该返回 RGB 值的组合是否会产生明显的红色、绿色或蓝色。如果组合没有产生这三种颜色中的任何一种,则应返回 false。
我尝试使用条件语句来做到这一点,尝试了一些变体,但没有什么是准确的。
例如我尝试过:
if (B > 100 && B > G / 2 && B > R / 2)
{
blueCount++;
}
else if (G > 100 && G > G / 2 && G > B / 2)
{
greenCount++;
}
else if (R > 100 && R > G / 2 && R > B / 2)
{
redCount++;
}
// R , G , B contains 0-255 values
Run Code Online (Sandbox Code Playgroud)
如果你将“有颜色”定义为:
那么你的代码看起来几乎不错(只需替换/为*)。
我只想使用 anenum作为结果:
enum Ish
{
Other, // 0 so this would be false if you convert it to bool
Red, // 1
Green, // 2
Blue // 3
}
Run Code Online (Sandbox Code Playgroud)
if (R>100 && R>G*2 && R>B*2)
return Ish.Red;
if (G>100 && G>R*2 && G>B*2) // you had G>G/2 here /!\
return Ish.Green;
if (B>100 && B>G*2 && B>R*2)
return Ish.Blue;
return Ish.Other;
Run Code Online (Sandbox Code Playgroud)
我使用了两次 2,但我认为你可以使用其他值,只要它是(例如,>1你不能说蓝色占主导地位B <= R)
这将是 R=100 时可能出现的红色值(左图的系数为 2,右图的系数为 1):
R=200 时(左图的系数为 2,右图的系数为 1):
因此您可能可以使用 1 到 2 之间的系数
对于 R=200,您可以看到红色,具体取决于以下因素: