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 Selenium爬取内容并存储至MySQL数据库的实现代码
Mar 16 Python
python实现PID算法及测试的例子
Aug 08 Python
Python变量、数据类型、数据类型转换相关函数用法实例详解
Jan 09 Python
基于pytorch padding=SAME的解决方式
Feb 18 Python
python的sys.path模块路径添加方式
Mar 09 Python
解决python父线程关闭后子线程不关闭问题
Apr 25 Python
使用Pycharm在运行过程中,查看每个变量的操作(show variables)
Jun 08 Python
Python使用itcaht库实现微信自动收发消息功能
Jul 13 Python
Python 如何测试文件是否存在
Jul 31 Python
用python绘制樱花树
Oct 09 Python
记一次Django响应超慢的解决过程
Sep 17 Python
python list的index()和find()的实现
Nov 16 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引用(&amp;)各种使用方法实例详解
2014/03/20 PHP
php实现建立多层级目录的方法
2014/07/19 PHP
PHP的PDO操作简单示例
2016/03/30 PHP
Yii2 输出xml格式数据的方法
2016/05/03 PHP
在你的网页中嵌入外部网页的方法
2007/04/02 Javascript
js 函数的执行环境和作用域链的深入解析
2009/11/01 Javascript
基于jquery的文章中所有图片width大小批量设置方法
2013/08/01 Javascript
使用js获取图片原始尺寸
2014/12/03 Javascript
JavaScript中的数组操作介绍
2014/12/30 Javascript
详解JavaScript ES6中的Generator
2015/07/28 Javascript
js鼠标点击按钮切换图片-图片自动切换-点击左右按钮切换特效代码
2015/09/02 Javascript
jQuery动态加载css文件实现方法
2016/06/15 Javascript
用js将long型数据转换成date型或datetime型的实例
2017/07/03 Javascript
layui点击左侧导航栏,实现不刷新整个页面,只刷新局部的方法
2019/09/25 Javascript
vue 实现v-for循环回来的数据动态绑定id
2019/11/07 Javascript
vue中如何自定义右键菜单详解
2020/12/08 Vue.js
Python实现二分法算法实例
2015/02/02 Python
Python实现迭代时使用索引的方法示例
2018/06/05 Python
python基于http下载视频或音频
2018/06/20 Python
Python OpenCV处理图像之图像像素点操作
2018/07/10 Python
Python 利用scrapy爬虫通过短短50行代码下载整站短视频
2018/10/29 Python
python3使用QQ邮箱发送邮件
2020/05/20 Python
Python实现DDos攻击实例详解
2019/02/02 Python
Python 多个图同时在不同窗口显示的实现方法
2019/07/07 Python
python3.6+django2.0+mysql搭建网站过程详解
2019/07/24 Python
Python中关于logging模块的学习笔记
2020/06/03 Python
python3定位并识别图片验证码实现自动登录功能
2021/01/29 Python
Python中的流程控制详解
2021/02/18 Python
CSS3 box-shadow属性实例详解
2020/06/19 HTML / CSS
Under Armour安德玛意大利官网:美国高端运动科技品牌
2020/01/16 全球购物
香港士多网上超级市场:Ztore
2021/01/09 全球购物
关于幼儿的自我评价
2013/12/18 职场文书
运动会广播稿30字
2014/01/21 职场文书
护理不良事件检讨书
2014/02/06 职场文书
公司拓展活动方案
2014/02/13 职场文书
大学生社会实践评语
2014/04/25 职场文书