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 相关文章推荐
Python多线程同步Lock、RLock、Semaphore、Event实例
Nov 21 Python
Python回调函数用法实例详解
Jul 02 Python
简单解决Python文件中文编码问题
Nov 22 Python
详解Python中的变量及其命名和打印
Mar 11 Python
详解Python中heapq模块的用法
Jun 28 Python
python实现单线程多任务非阻塞TCP服务端
Jun 13 Python
python保存文件方法小结
Jul 27 Python
python中的协程深入理解
Jun 10 Python
python开启debug模式的方法
Jun 27 Python
Windows上安装tensorflow  详细教程(图文详解)
Feb 04 Python
jupyter notebook 的工作空间设置操作
Apr 20 Python
Python在字符串中处理html和xml的方法
Jul 31 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
如何提高MYSQL数据库的查询统计速度 select 索引应用
2007/04/11 PHP
PHP最常用的ini函数分析 针对PHP.ini配置文件
2010/04/22 PHP
使用laravel和ajax实现整个页面无刷新的操作方法
2019/10/03 PHP
Laravel框架实现抢红包功能示例
2019/10/31 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
2019/12/25 PHP
js或css实现滚动广告的几种方案
2010/01/28 Javascript
JavaScript计算两个日期时间段内日期的方法
2015/03/16 Javascript
JavaScript列表框listbox全选和反选的实现方法
2015/03/18 Javascript
js+HTML5实现canvas多种颜色渐变效果的方法
2015/06/05 Javascript
在jQuery中使用$而避免跟其它库产生冲突的方法
2015/08/13 Javascript
解决js函数闭包内存泄露问题的办法
2016/01/25 Javascript
jquery实现文字单行横移或翻转(上下、左右跳转)
2017/01/08 Javascript
微信小程序 合法域名校验出错详解及解决办法
2017/03/09 Javascript
用vue和node写的简易购物车实现
2017/04/25 Javascript
vue.js 实现点击展开收起动画效果
2018/07/07 Javascript
Vue开发之watch监听数组、对象、变量操作分析
2019/04/25 Javascript
[01:18:36]LGD vs VP Supermajor 败者组决赛 BO3 第一场 6.10
2018/07/04 DOTA
举例详解Python中threading模块的几个常用方法
2015/06/18 Python
Django中处理出错页面的方法
2015/07/15 Python
15行Python代码实现网易云热门歌单实例教程
2019/03/10 Python
详解Python_shutil模块
2019/03/15 Python
python函数参数(必须参数、可变参数、关键字参数)
2019/08/16 Python
Python多进程编程multiprocessing代码实例
2020/03/12 Python
一篇文章带你学习CSS3图片边框
2020/11/04 HTML / CSS
canvas实现二维码和图片合成的示例代码
2018/08/01 HTML / CSS
大女孩胸罩:Big Girls Bras
2016/12/15 全球购物
阿联酋手表和配饰购物网站:Rivolishop
2019/11/25 全球购物
2014年应届大学生自我评价
2014/01/09 职场文书
一夜的工作教学反思
2014/02/08 职场文书
大学第二课堂活动总结
2014/07/08 职场文书
办公室主任岗位职责
2015/01/31 职场文书
教师个人学习总结
2015/02/11 职场文书
校车司机安全责任书
2015/05/11 职场文书
举起手来观后感
2015/06/09 职场文书
一封真诚的自荐信帮你赢得机会
2019/05/07 职场文书
使用 JavaScript 制作页面效果
2021/04/21 Javascript