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实现的寻找前5个默尼森数算法示例
Mar 25 Python
详解Python if-elif-else知识点
Jun 11 Python
opencv python 基于KNN的手写体识别的实例
Aug 03 Python
python去重,一个由dict组成的list的去重示例
Jan 21 Python
djang常用查询SQL语句的使用代码
Feb 15 Python
Python 给定的经纬度标注在地图上的实现方法
Jul 05 Python
Python将文字转成语音并读出来的实例详解
Jul 15 Python
python实现读取excel文件中所有sheet操作示例
Aug 09 Python
使用pyqt5 tablewidget 单元格设置正则表达式
Dec 13 Python
python 如何快速复制序列
Sep 07 Python
python爬虫多次请求超时的几种重试方法(6种)
Dec 01 Python
python自动化八大定位元素讲解
Jul 09 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上传图片、删除图片实现代码
2010/05/12 PHP
PHP stream_context_create()作用和用法分析
2011/03/29 PHP
采集邮箱的php代码(抓取网页中的邮箱地址)
2012/07/17 PHP
php实现生成带二维码图片并强制下载功能
2018/02/24 PHP
php+js实现点赞功能的示例详解
2020/08/07 PHP
JQuery优缺点分析说明
2011/04/10 Javascript
分享27个jQuery 表单插件集合推荐
2011/04/25 Javascript
加载 Javascript 最佳实践
2011/10/30 Javascript
nodejs win7下安装方法
2012/05/24 NodeJs
js通过地址栏给action传值(中文乱码全是问号)
2013/05/02 Javascript
JQuery拖动表头边框线调整表格列宽效果代码
2014/09/10 Javascript
Bootstrap导航栏各元素操作方法(表单、按钮、文本)
2015/12/28 Javascript
JavaScript函数参数的传递方式详解
2017/03/06 Javascript
在knockoutjs 上自己实现的flux(实例讲解)
2017/12/18 Javascript
vue-cli axios请求方式及跨域处理问题
2018/03/28 Javascript
Vue实现的父组件向子组件传值功能示例
2019/01/19 Javascript
详解vuex的简单todolist例子
2019/07/14 Javascript
解决vue项目中出现Invalid Host header的问题
2020/11/17 Javascript
[46:23]完美世界DOTA2联赛PWL S2 FTD vs Magma 第一场 11.20
2020/11/23 DOTA
Python迭代用法实例教程
2014/09/08 Python
Python greenlet实现原理和使用示例
2014/09/24 Python
Django中Forms的使用代码解析
2018/02/10 Python
基于python的多进程共享变量正确打开方式
2018/04/28 Python
python跳过第一行快速读取文件内容的实例
2018/07/12 Python
python爬虫简单的添加代理进行访问的实现代码
2019/04/04 Python
下载官网python并安装的步骤详解
2019/10/12 Python
Python confluent kafka客户端配置kerberos认证流程详解
2020/10/12 Python
CSS3的新特性介绍
2008/10/31 HTML / CSS
html5如何及时更新缓存文件(js、css或图片)
2013/06/24 HTML / CSS
西雅图电动自行车公司:Rad Power Bikes
2020/02/02 全球购物
数控机械专业个人的自我评价
2014/01/02 职场文书
欢迎领导标语
2014/06/27 职场文书
升职自我推荐信范文
2015/03/25 职场文书
小学英语教师2015年度个人工作总结
2015/10/14 职场文书
化工厂员工工作总结
2015/10/15 职场文书
Linux中sftp常用命令整理
2022/06/28 Servers