首頁 > 後端開發 > Python教學 > 如何用简单的算法生成一个类似『光盘』的彩色圆形图片?

如何用简单的算法生成一个类似『光盘』的彩色圆形图片?

WBOY
發布: 2016-06-06 16:23:57
原創
1538 人瀏覽過

大概就像下图所示的样子
如何用简单的算法生成一个类似『光盘』的彩色圆形图片?

回复内容:

补充一下MATLAB的代码:
<code class="language-matlab"><span class="n">t</span> <span class="p">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">:.</span><span class="mi">02</span><span class="p">:</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="nb">pi</span><span class="p">;</span>
<span class="n">r</span> <span class="p">=</span> <span class="mi">0</span><span class="p">:.</span><span class="mi">02</span><span class="p">:</span><span class="mi">1</span><span class="p">;</span>
<span class="n">pcolor</span><span class="p">(</span><span class="nb">cos</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">'*</span><span class="n">r</span><span class="p">,</span><span class="nb">sin</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">'*</span><span class="n">r</span><span class="p">,</span><span class="n">t</span><span class="o">'*</span><span class="p">(</span><span class="n">r</span><span class="o">==</span><span class="n">r</span><span class="p">))</span>
<span class="n">colormap</span><span class="p">(</span><span class="n">hsv</span><span class="p">(</span><span class="mi">256</span><span class="p">)),</span> <span class="n">shading</span> <span class="n">interp</span><span class="p">,</span> <span class="n">axis</span> <span class="n">image</span> <span class="n">off</span>
</code>
登入後複製
...被抢先了...

基本上就是在这个圆上颜色的色相(Hue),只与该点与圆心连线的夹角有关。知道这一点你就做一幅图像,然后遍历每一个点,把该点的坐标值(x,y)转换为极坐标(r,\theta ),色相值就是极坐标角度\theta /2\pi

用 matplotlib 实现的话有个小技巧,把imshow的cmap改为hsv就可以直接按照色相来画。

如何用简单的算法生成一个类似『光盘』的彩色圆形图片?

<code class="language-python"><span class="c"># -*- coding:utf-8 -*-</span>
<span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">center</span> <span class="o">=</span> <span class="p">(</span><span class="mi">250</span><span class="p">,</span> <span class="mi">250</span><span class="p">)</span>
<span class="n">radius</span> <span class="o">=</span> <span class="mi">250</span>

<span class="n">img</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">((</span><span class="mi">500</span><span class="p">,</span><span class="mi">500</span><span class="p">))</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">500</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">500</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">center</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mf">1.0</span>
        <span class="n">y</span> <span class="o">=</span> <span class="n">j</span> <span class="o">-</span> <span class="n">center</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="mf">1.0</span>
        <span class="k">if</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">+</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o"> <span class="n">radius</span><span class="o">**</span><span class="mi">2</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">x</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">img</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">arctan</span><span class="p">(</span><span class="n">y</span><span class="o">/</span><span class="n">x</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">x</span><span class="o"><span class="mi">0</span> <span class="ow">and</span> <span class="n">y</span><span class="o">>=</span><span class="mi">0</span><span class="p">:</span>
                <span class="n">img</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">arctan</span><span class="p">(</span><span class="n">y</span><span class="o">/</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">pi</span>
            <span class="k">elif</span> <span class="n">x</span><span class="o"><span class="mi">0</span> <span class="ow">and</span> <span class="n">y</span><span class="o"><span class="mi">0</span><span class="p">:</span>
                <span class="n">img</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">arctan</span><span class="p">(</span><span class="n">y</span><span class="o">/</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">pi</span>
            <span class="k">elif</span> <span class="n">x</span><span class="o">==</span><span class="mi">0</span> <span class="ow">and</span> <span class="n">y</span><span class="o">></span><span class="mi">0</span><span class="p">:</span>
                <span class="n">img</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">pi</span> <span class="o">/</span> <span class="mi">2</span>
            <span class="k">elif</span> <span class="n">x</span><span class="o">==</span><span class="mi">0</span> <span class="ow">and</span> <span class="n">y</span><span class="o"><span class="mi">0</span><span class="p">:</span>
                <span class="n">img</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">pi</span> <span class="o">/</span> <span class="o">-</span><span class="mi">2</span>
            <span class="k">elif</span> <span class="n">x</span><span class="o">==</span><span class="mi">0</span> <span class="ow">and</span> <span class="n">y</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
                <span class="n">img</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span>

<span class="k">print</span> <span class="n">img</span>
<span class="n">imshow</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="n">cm</span><span class="o">.</span><span class="n">hsv</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</span></span></span></span></span></code>
登入後複製
改编自@冯昱尧的代码

<code class="language-python"><span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">n</span><span class="o">=</span><span class="mi">300</span>
<span class="n">img</span><span class="o">=</span><span class="p">[[</span><span class="n">arctan2</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">*</span><span class="n">y</span><span class="o"><span class="n">n</span><span class="o">*</span><span class="n">n</span> <span class="k">else</span> <span class="mi">0</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">-</span><span class="n">n</span><span class="p">,</span><span class="n">n</span><span class="p">)]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">-</span><span class="n">n</span><span class="p">,</span><span class="n">n</span><span class="p">)]</span>
<span class="n">imshow</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="n">cm</span><span class="o">.</span><span class="n">hsv</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</span></code>
登入後複製
如果根据这幅图,“光盘”的意思就是每一条半径的颜色在色相Hue上渐变。
关于色相是什么(反正这里说的不是可以用来牺牲的那个色相了),可以参看Wiki:HSL和HSV色彩空间,那个H就是色相Hue。
有点像下面这幅图的横截面(摘自Wiki)
如何用简单的算法生成一个类似『光盘』的彩色圆形图片?只不过明度Chroma没有变化。
这样的话,蛮简单的啊。对每个像素算出它所在半径的角度 0~2π 然后映射到Hue的值域上就是了。
具体实现的话,话说可以用OpenCV吗?反正一些图像库里面是有HSR到RGB的转换函数的。

(我一直都不明白所谓邀请是怎么回事,以及为什么大家都爱说谢邀) 补充一下Mathematica代码

<code class="language-text">n=300;
c=Hue[i/n];
Graphics@Table[{c,EdgeForm@c,Disk[{0,0},1,2Pi/n{i-1,i}]},{i,n}]
</code>
登入後複製
期待mma的代码,估计一条语句搞定
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板