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爬取网易云音乐上评论火爆的歌曲
Jan 19 Python
Python 转义字符详细介绍
Mar 21 Python
python 3利用BeautifulSoup抓取div标签的方法示例
May 28 Python
Python3中关于cookie的创建与保存
Oct 21 Python
用python打印1~20的整数实例讲解
Jul 01 Python
解决pycharm下os.system执行命令返回有中文乱码的问题
Jul 07 Python
pycharm 关掉syntax检查操作
Jun 09 Python
Matplotlib 折线图plot()所有用法详解
Jul 28 Python
django教程如何自学
Jul 31 Python
python对批量WAV音频进行等长分割的方法实现
Sep 25 Python
Anaconda的安装与虚拟环境建立
Nov 18 Python
python游戏开发Pygame框架
Apr 22 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之规范编程命名小结
2013/05/15 PHP
ppk谈JavaScript style属性
2008/10/10 Javascript
用Javascript评估用户输入密码的强度实现代码
2011/11/30 Javascript
基于SVG的web页面图形绘制API介绍及编程演示
2013/06/28 Javascript
JS数组array元素的添加和删除方法代码实例
2015/06/01 Javascript
JavaScript使用DeviceOne开发实战(四)仿优酷视频应用
2015/12/02 Javascript
JavaScript快速切换繁体中文和简体中文的方法及网站支持简繁体切换的绝招
2016/03/07 Javascript
Node.js项目中调用JavaScript的EJS模板库的方法
2016/03/11 Javascript
BootStrap table表格插件自适应固定表头(超好用)
2016/08/24 Javascript
js图片延迟加载(Lazyload)三种实现方式
2017/03/01 Javascript
jQuery插件zTree实现删除树节点的方法示例
2017/03/08 Javascript
ajax跨域访问遇到的问题及解决方案
2019/05/23 Javascript
layui--select使用以及下拉框实现键盘选择的例子
2019/09/24 Javascript
原生JS实现萤火虫效果
2020/03/07 Javascript
JavaScript ES6 Class类实现原理详解
2020/05/08 Javascript
[01:52]2014DOTA2西雅图邀请赛 V社开大会你不知道的小秘密
2014/07/08 DOTA
[00:34]DOTA2上海特级锦标赛 Spirit战队宣传片
2016/03/04 DOTA
[01:04:48]VGJ.S vs TNC Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
[01:00] DOTA2英雄背景故事第五期之重力引力法则谜团
2020/07/16 DOTA
Python标准库之Sys模块使用详解
2015/05/23 Python
Python使用文件锁实现进程间同步功能【基于fcntl模块】
2017/10/16 Python
Python实现从log日志中提取ip的方法【正则提取】
2018/03/31 Python
Python将json文件写入ES数据库的方法
2019/04/10 Python
Python实现图片识别加翻译功能
2019/12/26 Python
python从内存地址上加载python对象过程详解
2020/01/08 Python
为什么相对PHP黑python的更少
2020/06/21 Python
python类共享变量操作
2020/09/03 Python
Python经纬度坐标转换为距离及角度的实现
2020/11/01 Python
css3 transform过渡抖动问题解决
2020/10/23 HTML / CSS
意大利在线购买隐形眼镜网站:VisionDirect.it
2019/03/18 全球购物
黑猩猩商店:The Chimp Store
2020/02/12 全球购物
运动会广播稿20字
2014/02/18 职场文书
人力资源部岗位职责
2015/02/11 职场文书
2015建军节87周年演讲稿
2015/03/19 职场文书
幼儿园庆元旦主持词
2015/07/06 职场文书
Apache POI的基本使用详解
2021/11/07 Servers