python扫描线填充算法详解


Posted in Python onFebruary 19, 2020

本文实例为大家分享了python扫描线填充算法,供大家参考,具体内容如下

介绍

1.用水平扫描线从上到下扫描由点线段构成的多段构成的多边形。

2.每根扫描线与多边形各边产生一系列交点。将这些交点按照x坐标进行分类,将分类后的交点成对取出,作为两个端点,以所填的色彩画水平直线。

3.多边形被扫描完毕后,填色也就完成。

python扫描线填充算法详解

数据结构

活性边表:

python扫描线填充算法详解

新边表:

python扫描线填充算法详解

代码(使用数组)

import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
 
array = np.ndarray((660, 660, 3), np.uint8)
array[:, :, 0] = 255
array[:, :, 1] = 255
array[:, :, 2] = 255
 
for i in range(0,660,1):
 array[i,330]=(0,0,0)
for j in range(0,660,1):
 array[330,j]=(0,0,0)
 
 
def creat_Net(point, row, y_min,y_max ): 
 Net = [([ ] * y_max ) for i in range(y_max )] 
 point_count = point.shape[0]
 for j in range(0, point_count): 
 x = np.zeros(10) 
 first = int(min(point[(j+1)%point_count][1] , point[j][1])) 
 x[1] = 1/((point[(j+1)%point_count][1]-point[j][1])/(point[(j+1)%point_count][0]-point[j][0])) # x 的增量
 x[2] = max(point[(j+1)%point_count][1] , point[j][1]) 
 if(point[(j+1)%point_count][1] < point[j][1]):
  x[0] = point[(j+1)%point_count][0] 
 else:
  x[0] = point[j][0] 
 Net[first].append(x) 
 return Net

def draw_line(i,x ,y ):
 for j in range(int(x),int(y)+1):
 array[330-i,j+330]=(20,20,20)
 

def polygon_fill(point):
 y_min = np.min(point[:,1])
 y_max = np.max(point[:,1]) 
 Net = creat_Net(point, y_max - y_min + 1, y_min, y_max) 
 x_sort = [] * 3
 for i in range(y_min, y_max): 
 x = Net[i]
 if(len(x) != 0): 
  for k in x :
  x_sort.append(k) 
 x_image = [] * 3 
 for cell in x_sort:
  x_image.append(cell[0])
 x_image.sort()
 if(len(x_image) >= 3 and x_image[0]==x_image[1] and x_image[2]>x_image[1]):
  x_image[1] = x_image[2] 
 draw_line(i, x_image[0], x_image[1])
 
 linshi = [] * 3 
 for cell in x_sort:
  if cell[2] > i: 
  cell[0] += cell[1]
  linshi.append(cell)  
 x_sort = linshi[:] 

 x_image = [] * 3 
 for cell in x_sort:
 x_image.append(cell[0])
 x_image.sort()
 
 draw_line(i, x_image[0],x_image[1]) 

def main(): 
 point = [[55,40], [100,80], [100,160],[55,180], [10,160], [10,80]]
 point = np.array(point) 
 polygon_fill( point )
 image = Image.fromarray(array)
 image.save('saomao.jpg')
 image.show() 

if __name__ == "__main__":
 main()

实例:

python扫描线填充算法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python3 实现的人人影视网站自动签到
Jun 19 Python
Python实现拷贝多个文件到同一目录的方法
Sep 19 Python
深入理解NumPy简明教程---数组3(组合)
Dec 17 Python
Python数据结构与算法之字典树实现方法示例
Dec 13 Python
PyQt5每天必学之日历控件QCalendarWidget
Apr 19 Python
django自带的server 让外网主机访问方法
May 14 Python
使用pandas模块读取csv文件和excel表格,并用matplotlib画图的方法
Jun 22 Python
django基于cors解决跨域请求问题详解
Aug 06 Python
Python实现bilibili时间长度查询的示例代码
Jan 14 Python
python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
Mar 06 Python
Python自带的IDE在哪里
Jul 01 Python
pandas apply多线程实现代码
Aug 17 Python
Python关于__name__属性的含义和作用详解
Feb 19 #Python
opencv+python实现均值滤波
Feb 19 #Python
python手写均值滤波
Feb 19 #Python
pytorch实现CNN卷积神经网络
Feb 19 #Python
python+opencv3生成一个自定义纯色图教程
Feb 19 #Python
Python 实现Image和Ndarray互相转换
Feb 19 #Python
python3+opencv生成不规则黑白mask实例
Feb 19 #Python
You might like
PHP脚本的10个技巧(7)
2006/10/09 PHP
PHP 5.0 Pear安装方法
2006/12/06 PHP
PHP字符串处理的10个简单方法
2010/06/30 PHP
PHP变量赋值、代入给JavaScript中的变量
2015/06/29 PHP
Symfony2安装的方法(2种方法)
2016/02/04 PHP
PHP错误处理函数
2016/04/03 PHP
PHP简单留言本功能实现代码
2017/06/09 PHP
老生常谈php中传统验证与thinkphp框架(必看篇)
2017/06/10 PHP
JS字符串截取函数实例
2013/12/27 Javascript
node.js中的buffer.Buffer.isEncoding方法使用说明
2014/12/14 Javascript
js基本算法:冒泡排序,二分查找的简单实例
2016/10/08 Javascript
JavaScript DOM节点操作实例小结(新建,删除HTML元素)
2017/01/19 Javascript
angular仿支付宝密码框输入效果
2017/03/25 Javascript
mint-ui的search组件在键盘显示搜索按钮的实现方法
2017/10/27 Javascript
详解vue项目的构建,打包,发布全过程
2017/11/23 Javascript
windows系统下更新nodejs版本的方案
2017/11/24 NodeJs
详解在vue-test-utils中mock全局对象
2018/11/07 Javascript
微信小程序常见页面跳转操作简单示例
2019/05/01 Javascript
JavaScript实现好看的跟随彩色气泡效果
2020/02/06 Javascript
如何使用Jquery动态生成二级选项列表
2020/02/06 jQuery
Vue如何实现验证码输入交互
2020/12/07 Vue.js
[01:21:36]CHAOS vs Alliacne 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
Python编写电话薄实现增删改查功能
2016/05/07 Python
django框架forms组件用法实例详解
2019/12/10 Python
Python判断变量是否是None写法代码实例
2020/10/09 Python
详解Django中异步任务之django-celery
2020/11/05 Python
CSS3中的opacity属性使用教程
2015/08/19 HTML / CSS
html5 input输入实时检测以及延时优化
2018/07/18 HTML / CSS
社区党务公开实施方案
2014/03/18 职场文书
学生自我评语大全
2014/04/18 职场文书
超市开业庆典策划方案
2014/05/14 职场文书
信仰心得体会
2014/09/05 职场文书
孔庙导游词
2015/02/04 职场文书
学校教学管理制度
2015/08/06 职场文书
教师年度考核自我评鉴
2015/08/11 职场文书
2015年度学校应急管理工作总结
2015/10/22 职场文书