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 04 Python
Python实现批量检测HTTP服务的状态
Oct 27 Python
简单了解Django模板的使用
Dec 20 Python
python查询mysql,返回json的实例
Mar 26 Python
Python使用itertools模块实现排列组合功能示例
Jul 02 Python
Python高阶函数、常用内置函数用法实例分析
Dec 26 Python
Python实现仿射密码的思路详解
Apr 23 Python
浅谈pytorch 模型 .pt, .pth, .pkl的区别及模型保存方式
May 25 Python
聊聊python中的异常嵌套
Sep 01 Python
python时间time模块处理大全
Oct 25 Python
Python OpenCV 图像平移的实现示例
Jun 04 Python
Python turtle编写简单的球类小游戏
Mar 31 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
用Apache反向代理设置对外的WWW和文件服务器
2006/10/09 PHP
Yii使用技巧大汇总
2015/12/29 PHP
PHP入门教程之面向对象的特性分析(继承,多态,接口,抽象类,抽象方法等)
2016/09/11 PHP
Javascript连接多个数组不用concat来解决
2014/03/24 Javascript
使用jQuery动态加载js脚本文件的方法
2014/04/03 Javascript
node.js中的fs.fsync方法使用说明
2014/12/15 Javascript
jQuery中nextAll()方法用法实例
2015/01/07 Javascript
React组件之间的通信的实例代码
2017/06/27 Javascript
vue单页面打包文件大?首次加载慢?nginx带你飞,从7.5M到1.3M蜕变过程(推荐)
2018/01/16 Javascript
vue移动端下拉刷新和上拉加载的实现代码
2018/09/08 Javascript
vue 刷新之后 嵌套路由不变 重新渲染页面的方法
2018/09/13 Javascript
Vuex的初探与实战小结
2018/11/26 Javascript
jQuery实现动态向上滚动
2020/12/21 jQuery
Python中bisect的用法
2014/09/23 Python
在Python的Django框架中生成CSV文件的方法
2015/07/22 Python
Python线性方程组求解运算示例
2018/01/17 Python
对python pandas读取剪贴板内容的方法详解
2019/01/24 Python
Python 通过requests实现腾讯新闻抓取爬虫的方法
2019/02/22 Python
详解Python下载图片并保存本地的两种方式
2019/05/15 Python
Python Multiprocessing多进程 使用tqdm显示进度条的实现
2019/08/13 Python
python 并发编程 非阻塞IO模型原理解析
2019/08/20 Python
python ImageDraw类实现几何图形的绘制与文字的绘制
2020/02/26 Python
python爬虫用request库处理cookie的实例讲解
2021/02/20 Python
CSS3 三维变形实现立体方块特效源码
2016/12/15 HTML / CSS
localStorage的过期时间设置的方法详解
2018/11/26 HTML / CSS
美国本地交易和折扣网站:LocalFlavor.com
2017/10/26 全球购物
Belvilla法国:休闲度假房屋出租
2020/10/03 全球购物
幼儿运动会邀请函
2014/01/17 职场文书
趣味游戏活动方案
2014/02/07 职场文书
大学生职业生涯规划书汇总
2014/03/20 职场文书
大学体育课感想
2015/08/10 职场文书
2016教师节感恩话语
2015/12/09 职场文书
土木工程生产实习心得体会
2016/01/22 职场文书
Windows环境下实现批量执行Sql文件
2021/10/05 SQL Server
三星 3nm 芯片将于第二季度开始量产
2022/04/29 数码科技
nginx 添加http_stub_status_module模块
2022/05/25 Servers