检查像素(RGB 或 HSV)是否包含在色阶范围内,如何

发布于 2024-10-19 19:19:10 字数 360 浏览 3 评论 0原文

我需要检查像素(RGB)是否包含在从非常浅的粉色到深紫色的色阶中。 使用 RGB 方案我可以进行这样的检查:

IF image [x, y] [R]> threshold and image [x, y] [G]> threshold image [x, y] [B]> threshold and \
     image [x, y] [R] <threshold and image [x, y] [G] < threshold image [x, y] [B] <threshold THAN ...

如果没有,我还可以选择将像素置于 HSV 中。

谢谢!

I need to check if a pixel (RGB) is contained in the color scale ranging from very light pink to dark purple.
Using the RGB scheme can I do a check like this:

IF image [x, y] [R]> threshold and image [x, y] [G]> threshold image [x, y] [B]> threshold and \
     image [x, y] [R] <threshold and image [x, y] [G] < threshold image [x, y] [B] <threshold THAN ...

?

If not, I also have available the option of having the pixel in HSV.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两仪 2024-10-26 19:19:10

您可能想要使用 HSV,因为它比 RGB 更直观地映射到人类感知。如果你让一个人将颜色标记为“在范围内”或“在范围外”,并将它们映射到 HSV 中,你将能够找到指示进入和退出的 H、S 和 V 值的界限。

然后,您可以使用简单的范围检查(如您概述的那样)来确定颜色是在内部还是在外部。如果您需要 Python 帮助进行颜色空间转换,标准库模块 colorsys 将为您完成。

You probably want to work in HSV, because it maps much more intuitively to human perception than RGB. If you get a human to mark colors as "in range" or "out of range", and map them in HSV, you'll be able to find a bound on the H, S, and V values that indicates in and out.

Then you can use a simple range check like you've outlined about to determine if a color is in or out. If you need Python help with the color space conversions, the standard library module colorsys will do it for you.

旧人 2024-10-26 19:19:10

使用每通道截止值相当于在 RGB 颜色空间中指定一个轴对齐的矩形框。盒子是一种令人讨厌的形状 - 您可能必须包含一些您并不真正想要的区域(在角落),以避免排除您确实想要的区域(在脸部的中心)。

如果您选择两个锚点和一个截止距离,则它相当于任意对齐的 3D 椭球体 - 它有望更准确地匹配您想要的颜色。

类似的东西

def colorRange(ax, ay, az, bx, by, bz, dist):
    def testPixel(cx, cy, cz):
        return (
            (cx-ax)**2 + (cy-ay)**2 + (cz-az)**2
            + (cx-bx)**2 + (cy-by)**2 + (cz-bz)**2
        ) < dist**2
    return testPixel

isPurple = colorRange(140,0,140, 255,75,255, 230)
isPurple(203,46,195)  # -> True

Using per-channel cut-off values is equivalent to designating an axis-aligned rectangular box in RGB color space. A box is kind of a nasty shape - you will likely have to include some areas you don't really want (in the corners) to avoid excluding areas you do want (in the center of the faces).

If you instead choose two anchor points and a cut-off distance, it is the equivalent of an arbitrarily aligned 3d ellipsoid - which should hopefully more accurately match the colors you want.

Something like

def colorRange(ax, ay, az, bx, by, bz, dist):
    def testPixel(cx, cy, cz):
        return (
            (cx-ax)**2 + (cy-ay)**2 + (cz-az)**2
            + (cx-bx)**2 + (cy-by)**2 + (cz-bz)**2
        ) < dist**2
    return testPixel

isPurple = colorRange(140,0,140, 255,75,255, 230)
isPurple(203,46,195)  # -> True
っ〆星空下的拥抱 2024-10-26 19:19:10

如果您谈论的是整数 RGB 值,则可以将所有值放入 set 中,然后只需执行 if color in set:

如果您的“颜色范围”是以复杂的方式定义,定义一组具体的颜色可能是唯一的方法。

If you're talking about integer RGB values, you can put all of the values into a set and then simply do if color in set:

If your 'color range' is defined in a complex way, defining a concrete set of colors might be the only way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文