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中用于计算指数的exp()方法
May 14 Python
python获取目录下所有文件的方法
Jun 01 Python
详解Python实现多进程异步事件驱动引擎
Aug 25 Python
pandas 使用均值填充缺失值列的小技巧分享
Jul 04 Python
django 消息框架 message使用详解
Jul 22 Python
python爬虫selenium和phantomJs使用方法解析
Aug 08 Python
学习Django知识点分享
Sep 11 Python
win10从零安装配置pytorch全过程图文详解
May 08 Python
Python join()函数原理及使用方法
Nov 14 Python
python 实现图片裁剪小工具
Feb 02 Python
python内置模块之上下文管理contextlib
Jun 14 Python
python中validators库的使用方法详解
Sep 23 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
html中select语句读取mysql表中内容
2006/10/09 PHP
解析php框架codeigniter中如何使用框架的session
2013/06/24 PHP
功能强大的PHP POST提交数据类
2016/07/15 PHP
PHP实现二维数组按照指定的字段进行排序算法示例
2019/04/23 PHP
在JavaScript中通过URL传递汉字的方法
2007/04/09 Javascript
javascript语句中的CDATA标签的意义
2007/05/09 Javascript
几个有趣的Javascript Hack
2010/07/24 Javascript
浅谈javascript 归并方法
2015/01/21 Javascript
javascript中callee与caller的区别分析
2015/04/20 Javascript
jQuery超酷平面式时钟效果代码分享
2020/03/30 Javascript
深入理解js数组的sort排序
2016/05/28 Javascript
浅谈JavaScript for循环 闭包
2016/06/22 Javascript
JavaScript直播评论发弹幕切图功能点集合效果代码
2016/06/26 Javascript
Bootstrap框架安装使用详解
2017/01/21 Javascript
AngularJS获取json数据的方法详解
2017/05/27 Javascript
jquery使用iscorll实现上拉、下拉加载刷新
2017/10/26 jQuery
angular写一个列表的选择全选交互组件的示例
2018/01/22 Javascript
详解基于DllPlugin和DllReferencePlugin的webpack构建优化
2018/06/28 Javascript
详解ECMAScript typeof用法
2018/07/25 Javascript
layui表单提交到后台自动封装到实体类的方法
2019/09/12 Javascript
JavaScript前端开发时数值运算的小技巧
2020/07/28 Javascript
微信小程序实现简单的select下拉框
2020/11/23 Javascript
[56:12]LGD vs Optic Supermajor小组赛D组胜者组决赛 BO3 第一场 6.3
2018/06/04 DOTA
python自动安装pip
2014/04/24 Python
python3.6根据m3u8下载mp4视频
2019/06/17 Python
Python如何存储数据到json文件
2020/03/09 Python
python产生模拟数据faker库的使用详解
2020/11/04 Python
html5 canvas移动浏览器上实现图片压缩上传
2016/03/11 HTML / CSS
基于canvas使用贝塞尔曲线平滑拟合折线段的方法
2018/01/10 HTML / CSS
介绍一下MYSQL常用的优化技巧
2012/10/25 面试题
简历中的自我评价怎么写
2014/01/29 职场文书
维稳工作情况汇报
2014/10/27 职场文书
2015年质量月活动总结报告
2015/03/27 职场文书
开学典礼观后感
2015/06/15 职场文书
毕业感言怎么写
2015/07/31 职场文书
2015年库房管理工作总结
2015/10/14 职场文书