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 IDLE加上自动补全和历史功能
Nov 30 Python
Python多线程编程(三):threading.Thread类的重要函数和方法
Apr 05 Python
在Python中编写数据库模块的教程
Apr 29 Python
使用Django的模版来配合字符串翻译工作
Jul 27 Python
python实现Adapter模式实例代码
Feb 09 Python
Python3.5多进程原理与用法实例分析
Apr 05 Python
python使用threading.Condition交替打印两个字符
May 07 Python
win10环境下配置vscode python开发环境的教程详解
Oct 16 Python
Python代码生成视频的缩略图的实例讲解
Dec 22 Python
Python 元组拆包示例(Tuple Unpacking)
Dec 24 Python
python3爬取torrent种子链接实例
Jan 16 Python
使用Python绘制台风轨迹图的示例代码
Sep 21 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中strlen和mb_strlen用法实例分析
2016/11/12 PHP
JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
2007/08/13 Javascript
window.dialogArguments 使用说明
2011/04/11 Javascript
关于js内存泄露的一个好例子
2013/12/09 Javascript
js插件方式打开pdf文件(浏览器pdf插件分享)
2013/12/20 Javascript
javascript 实现子父窗体互相传值的简单实例
2014/02/17 Javascript
jquery实现很酷的网页顶部图标下拉菜单效果
2015/08/22 Javascript
javascript绘制漂亮的心型线效果完整实例
2016/02/02 Javascript
分享12个非常实用的JavaScript小技巧
2016/05/11 Javascript
Bootstrap轮播插件简单使用方法介绍
2016/06/21 Javascript
jQuery EasyUI编辑DataGrid用combobox实现多级联动
2016/08/29 Javascript
javascript 动态样式添加的简单实现
2016/10/11 Javascript
移动端触摸滑动插件swiper使用方法详解
2017/08/11 Javascript
VueJS 集成 Medium Editor的示例代码 (自定义编辑器按钮)
2017/08/24 Javascript
利用JQUERY实现多个AJAX请求等待的实例
2017/12/14 jQuery
Swiper自定义分页器使用详解
2017/12/28 Javascript
vue cli构建的项目中请求代理与项目打包问题
2018/02/26 Javascript
Vue表单输入绑定的示例代码
2018/11/01 Javascript
javascript写一个ajax自动拦截并下载数据代码实例
2019/09/07 Javascript
Vue列表如何实现滚动到指定位置样式改变效果
2020/05/09 Javascript
[45:25]完美世界DOTA2联赛循环赛 PXG vs IO 第一场 11.06
2020/11/09 DOTA
Django 如何获取前端发送的头文件详解(推荐)
2017/08/15 Python
python自动重试第三方包retrying模块的方法
2018/04/24 Python
用python处理图片实现图像中的像素访问
2018/05/04 Python
利用Python进行数据可视化常见的9种方法!超实用!
2018/07/11 Python
对python 匹配字符串开头和结尾的方法详解
2018/10/27 Python
对Python生成器、装饰器、递归的使用详解
2019/07/19 Python
马来西亚时装购物网站:ZALORA马来西亚
2017/03/14 全球购物
L’AGENCE官网:加州女装品牌
2018/06/03 全球购物
理想演讲稿范文
2014/05/21 职场文书
个人自荐材料
2014/05/23 职场文书
三方合作意向书范本
2015/05/09 职场文书
长征观后感
2015/06/09 职场文书
让人瞬间清醒的句子,句句经典,字字如金
2019/07/08 职场文书
Python数据类型最全知识总结
2021/05/31 Python
css3中transform属性实现的4种功能
2021/08/07 HTML / CSS