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实现的HTTP并发测试完整示例
Apr 23 Python
Python数组定义方法
Apr 13 Python
python爬虫之xpath的基本使用详解
Apr 18 Python
Django 中自定义 Admin 样式与功能的实现方法
Jul 04 Python
python中时间转换datetime和pd.to_datetime详析
Aug 11 Python
Python 使用 PyMysql、DBUtils 创建连接池提升性能
Aug 14 Python
Django之路由层的实现
Sep 09 Python
python 装饰器功能与用法案例详解
Mar 06 Python
使用Django实现把两个模型类的数据聚合在一起
Mar 28 Python
Python中如何引入第三方模块
May 27 Python
Python二元算术运算常用方法解析
Sep 15 Python
Python 中Operator模块的使用
Jan 30 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简单对象与数组的转换函数代码(php多层数组和对象的转换)
2011/05/18 PHP
PHP连接MySQL查询结果中文显示乱码解决方法
2013/10/25 PHP
强制PHP命令行脚本单进程运行的方法
2014/04/15 PHP
PHP批量生成静态HTML的简单原理和方法
2014/04/20 PHP
php准确计算复活节日期的方法
2015/04/18 PHP
php实现scws中文分词搜索的方法
2015/12/25 PHP
CodeIgniter框架基本增删改查操作示例
2017/03/23 PHP
SyntaxHighlighter语法高亮插件使用说明
2011/08/14 Javascript
javascript的propertyIsEnumerable()方法使用介绍
2014/04/09 Javascript
window.returnValue使用方法示例介绍
2014/07/03 Javascript
js脚本实现数据去重
2014/11/27 Javascript
学习JavaScript设计模式(多态)
2015/11/25 Javascript
原生javascript实现的一个简单动画效果
2016/03/30 Javascript
JS 通过系统时间限定动态添加 select option的实例代码
2016/06/09 Javascript
js转html实体的方法
2016/09/27 Javascript
elementUI Vue 单个按钮显示和隐藏的变换功能(两种方法)
2018/09/04 Javascript
解决vue无法设置滚动位置的问题
2018/10/07 Javascript
详解js中的原型,原型对象,原型链
2020/07/16 Javascript
前端使用crypto.js进行加密的函数代码
2020/08/16 Javascript
vue中如何自定义右键菜单详解
2020/12/08 Vue.js
Python中使用异常处理来判断运行的操作系统平台方法
2015/01/22 Python
使用 Python 实现微信公众号粉丝迁移流程
2018/01/03 Python
Flask解决跨域的问题示例代码
2018/02/12 Python
浅谈python中requests模块导入的问题
2018/05/18 Python
python使用suds调用webservice接口的方法
2019/01/03 Python
解决Python 命令行执行脚本时,提示导入的包找不到的问题
2019/01/19 Python
详解10个可以快速用Python进行数据分析的小技巧
2019/06/24 Python
Python cookie的保存与读取、SSL讲解
2020/02/17 Python
美国著名的户外用品品牌:L.L.Bean
2018/01/05 全球购物
连卡佛中国官网:Lane Crawford中文站
2018/01/27 全球购物
Under Armour安德玛法国官网:美国高端运动科技品牌
2018/06/29 全球购物
Perfume’s Club中文官网:西班牙美妆在线零售品牌
2020/08/24 全球购物
党的群众路线教育实践活动心得体会范文
2014/11/05 职场文书
小学校长个人总结
2015/03/03 职场文书
校长一岗双责责任书
2015/05/09 职场文书
2015年领导干部廉洁自律工作总结
2015/05/26 职场文书