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之str操作方法(详解)
Jun 19 Python
详解python多线程、锁、event事件机制的简单使用
Apr 27 Python
python 编码规范整理
May 05 Python
python对离散变量的one-hot编码方法
Jul 11 Python
Python设计模式之外观模式实例详解
Jan 17 Python
windows下安装Python虚拟环境virtualenvwrapper-win
Jun 14 Python
Python完成哈夫曼树编码过程及原理详解
Jul 29 Python
使用pandas的box_plot去除异常值
Dec 10 Python
浅谈在django中使用filter()(即对QuerySet操作)时踩的坑
Mar 31 Python
Python如何定义接口和抽象类
Jul 28 Python
Python GUI库Tkiner使用方法代码示例
Nov 27 Python
教你如何用Python实现人脸识别(含源代码)
Jun 23 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
世界收音机发展史
2021/03/01 无线电
PHP中for与foreach的区别分析
2011/03/09 PHP
基于php权限分配的实现代码
2013/04/28 PHP
smarty内部日期函数html_select_date()用法实例分析
2015/07/08 PHP
PHP实现事件机制的方法
2015/07/10 PHP
PHP结合Ueditor并修改图片上传路径
2016/10/16 PHP
PHP输出图像imagegif、imagejpeg与imagepng函数用法分析
2016/11/14 PHP
for 循环性能比较 提高for循环的效率
2009/03/19 Javascript
jQuery.ajax 用户登录验证代码
2010/10/29 Javascript
jquery Moblie入门—hello world的示例代码学习
2013/01/08 Javascript
用jquery的方法制作一个简单的导航栏
2014/06/23 Javascript
JQuery仿小米手机抢购页面倒计时效果
2014/12/16 Javascript
10个JavaScript中易犯小错误
2016/02/14 Javascript
Bootstrap按钮组件详解
2016/04/26 Javascript
基于Javascript倒计时效果
2016/12/22 Javascript
Mac系统下Webstorm快捷键整理大全
2017/05/28 Javascript
Vue前后端不同端口的实现方法
2018/09/19 Javascript
JavaScript根据json生成html表格的示例代码
2018/10/24 Javascript
vue模块拖拽实现示例代码
2019/03/09 Javascript
解决vue自定义指令导致的内存泄漏问题
2020/08/04 Javascript
简明 Python 基础学习教程
2007/02/08 Python
Python实现简单拆分PDF文件的方法
2015/07/30 Python
Python 获得13位unix时间戳的方法
2017/10/20 Python
django 解决manage.py migrate无效的问题
2018/05/27 Python
python中的插值 scipy-interp的实现代码
2018/07/23 Python
深入浅析Python中list的复制及深拷贝与浅拷贝
2018/09/03 Python
为什么你还不懂得怎么使用Python协程
2019/05/13 Python
Python实现一个数组除以一个数的例子
2019/07/20 Python
Python学习笔记之For循环用法详解
2019/08/14 Python
html5之Canvas路径绘图、坐标变换应用实例
2012/12/26 HTML / CSS
英国著名的药妆网站:Escentual
2016/07/29 全球购物
万得城电器土耳其网站:欧洲第一大电子产品零售商
2016/10/07 全球购物
护理专业毕业生推荐信
2013/10/31 职场文书
高一化学教学反思
2014/02/05 职场文书
餐厅经理岗位职责和岗位目标
2014/02/13 职场文书
2014小学教师个人工作总结
2014/11/10 职场文书