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类定义的讲解
Nov 01 Python
Python实现提取谷歌音乐搜索结果的方法
Jul 10 Python
python开发之list操作实例分析
Feb 22 Python
深入解析Python中的线程同步方法
Jun 14 Python
python3中dict(字典)的使用方法示例
Mar 22 Python
python实现决策树
Dec 21 Python
pandas数值计算与排序方法
Apr 12 Python
pytorch索引查找 index_select的例子
Aug 18 Python
python实现异常信息堆栈输出到日志文件
Dec 26 Python
python读取ini配置的类封装代码实例
Jan 08 Python
如何通过命令行进入python
Jul 06 Python
python安装mysql的依赖包mysql-python操作
Jan 01 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页面运行时间的函数介绍
2013/07/01 PHP
PHP面向对象程序设计之类常量用法实例
2014/08/20 PHP
php 数据结构之链表队列
2017/10/17 PHP
PHP区块查询实现方法分析
2018/05/12 PHP
strpos() 函数判断字符串中是否包含某字符串的方法
2019/01/16 PHP
PHP调用全国天气预报数据接口查询天气示例
2019/02/20 PHP
HTML node相关的一些资料整理
2010/01/01 Javascript
为JavaScript类型增加方法的实现代码(增加功能)
2011/12/29 Javascript
js兼容的placeholder属性详解
2013/08/18 Javascript
js模拟C#中List的简单实例
2014/03/06 Javascript
JavaScript使用FileSystemObject对象写入文本文件内容的方法
2015/08/05 Javascript
利用Bootstrap实现表格复选框checkbox全选
2016/12/21 Javascript
浅谈jQuery中的$.extend方法来扩展JSON对象
2017/02/12 Javascript
Node.js数据库操作之连接MySQL数据库(一)
2017/03/04 Javascript
利用JS如何计算字符串所占字节数示例代码
2017/09/13 Javascript
使用angularjs.foreach时return的问题解决
2018/09/30 Javascript
echarts统计x轴区间的数值实例代码详解
2019/07/07 Javascript
vue-froala-wysiwyg 富文本编辑器功能
2019/09/19 Javascript
Vue 实现CLI 3.0 + momentjs + lodash打包时优化
2019/11/13 Javascript
在Python中操作时间之mktime()方法的使用教程
2015/05/22 Python
Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)
2016/09/21 Python
详解python中的Turtle函数库
2018/11/19 Python
Python3实现统计单词表中每个字母出现频率的方法示例
2019/01/28 Python
Python datetime和unix时间戳之间相互转换的讲解
2019/04/01 Python
python使用Plotly绘图工具绘制气泡图
2019/04/01 Python
详解Python传入参数的几种方法
2019/05/16 Python
浅谈keras通过model.fit_generator训练模型(节省内存)
2020/06/17 Python
Python学习之路之pycharm的第一个项目搭建过程
2020/06/18 Python
美国女性卫生用品公司:Thinx
2017/06/30 全球购物
教师绩效工资方案
2014/02/01 职场文书
财务部副经理岗位职责
2014/03/14 职场文书
制冷与空调专业毕业生推荐信
2014/07/07 职场文书
行政撤诉申请书
2015/05/18 职场文书
结婚典礼致辞
2015/07/28 职场文书
java解析XML详解
2021/07/09 Java/Android
win11怎么消除图标小盾牌?win11消除图标小盾牌解决方法
2022/08/05 数码科技