试图找到一种方法来识别特定屏幕截图上是否存在“绿色”颜色(下图)。
问题是,当使用普通的RGB位图时,它不起作用,因为图像是从网页的屏幕截图中获取的,并且图像有点模糊,因此很多像素看起来都不像“绿色”。
我需要某种方式来理解如何定义特定屏幕截图上是否存在“绿色”
答案 0 :(得分:2)
我需要某种方式知道是否有绿色
迭代所有像素并搜索所需的颜色
Color c = Color.Green; //or the color you want to search
Bitmap bmp = new Bitmap(Image.FromFile(@"c:\file.bmp"));
bool containsgreen = false;
for (int w = 0; w < bmp.Width; w++)
for (int h = 0; h < bmp.Height; h++)
if (bmp.GetPixel(w, h) == c)
containsgreen = true;
如果您正在寻找颜色范围或相似的颜色,还可以计算颜色距离以增加公差
public static double GetColourDistance(Color e1, Color e2)
{
return Math.Sqrt((e1.R - e2.R) * (e1.R - e2.R) + (e1.G - e2.G) * (e1.G - e2.G) + (e1.B - e2.B) * (e1.B - e2.B));
}
答案 1 :(得分:1)
我将从您的问题中假设,当您说 green 时,您并不意味着任何像素对{{ 1}}颜色,但是您的意思是对于人类来说,它看起来是绿色的。
如果是这种情况,我建议对@fubo的代码进行修改,以计算“视觉绿色”。那就是class TabBarController: UITabBarController , UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return tabBarController.selectedIndex != tabBarController.viewControllers?.index(of: viewController)
}
}
分量大于其他分量的情况。
现在,这将返回G
,用于一些粗略的绿色,例如一种非常非常暗或非常非常浅的绿色。如果要过滤掉这些,请使用您选择的公差值。
代码如下:
RGB
如果您要查找“绿色中的主要绿色是什么”,则可以通过计算颜色并将其放入存储桶(即histogram)中来进一步修改此算法。