如何算法上色歌曲列表在iTunes 11的工作? [英] How does the algorithm to color the song list in iTunes 11 work?

查看:138
本文介绍了如何算法上色歌曲列表在iTunes 11的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的iTunes 11的专辑的歌曲列表,挑选颜色的字体和背景在专辑封面的功能,一个非常漂亮的景色。任何人都知道如何在算法的工作?

The new iTunes 11 has a very nice view for the song list of an album, picking the colors for the fonts and background in function of album cover. Anyone figured out how the algorithm works?

推荐答案

我在近似数学iTunes的11色算法给出的专辑封面作为输入:

I approximated the iTunes 11 color algorithm in Mathematica given the album cover as input:

经过反复试验,我想出了一个算法,适用于〜80%,与我测试过它的专辑。

Through trial and error, I came up with an algorithm that works on ~80% of the albums with which I've tested it.

的算法处理找到图像的主色的大部分。甲prerequisite找到主色,但是,在计算两种颜色之间的量化差值。计算两个颜色之间的差别的一种方法是计算在RGB色彩空间中的欧几里得距离。然而,人类色彩感觉不与距离在RGB颜色空间匹配得很好。

The bulk of the algorithm deals with finding the dominant color of an image. A prerequisite to finding dominant colors, however, is calculating a quantifiable difference between two colors. One way to calculate the difference between two colors is to calculate their Euclidean distance in the RGB color space. However, human color perception doesn't match up very well with distance in the RGB color space.

所以,我写了一个函数来转换RGB颜色(格式为 {1,1,1} )来的YUV 时,颜色空间是在近似颜色感觉好得多:

Therefore, I wrote a function to convert RGB colors (in the form {1,1,1}) to YUV, a color space which is much better at approximating color perception:

<子>(编辑: @cormullion 和的 @德雷克指出,Mathematica的内置CIELAB和CIELUV色彩空间将是一种合适的......貌似我重新发明轮子了一下这里)

( @cormullion and @Drake pointed out that Mathematica's built-in CIELAB and CIELUV color spaces would be just as suitable... looks like I reinvented the wheel a bit here)

convertToYUV[rawRGB_] :=
    Module[{yuv},
        yuv = {{0.299, 0.587, 0.114}, {-0.14713, -0.28886, 0.436},
            {0.615, -0.51499, -0.10001}};
        yuv . rawRGB
    ]

接下来,我写了一个函数来计算颜色距离与上述的转换:

Next, I wrote a function to calculate color distance with the above conversion:

ColorDistance[rawRGB1_, rawRGB2_] := 
    EuclideanDistance[convertToYUV @ rawRGB1, convertToYUV @ rawRGB2]

主导颜色

我很快发现,内置的数学函数 DominantColors 不允许足够的细粒度控制,以近似地认为iTunes中使用的算法。我写我自己的功能,而不是...

Dominant Colors

I quickly discovered that the built-in Mathematica function DominantColors doesn't allow enough fine-grained control to approximate the algorithm that iTunes uses. I wrote my own function instead...

有一个简单的方法来计算一组像素的主色是收集所有像素成相似颜色的桶,然后找到最大的水桶。

A simple method to calculate the dominant color in a group of pixels is to collect all pixels into buckets of similar colors and then find the largest bucket.

DominantColorSimple[pixelArray_] :=
    Module[{buckets},
        buckets = Gather[pixelArray, ColorDistance[#1,#2] < .1 &];
        buckets = Sort[buckets, Length[#1] > Length[#2] &];
        RGBColor @@ Mean @ First @ buckets
    ]

<子>注意 .1 是容忍如何不同的颜色必须要考虑的区分开来。还要注意的是,虽然输入的是原始三重形式的像素阵列( {{1,1,1},{0,0,0}} ),我回一个数学 RGBColor 元素,以更好地逼近内置 DominantColors 的功能。

Note that .1 is the tolerance for how different colors must be to be considered separate. Also note that although the input is an array of pixels in raw triplet form ({{1,1,1},{0,0,0}}), I return a Mathematica RGBColor element to better approximate the built-in DominantColors function.

我的实际功能 DominantColorsNew 添加返回至 N 主色过滤掉了一个给定的其它颜色后的选择。这也暴露了公差为每种颜色比较:

My actual function DominantColorsNew adds the option of returning up to n dominant colors after filtering out a given other color. It also exposes tolerances for each color comparison:

DominantColorsNew[pixelArray_, threshold_: .1, n_: 1, 
    numThreshold_: .2, filterColor_: 0, filterThreshold_: .5] :=
    Module[
        {buckets, color, previous, output},
        buckets = Gather[pixelArray, ColorDistance[#1, #2] < threshold &];
        If[filterColor =!= 0, 
        buckets = 
            Select[buckets, 
                ColorDistance[ Mean[#1], filterColor] > filterThreshold &]];
        buckets = Sort[buckets, Length[#1] > Length[#2] &];
        If[Length @ buckets == 0, Return[{}]];
        color = Mean @ First @ buckets;
        buckets = Drop[buckets, 1];
        output = List[RGBColor @@ color];
        previous = color;
        Do[
            If[Length @ buckets == 0, Return[output]];
            While[
                ColorDistance[(color = Mean @ First @ buckets), previous] < 
                    numThreshold, 
                If[Length @ buckets != 0, buckets = Drop[buckets, 1], 
                    Return[output]]
            ];
            output = Append[output, RGBColor @@ color];
            previous = color,
            {i, n - 1}
        ];
        output
    ]

的算法的剩余

首先,我调整了专辑封面( 36px 36px )及减少了详细的双边滤波器

The Rest of the Algorithm

First I resized the album cover (36px, 36px) & reduced detail with a bilateral filter

image = Import["http://i.imgur.com/z2t8y.jpg"]
thumb = ImageResize[ image, 36, Resampling -> "Nearest"];
thumb = BilateralFilter[thumb, 1, .2, MaxIterations -> 2];

的iTunes拾取通过找出沿曲集的边缘处的主色的背景色。然而,剪切图像忽略狭窄的专辑封面边框。

iTunes picks the background color by finding the dominant color along the edges of the album. However, it ignores narrow album cover borders by cropping the image.

thumb = ImageCrop[thumb, 34];

接下来,我发现主色(用新的功能上面)沿图像使用默认宽容的最外边缘 .1

border = Flatten[
    Join[ImageData[thumb][[1 ;; 34 ;; 33]] , 
        Transpose @ ImageData[thumb][[All, 1 ;; 34 ;; 33]]], 1];
background = DominantColorsNew[border][[1]];

最后,我的图像中返回的2主色作为一个整体,告诉函数滤除背景颜色以及

Lastly, I returned 2 dominant colors in the image as a whole, telling the function to filter out the background color as well.

highlights = DominantColorsNew[Flatten[ImageData[thumb], 1], .1, 2, .2, 
    List @@ background, .5];
title = highlights[[1]];
songs = highlights[[2]];

以上<子>的公差值如下: .1 是独立的颜色之间的最小差异; 0.2 众多主色(较低的值可能会返回黑色和深灰色,而较高的值,可以确保更多样性的主色)之间的最小差异; 0.5 是占主导地位的颜色和背景(较高的值会产生高对比度的颜色组合)

The tolerance values above are as follows: .1 is the minimum difference between "separate" colors; .2 is the minimum difference between numerous dominant colors (A lower value might return black and dark gray, while a higher value ensures more diversity in the dominant colors); .5 is the minimum difference between dominant colors and the background (A higher value will yield higher-contrast color combinations)

瞧!

Graphics[{background, Disk[]}]
Graphics[{title, Disk[]}]
Graphics[{songs, Disk[]}]

该算法可非常普遍适用。我调整了上面的设置,并在那里工作产生通常是正确的颜色〜80%的唱片涵盖了我测试点的公差值。当 DominantColorsNew 没有发现两种颜色换来的亮点出现一些极端情况(即当专辑封面是黑白)。我的算法没有处理这些案件,但它是琐碎重复的iTunes的功能:这张专辑产生不到两个亮点的时候,标题变为白色或黑色取决于与背景的最佳对比度。然后,歌曲成为一大亮点的颜色(如果有),或在标题颜色隐退了一点。

The algorithm can be applied very generally. I tweaked the above settings and tolerance values to the point where they work to produce generally correct colors for ~80% of the album covers I tested. A few edge cases occur when DominantColorsNew doesn't find two colors to return for the highlights (i.e. when the album cover is monochrome). My algorithm doesn't address these cases, but it would be trivial to duplicate iTunes' functionality: when the album yields less than two highlights, the title becomes white or black depending on the best contrast with the background. Then the songs become the one highlight color if there is one, or the title color faded into the background a bit.

这篇关于如何算法上色歌曲列表在iTunes 11的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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