使用PIL的RGB到HSV转换 [英] RGB to HSV conversion using PIL

查看:1012
本文介绍了使用PIL的RGB到HSV转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动增强一些要转移到数码相框的图像.我有适当的代码可以调整大小,在图像的最低有效(最小细节)角添加日期/时间,并将成对的肖像图像粘贴在一起,以避免在帧的41:20低分辨率屏幕中显示单个肖像. /p>

我使用 itertools 技巧,转换也要花费很多时间.我设法使用 psyco 进行了改进.

但是,我注意到了PIL的示例 Image.convert 使用4×4矩阵作为第二个参数,可以将RGB转换为 XYZ颜色空间. convert方法,我想知道:

如何使用convert方法调用中的自定义矩阵将RGB转换为HSV(然后再将HSV转换回RGB)? (在这种情况下,较小的舍入误差并不重要,因此我不介意每个波段都将表示为一系列0…255的整数)

谢谢.

解决方案

尽管我已经看到参考文献[1]声称HSV颜色空间是从RGB线性变换的,但这似乎暗示可以使用矩阵,我无法自己找到或确定这种矩阵的外观.在某种程度上,基于我也已经看到的所有[相似]非矩阵过程实现,这并没有让我感到惊讶-他们的实现方式并非线性. >

无论如何,在研究此问题时,我碰到了前SGI研究人员的一篇[有些过时]的文章 Paul Haeberli 的在线计算机图形笔记本,标题为 用于图像处理的矩阵运算 描述了如何使用4x4矩阵进行多种不同的颜色转换,这可能会对您有所帮助.给出的所有示例都可以直接在RGB彩色图像上运行,并且像几何矩阵转换一样,可以使用级联将它们的任何序列组合为一个矩阵.

希望这会有所帮助.


[1]:颜色空间转换< http://www.poynton .com/PDFs/coloureq.pdf >:

2.7.3 HSL(色相饱和度和亮度)

这代表了很多类似的东西 颜色空间,备用名称 包括HSI(强度),HSV(值), HCI(色度/色彩),HVC, TSD(色相饱和度和黑暗度)等 这些颜色空间大多数是 来自RGB的线性变换,并且 因此取决于设备和 非线性的.他们的优势在于 极其直观的方式 指定颜色.这很容易 选择所需的色调,然后 通过调整对它稍作修改 饱和度和强度.

I'm trying to automate the enhancement of some images that are to be transfered to a digital frame. I have code in place that resizes, adds a date/time to the least-significant (least details) corner of the image and pastes together pairs of portrait images to avoid displaying a single portrait in the frame's 41:20 low resolution screen.

I've implemented a brightness-stretching filter for those pictures where the lighting wasn't so good, using the colorsys.rgb_to_hsv function to calculate H, S, V bands, operating on the V one and then converting back to RGB before saving a JPEG in the digital frame. Obviously, the conversion takes a lot of time, even using itertools tricks; I managed to improve things using psyco.

However, I noticed an example for the PIL Image.convert where RGB can be converted to XYZ color space using a 4×4 matrix as a second argument to the convert method, and I got to wonder:

How can I convert RGB to HSV (and then HSV back to RGB) using a custom matrix in the convert method call? (Minor rounding errors are not important in this case, so I don't mind that each band will be expressed as a series of 0…255 integers)

Thank you in advance.

解决方案

Although I've seen references[1] that claim HSV color-space is linear transformation from RGB, which would seem to imply that it could be done with a matrix, I have been unable to find or determine for myself just what such a matrix would look like. In a way this doesn't really surprise me based on all the [similar] non-matrix procedural implementations I've also seen -- the way they go about it doesn't look linear.

Anyway, while looking into this, I ran across a [somewhat dated] article in former SGI researcher Paul Haeberli's online computer graphics notebook titled Matrix Operations for Image Processing which describes how to do a number of different color transformations using 4x4 matrices which might help you. All of the examples given operate directly on RGB color images and, like geometric matrix transformations, any sequence of them can be combined into a single matrix using concatenation.

Hope this helps.


[1]: Colour Space Conversions <http://www.poynton.com/PDFs/coloureq.pdf>:

2.7.3 HSL (Hue Saturation and Lightness)

This represents a wealth of similar colour spaces, alternative names include HSI (intensity), HSV (value), HCI (chroma / colourfulness), HVC, TSD (hue saturation and darkness) etc. Most of these colour spaces are linear transforms from RGB and are therefore device dependent and non–linear. Their advantage lies in the extremely intuitive manner of specifying colour. It is very easy to select a desired hue and to then modify it slightly by adjustment of its saturation and intensity.

这篇关于使用PIL的RGB到HSV转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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