python 使用递归的方式实现语义图片分割功能


Posted in Python onJuly 16, 2020

实现效果

python 使用递归的方式实现语义图片分割功能

第一张图为原图,其余的图为分割后的图形

代码实现:

# -*-coding:utf-8-*-
import numpy as np
import cv2

#----------------------------------------------------------------------
def obj_clip(img, foreground, border):
  result = []
  height ,width = np.shape(img)
  visited = set()
  for h in range(height):
    for w in range(width):
      if img[h,w] == foreground and not (h,w) in visited:
        obj = visit(img, height, width, h, w, visited, foreground, border)
        result.append(obj)
  return result
#----------------------------------------------------------------------
def visit(img, height, width, h, w, visited, foreground, border):
  visited.add((h,w))
  result = [(h,w)]
  if w > 0 and not (h, w-1) in visited:
    if img[h, w-1] == foreground: 
      result += visit(img, height, width, h, w-1, visited , foreground, border)
    elif border is not None and img[h, w-1] == border:
      result.append((h, w-1))
  if w < width-1 and not (h, w+1) in visited:
    if img[h, w+1] == foreground:
      result += visit(img, height, width, h, w+1, visited, foreground, border)
    elif border is not None and img[h, w+1] == border:
      result.append((h, w+1))
  if h > 0 and not (h-1, w) in visited:
    if img[h-1, w] == foreground:
      result += visit(img, height, width, h-1, w, visited, foreground, border)
    elif border is not None and img[h-1, w] == border:
      result.append((h-1, w))
  if h < height-1 and not (h+1, w) in visited:
    if img[h+1, w] == foreground :
      result += visit(img, height, width, h+1, w, visited, foreground, border) 
    elif border is not None and img[h+1, w] == border:
      result.append((h+1, w))
  return result
#----------------------------------------------------------------------
if __name__ == "__main__":
  import cv2
  import sys
  sys.setrecursionlimit(100000)
  img = np.zeros([400,400])
  cv2.rectangle(img, (10,10), (150,150), 1.0, 5)
  cv2.circle(img, (270,270), 70, 1.0, 5)
  cv2.line(img, (100,10), (100,150), 0.5, 5)
  #cv2.putText(img, "Martin",(200,200), 1.0, 5)
  cv2.imshow("img", img*255)
  cv2.waitKey(0)
  for obj in obj_clip(img, 1.0, 0.5):
    clip = np.zeros([400, 400])
    for h, w in obj:
      clip[h, w] = 0.2
    cv2.imshow("aa", clip*255)
    cv2.waitKey(0)

总结

到此这篇关于python 使用递归的方式实现语义图片分割的文章就介绍到这了,更多相关python 语义图片分割内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python分析apache访问日志脚本分享
Feb 26 Python
Python and、or以及and-or语法总结
Apr 14 Python
利用Python中的mock库对Python代码进行模拟测试
Apr 16 Python
Python中方法链的使用方法
Feb 23 Python
python 回调函数和回调方法的实现分析
Mar 23 Python
Python中turtle作图示例
Nov 15 Python
Python3 Random模块代码详解
Dec 04 Python
Python微医挂号网医生数据抓取
Jan 24 Python
pytorch::Dataloader中的迭代器和生成器应用详解
Jan 03 Python
如何通过Python3和ssl实现加密通信功能
May 09 Python
Python3.9 beta2版本发布了,看看这7个新的PEP都是什么
Jun 10 Python
Python提取PDF指定内容并生成新文件
Jun 09 Python
Django serializer优化类视图的实现示例
Jul 16 #Python
python中plt.imshow与cv2.imshow显示颜色问题
Jul 16 #Python
Python实现GIF图倒放
Jul 16 #Python
浅谈python处理json和redis hash的坑
Jul 16 #Python
Python requests及aiohttp速度对比代码实例
Jul 16 #Python
Python3 搭建Qt5 环境的方法示例
Jul 16 #Python
python3实现将json对象存入Redis以及数据的导入导出
Jul 16 #Python
You might like
PHP自动更新新闻DIY
2006/10/09 PHP
php 操作excel文件的方法小结
2009/12/31 PHP
php下用cookie统计用户访问网页次数的代码
2010/05/09 PHP
Wordpress 相册插件 NextGEN-Gallery 添加目录将中文转为拼音的解决办法
2010/12/29 PHP
PHP gbk环境下json_dencode传送来的汉字
2012/11/13 PHP
apache php模块整合操作指南
2012/11/16 PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
2015/04/07 PHP
PDO::query讲解
2019/01/29 PHP
用js实现计算加载页面所用的时间
2010/04/02 Javascript
JS倒计时代码汇总
2014/11/25 Javascript
基于javascript实现listbox左右移动
2016/01/29 Javascript
简单实现js点击展开二级菜单功能
2017/05/16 Javascript
Vue Cli与BootStrap结合实现表格分页功能
2017/08/18 Javascript
基于JavaScript实现评论框展开和隐藏功能
2017/08/25 Javascript
JS Testing Properties 判断属性是否在对象里的方法
2017/10/01 Javascript
webpack中使用iconfont字体图标的方法
2018/02/22 Javascript
使用Vue中 v-for循环列表控制按钮隐藏显示功能
2019/04/23 Javascript
vue路由传参页面刷新参数丢失问题解决方案
2019/10/08 Javascript
js面向对象之实现淘宝放大镜
2020/01/15 Javascript
微信小程序仿淘宝热搜词在搜索框中轮播功能
2020/01/21 Javascript
小程序实现录音功能
2020/09/22 Javascript
vue+springboot+element+vue-resource实现文件上传教程
2020/10/21 Javascript
Python实现爬取知乎神回复简单爬虫代码分享
2015/01/04 Python
Python中pow()和math.pow()函数用法示例
2018/02/11 Python
python用pandas数据加载、存储与文件格式的实例
2018/12/07 Python
[机器视觉]使用python自动识别验证码详解
2019/05/16 Python
python求加权平均值的实例(附纯python写法)
2019/08/22 Python
python conda操作方法
2019/09/11 Python
Python基于类路径字符串获取静态属性
2020/03/12 Python
从python读取sql的实例方法
2020/07/21 Python
利用Python实现学生信息管理系统的完整实例
2020/12/30 Python
selenium携带cookies模拟登陆CSDN的实现
2021/01/19 Python
银行存款证明样本
2014/01/17 职场文书
十周年庆典策划方案
2014/06/03 职场文书
确保减税降费落地生根,用实实在在措施
2019/07/19 职场文书
Win11快速关闭所有广告推荐
2022/04/19 数码科技