目录
回复内容:
首页 后端开发 Python教程 如何用简单的算法生成一个类似『光盘』的彩色圆形图片?

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

Jun 06, 2016 pm 04:23 PM

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

回复内容:

补充一下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>
登录后复制
...被抢先了...

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

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

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

<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> <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><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><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><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><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 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><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>
登录后复制
如果根据这幅图,“光盘”的意思就是每一条半径的颜色在色相Hue上渐变。
关于色相是什么(反正这里说的不是可以用来牺牲的那个色相了),可以参看Wiki:HSL和HSV色彩空间,那个H就是色相Hue。
有点像下面这幅图的横截面(摘自Wiki)
如何用简单的算法生成一个类似『光盘』的彩色圆形图片?只不过明度Chroma没有变化。
这样的话,蛮简单的啊。对每个像素算出它所在半径的角度 0~2π 然后映射到Hue的值域上就是了。
具体实现的话,话说可以用OpenCV吗?反正一些图像库里面是有HSR到RGB的转换函数的。

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

n=300;
c=Hue[i/n];
Graphics@Table[{c,EdgeForm@c,Disk[{0,0},1,2Pi/n{i-1,i}]},{i,n}]
登录后复制
期待mma的代码,估计一条语句搞定
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

我如何使用美丽的汤来解析HTML? 我如何使用美丽的汤来解析HTML? Mar 10, 2025 pm 06:54 PM

本文解释了如何使用美丽的汤库来解析html。 它详细介绍了常见方法,例如find(),find_all(),select()和get_text(),以用于数据提取,处理不同的HTML结构和错误以及替代方案(SEL)

Python中的数学模块:统计 Python中的数学模块:统计 Mar 09, 2025 am 11:40 AM

Python的statistics模块提供强大的数据统计分析功能,帮助我们快速理解数据整体特征,例如生物统计学和商业分析等领域。无需逐个查看数据点,只需查看均值或方差等统计量,即可发现原始数据中可能被忽略的趋势和特征,并更轻松、有效地比较大型数据集。 本教程将介绍如何计算平均值和衡量数据集的离散程度。除非另有说明,本模块中的所有函数都支持使用mean()函数计算平均值,而非简单的求和平均。 也可使用浮点数。 import random import statistics from fracti

python对象的序列化和避难所化:第1部分 python对象的序列化和避难所化:第1部分 Mar 08, 2025 am 09:39 AM

Python 对象的序列化和反序列化是任何非平凡程序的关键方面。如果您将某些内容保存到 Python 文件中,如果您读取配置文件,或者如果您响应 HTTP 请求,您都会进行对象序列化和反序列化。 从某种意义上说,序列化和反序列化是世界上最无聊的事情。谁会在乎所有这些格式和协议?您想持久化或流式传输一些 Python 对象,并在以后完整地取回它们。 这是一种在概念层面上看待世界的好方法。但是,在实际层面上,您选择的序列化方案、格式或协议可能会决定程序运行的速度、安全性、维护状态的自由度以及与其他系

如何使用TensorFlow或Pytorch进行深度学习? 如何使用TensorFlow或Pytorch进行深度学习? Mar 10, 2025 pm 06:52 PM

本文比较了Tensorflow和Pytorch的深度学习。 它详细介绍了所涉及的步骤:数据准备,模型构建,培训,评估和部署。 框架之间的关键差异,特别是关于计算刻度的

哪些流行的Python库及其用途? 哪些流行的Python库及其用途? Mar 21, 2025 pm 06:46 PM

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

用美丽的汤在Python中刮擦网页:搜索和DOM修改 用美丽的汤在Python中刮擦网页:搜索和DOM修改 Mar 08, 2025 am 10:36 AM

该教程建立在先前对美丽汤的介绍基础上,重点是简单的树导航之外的DOM操纵。 我们将探索有效的搜索方法和技术,以修改HTML结构。 一种常见的DOM搜索方法是EX

如何使用Python创建命令行接口(CLI)? 如何使用Python创建命令行接口(CLI)? Mar 10, 2025 pm 06:48 PM

本文指导Python开发人员构建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等库详细介绍,强调输入/输出处理,并促进用户友好的设计模式,以提高CLI可用性。

解释Python中虚拟环境的目的。 解释Python中虚拟环境的目的。 Mar 19, 2025 pm 02:27 PM

文章讨论了虚拟环境在Python中的作用,重点是管理项目依赖性并避免冲突。它详细介绍了他们在改善项目管理和减少依赖问题方面的创建,激活和利益。

See all articles