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实现提取百度搜索结果的方法
May 19 Python
Python基于pygame实现的弹力球效果(附源码)
Nov 11 Python
python实现推箱子游戏
Mar 25 Python
python点击鼠标获取坐标(Graphics)
Aug 10 Python
python读取Excel表格文件的方法
Sep 02 Python
Django将默认的SQLite更换为MySQL的实现
Nov 18 Python
10个python3常用排序算法详细说明与实例(快速排序,冒泡排序,桶排序,基数排序,堆排序,希尔排序,归并排序,计数排序)
Mar 17 Python
Python绘制动态水球图过程详解
Jun 03 Python
浅谈matplotlib中FigureCanvasXAgg的用法
Jun 16 Python
python 用递归实现通用爬虫解析器
Apr 16 Python
Python自动化之批量处理工作簿和工作表
Jun 03 Python
python的列表生成式,生成器和generator对象你了解吗
Mar 16 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 Smarty模板生成html文档的方法
2010/04/12 PHP
WampServer下安装多个版本的PHP、mysql、apache图文教程
2015/01/07 PHP
php无限级分类实现方法分析
2016/10/19 PHP
ThinkPHP3.2框架操作Redis的方法分析
2019/05/05 PHP
javascript当中的代码嗅探扩展原生对象和原型(prototype)
2013/01/11 Javascript
jquery清空textarea等输入框实现代码
2013/04/22 Javascript
$.get获取一个文件的内容示例代码
2013/09/11 Javascript
jsp网页搜索结果中实现选中一行使其高亮
2014/02/17 Javascript
javascript数组去重方法终极总结
2014/06/05 Javascript
Textarea输入字数限制实例(兼容iOS&amp;安卓)
2017/07/06 Javascript
javascript中神奇的 Date对象小结
2017/10/12 Javascript
html中通过JS获取JSON数据并加载的方法
2017/11/30 Javascript
JS实现鼠标拖拽盒子移动及右键点击盒子消失效果示例
2019/01/29 Javascript
json数据格式常见操作示例
2019/06/13 Javascript
[00:50]深扒TI7聊天轮盘语音出处6
2017/05/11 DOTA
Python使用CMD模块更优雅的运行脚本
2015/05/11 Python
详解Python中dict与set的使用
2015/08/10 Python
Python中装饰器兼容加括号和不加括号的写法详解
2017/07/05 Python
Python给你的头像加上圣诞帽
2018/01/04 Python
python线程池threadpool实现篇
2018/04/27 Python
python切片及sys.argv[]用法详解
2018/05/25 Python
influx+grafana自定义python采集数据和一些坑的总结
2018/09/17 Python
python获取时间及时间格式转换问题实例代码详解
2018/12/06 Python
Python基于gevent实现文件字符串查找器
2020/08/11 Python
世界上最大的二手相机店:KEN
2017/05/17 全球购物
英国排名第一的餐具品牌:Denby Pottery
2019/11/01 全球购物
英国著名的美容护肤和护发产品购物网站:Lookfantastic
2020/11/23 全球购物
物业管理个人自我评价
2013/11/08 职场文书
力学专业毕业生自荐信
2013/11/17 职场文书
全国法院系统开展党的群众路线教育实践活动综述(全文)
2014/10/25 职场文书
工会文体活动总结
2015/05/07 职场文书
教师节获奖感言
2015/07/31 职场文书
学生会宣传部竞选稿
2015/11/21 职场文书
毕业生的自我鉴定表范文
2019/05/16 职场文书
党员学习型组织心得体会
2019/06/21 职场文书
Python中递归以及递归遍历目录详解
2021/10/24 Python