如何将位图缩小为一组已知的 RGB 颜色 [英] How do I reduce a bitmap to a known set of RGB colours

查看:29
本文介绍了如何将位图缩小为一组已知的 RGB 颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个业余爱好项目,我将构建一个程序,当给定图像位图时,该程序将创建十字绣图案作为 PDF.我将在 Mac 上使用 Cocoa/Objective C.

For a hobby project I'm going to build a program that when given an image bitmap will create a cross-stitch pattern as a PDF. I'll be using Cocoa/Objective C on a Mac.

源位图通常是 24bpp 图像,但在数百万种可用颜色中,只有少数作为十字绣线存在.线程有多种类型.DMC 是最广泛可用的,几乎它们的整个范围都可以从各种网站获得 RGB 值.例如,这是一个.

The source bitmap will typically be a 24bpp image, but of the millions of colours available, only a few exist as cross-stitch threads. Threads come in various types. DMC is the most widely available, and almost their entire range is available as RGB values from various web sites. Here's one, for instance.

DMC#  Name               R   G   B
----- ------------------ --- --- ---
blanc White              255 255 255
208   Lavender - vy dk   148  91 128
209   Lavender - dk      206 148 186
210   Lavender - md      236 207 225
211   Lavender - lt      243 218 228
      ...etc...

在我看来,我的第一个问题是从图像中像素的 RGB 起点开始,从 DMC 集中选择最接近的可用颜色.在数学上找到最接近的 DMC 颜色并确保它与颜色非常接近的最佳方法是什么?

My first problem, as I see it, is from a starting point of the RGB from a pixel in the image choosing the nearest colour available from the DMC set. What's the best way of finding the nearest DMC colour mathematically, and ensuring that it's a close fit as a colour too?

尽管我将使用 Cocoa,但您可以在您发布的任何代码中随意使用伪代码(甚至 Java!).

Although I'll be using Cocoa, feel free to use pseudo-code (or even Java!) in any code you post.

推荐答案

使用LAB 颜色空间并找到最接近 欧几里得距离的颜色.在 RGB 颜色空间中执行此操作会产生违反直觉的结果.(或使用 HSL 色彩空间.)

Use the LAB color space and find the color with the nearest euclidean distance. Doing this in the RGB color space will yield counter-intuitive results. (Or use the HSL color space.)

因此只需遍历每个像素并在您选择的颜色空间内找到距离最近的颜色.请注意,对于某些颜色空间(例如,使用 hue 的颜色空间)必须循环计算距离.

So just iterate over each pixel and find the color with the closest distance within the color space you choose. Note that the distance must be computed circularly for some color spaces (e.g. those employing hue).

(大多数颜色量化都围绕实际选择调色板展开,但在您的情况下已经解决了这一问题,因此您无法使用更流行的量化技术.)

(Most color quanization revolves around actually choosing a palette, but that has already been taken care of in your case, so you can't use the more popular quantization techniques.)

另外,请查看 要在 Cocoa 中找到 HSB 色调,您可以使用 在 NSColor.h 中声明的 getHue 方法.

To find the HSB hue in Cocoa, it looks like you can use the getHue method declared in NSColor.h.

但是,如果您只是使用这种技术将图像转换为十字绣设计,实际拼接起来会非常困难.它将充满单像素色域,有点违背了十字绣的目的.

However, if you just convert an image to a cross-stitch design using this technique, it will be very hard to actually stitch it. It will be full of single-pixel color fields, which sort of defeats the purpose of cross-stitching.

这篇关于如何将位图缩小为一组已知的 RGB 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆