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练习程序批量修改文件名
Jan 16 Python
Python实现命令行通讯录实例教程
Aug 18 Python
Python正则抓取新闻标题和链接的方法示例
Apr 24 Python
多个应用共存的Django配置方法
May 30 Python
解决python读取几千万行的大表内存问题
Jun 26 Python
Python实现的简单排列组合算法示例
Jul 04 Python
python TKinter获取文本框内容的方法
Oct 11 Python
利用pyshp包给shapefile文件添加字段的实例
Dec 06 Python
Python实现转换图片背景颜色代码
Apr 30 Python
Python文件操作模拟用户登陆代码实例
Jun 09 Python
如何利用Python写个坦克大战
Nov 18 Python
pandas取dataframe特定行列的实现方法
May 24 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 url地址栏传中文乱码解决方法集合
2010/06/25 PHP
php IP转换整形(ip2long)的详解
2013/06/06 PHP
php上传文件中文文件名乱码的解决方法
2013/11/01 PHP
PHP 如何获取二维数组中某个key的集合
2014/06/03 PHP
Thinkphp将二维数组变为标签适用的一维数组方法总结
2014/10/30 PHP
Laravel框架中实现使用阿里云ACE缓存服务
2015/02/10 PHP
基于php实现的验证码小程序
2016/12/13 PHP
10款新鲜出炉的 jQuery 插件(Ajax 插件,有幻灯片、图片画廊、菜单等)
2011/06/08 Javascript
js实现鼠标划过给div加透明度的方法
2015/05/25 Javascript
javascript实现的多个层切换效果通用函数实例
2015/07/06 Javascript
Javascript 字符串模板的简单实现
2016/02/13 Javascript
js实现常见的工具条效果
2017/03/02 Javascript
JS如何设置元素样式的方法示例
2017/08/28 Javascript
jQuery实现动态添加节点与遍历节点功能示例
2017/11/09 jQuery
bootstrap 路径导航 分页 进度条的实例代码
2018/08/06 Javascript
vue文件运行的方法教学
2019/02/12 Javascript
Node.JS发送http请求批量检查文件中的网页地址、服务是否有效可用
2019/11/20 Javascript
vue中实现点击空白区域关闭弹窗的两种方法
2020/12/30 Vue.js
python编程实现归并排序
2017/04/14 Python
Python中 map()函数的用法详解
2018/07/10 Python
Flask之flask-script模块使用
2018/07/26 Python
使用Template格式化Python字符串的方法
2019/01/22 Python
tensorflow模型转ncnn的操作方式
2020/05/25 Python
Python DES加密实现原理及实例解析
2020/07/17 Python
CSS3中引入多种自定义字体font-face
2020/06/12 HTML / CSS
北大自主招生自荐信
2013/10/19 职场文书
三年级音乐教学反思
2014/01/28 职场文书
会议主持词
2014/03/17 职场文书
校园绿化美化方案
2014/06/08 职场文书
党支部特色活动方案
2014/08/20 职场文书
大学生党员学习焦裕禄精神思想汇报
2014/09/10 职场文书
机票销售员态度不好检讨书
2014/09/27 职场文书
村干部群众路线整改措施思想汇报
2014/10/12 职场文书
卖车协议书范文
2016/03/23 职场文书
2019入党申请书格式
2019/06/25 职场文书
PYTHON 使用 Pandas 删除某列指定值所在的行
2022/04/28 Python