python找色_Python-查找相似的颜色,最好的方法

2023-11-09

1586010002-jmsa.png

I've made a function to find a color within a image, and return x, y. Now I need to add a new function, where I can find a color with a given tolerence. Should be easy?

Code to find color in image, and return x, y:

def FindColorIn(r,g,b, xmin, xmax, ymin, ymax):

image = ImageGrab.grab()

for x in range(xmin, xmax):

for y in range(ymin,ymax):

px = image.getpixel((x, y))

if px[0] == r and px[1] == g and px[2] == b:

return x, y

def FindColor(r,g,b):

image = ImageGrab.grab()

size = image.size

pos = FindColorIn(r,g,b, 1, size[0], 1, size[1])

return pos

Outcome:

Taken from the answers the normal methods of comparing two colors are in Euclidean distance, or Chebyshev distance.

I decided to mostly use (squared) euclidean distance, and multiple different color-spaces. LAB, deltaE (LCH), XYZ, HSL, and RGB. In my code, most color-spaces use squared euclidean distance to compute the difference.

For example with LAB, RGB and XYZ a simple squared euc. distance does the trick:

if ((X-X1)^2 + (Y-Y1)^2 + (Z-Z1)^2) <= (Tol^2) then

...

LCH, and HSL is a little more complicated as both have a cylindrical hue, but some piece of math solves that, then it's on to using squared eucl. here as well.

In most these cases I've added "separate parameters" for tolerance for each channel (using 1 global tolerance, and alternative "modifiers" HueTol := Tolerance * hueMod or LightTol := Tolerance * LightMod).

It seems like colorspaces built on top of XYZ (LAB, LCH) does perform best in many of my scenarios. Tho HSL yields very good results in some cases, and it's much cheaper to convert to from RGB, RGB is also great tho, and fills most of my needs.

解决方案

Computing distances between RGB colours, in a way that's meaningful to the eye, isn't as easy a just taking the Euclidian distance between the two RGB vectors.

There is an interesting article about this here: http://www.compuphase.com/cmetric.htm

The example implementation in C is this:

typedef struct {

unsigned char r, g, b;

} RGB;

double ColourDistance(RGB e1, RGB e2)

{

long rmean = ( (long)e1.r + (long)e2.r ) / 2;

long r = (long)e1.r - (long)e2.r;

long g = (long)e1.g - (long)e2.g;

long b = (long)e1.b - (long)e2.b;

return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));

}

It shouldn't be too difficult to port to Python.

EDIT:

Alternatively, as suggested in this answer, you could use HLS and HSV. The colorsys module seems to have functions to make the conversion from RGB. Its documentation also links to these pages, which are worth reading to understand why RGB Euclidian distance doesn't really work:

EDIT 2:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python找色_Python-查找相似的颜色,最好的方法 的相关文章

随机推荐

  • 数据库文档管理化开源项目工具SmartSQL

    数据库文档管理化开源项目工具SmartSQL 为何写该博文 由于这段时间需要理清软件的相关表结构 以及在客户端操作时使用 SQL Server Profiler 来检索一些简单的CURD sql语句 为了更好高效的理清内部的一些表结构 视图
  • react eslint解决方案整理

    eslint 解决方案整理 最近在处理react项目中报的warning 进行了以下整理 参考文档 http eslint cn docs rules 项目中遇到warning的解决 xxx is defined but never use
  • 使用EKF融合odometry及imu数据

    整理资料发现早前学习robot pose ekf的笔记 大抵是一些原理基础的东西加一些自己的理解 可能有不太正确的地方 当时做工程遇到的情况为机器人在一些如光滑的地面上打滑的情形 期望使用EKF利用imu对odom数据进行校正 就结果来看
  • python 点云配准_TEASER++:快速鲁棒的C++点云配准库

    TEASER fast certifiable 3D registration TEASER is a fast and certifiably robust point cloud registration library written
  • 在腾讯开发 QQ IM 的工作体验是怎样的?

    转载 http blog csdn net kobejayandy article details 8685271 目录 一 引言 二 个人网站 三 Oracle 支付宝 旺旺 四 淘宝技术发展 Java时代 脱胎换骨 五 淘宝技术发展 J
  • NIO:通道Channel讲解

    了解了缓冲区后 下来需要了解真正传输数据的通道Channel Channel是做什么用的 再来简单回顾一下 通道顾名思义就是传递的介质 Channel就类似于传统IO中的流 是直接对接操作系统的 当我们处理好缓冲区后 就可以通过缓冲区对通道
  • linux的程序安装失败,linux 安装软件各种错误集锦及解决方法

    1 最小化安装了centos 但是使用ifconfig命令时候出现 bash ifconfig command not found 解决方法 yum y install net tools x86 64 2 bash getenforce
  • 注册mysql到服务中

    前言 如果命令行输入net start mysql 提示 服务名无效 就表示你还没有将mysql注册到服务中去 因为net start 服务名 启动的是win下注册的服务 接下来的教程会手把手的教你如何将MySQL注册到win服务里面 注册
  • PostgreSQL jdbc 9.4 支持load balance 和 connection failover了

    Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开 本次大会嘉宾阵容强大 国内顶级PostgreSQL数据库专家将悉数到场 并特邀欧洲 俄罗斯 日本 美国等国家和地区的数据库方面专家助阵 Postgres XC
  • js使用theamleaf的值

  • Fn+F11/F12无法调整屏幕亮度的问题

    设备管理器看下监视器是否显示通用即插即用显示器 双击打开 位置显示 在 Intel UHD Graphics 上 右击开始菜单 设备管理器 监视器 双击 如果不是的话就是其他乱七八糟的远程软件 或者是什么鬼东西给显示器安装了个软件驱动方便它
  • 求二叉树中度为1的结点个数

    内容 若用二叉链表作为二叉树的存储表示 设计算法求二叉树中度为1的结点个数 步骤 1 算法分析 本题需要采用二叉链表作为二叉树的存储结构 来求解二叉树中度为1的结点个数 大体上分为两部分 第一 建立二叉树 此处采用先序的方式建立二叉树 其次
  • 新电脑的正确打开方式——(近万字图文并茂详细分步骤讲解)【万一那天你就有新电脑了呢】包括个性化·等你来解锁哦

    新电脑买回来如何让配置 新电脑买回来如何装饰 新电脑买回来如何让电脑更干净 等等 这些问题都在困惑着大家 那么接下来我急来告诉大家 新电脑的正确打开方式吧 建议收藏此博客 万一那天你也有属于自己的新机了呢 似此星辰非昨夜 为谁风露立中宵 作
  • 计算机毕业设计 基于SpringBoot餐厅点餐系统的设计与实现 Java实战项目 附源码+文档+视频讲解

    博主介绍 从事软件开发10年之余 专注于Java技术领域 Python人工智能及数据挖掘 小程序项目开发和Android项目开发等 CSDN 掘金 华为云 InfoQ 阿里云等平台优质作者 文末获取源码联系 精彩专栏推荐订阅 不然下次找不到
  • 03_uboot的源码目录分析

    一 文件夹 1 api 硬件无关的功能函数的API 这些函数是uboot本身使用的 uboot移植时基本不用管 2 api examples API相关的测试事例代码 3 board board是板的意思 就是开发板 这个文件夹下放的每一个
  • mysql优化(1)show命令 慢查询日志 explain profiling

    原文地址 http zhumeng8337797 blog 163 com blog static 1007689142012095142876 suggestedreading wumii 目录 一 优化概述 二 查询与索引优化分析 1性
  • 正则表达式判断是否以某字符串结尾

    前言 正则来实现判断他是否以指定字符串结尾 实现效果 实现思路 判断他是否以指定字符串结尾 实现代码 判断是否以 jpeg结尾 let str ajsjsjsjsjjsjjpeg let str1 jpegajsjsjsjsjjsjjpmm
  • Python 面向对象(二)

    6 2 继承 继承就是一个类可以获得另外一个类中的成员属性和成员方法 作用 减少代码 增加代码的复用功能 同时可以设置类与类直接的关系 继承与被继承的概念 被继承的类叫父类 也叫基类 也叫超类 用与继承的类 叫子类 也叫派生类 继承与被继承
  • 一步一步教你如何在Altium Designer中导入ALLEGRO的brd文件

    课题组其他人都用Allegro 我一个人死撑着用AD 共享设计时原理图还方便导入 PCB文件麻烦了 于是一直在找如何从AD中导入Allegro的brd文件的方法 一直未果 曾经找了个CAMCAD Pro 号称可以 但不会用 呵呵 也可能是不
  • python找色_Python-查找相似的颜色,最好的方法

    I ve made a function to find a color within a image and return x y Now I need to add a new function where I can find a c