关于初始种子自动选取的区域生长实例(python+opencv)


Posted in Python onJanuary 16, 2020

算法中,初始种子可自动选择(通过不同的划分可以得到不同的种子,可按照自己需要改进算法),图分别为原图(自己画了两笔为了分割成不同区域)、灰度图直方图、初始种子图、区域生长结果图。

另外,不管时初始种子选择还是区域生长,阈值选择很重要。

import cv2
import numpy as np
import matplotlib.pyplot as plt

#初始种子选择
def originalSeed(gray, th):
 ret, thresh = cv2.cv2.threshold(gray, th, 255, cv2.THRESH_BINARY)#二值图,种子区域(不同划分可获得不同种子)
 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))#3×3结构元

 thresh_copy = thresh.copy() #复制thresh_A到thresh_copy
 thresh_B = np.zeros(gray.shape, np.uint8) #thresh_B大小与A相同,像素值为0

 seeds = [ ] #为了记录种子坐标

 #循环,直到thresh_copy中的像素值全部为0
 while thresh_copy.any():

  Xa_copy, Ya_copy = np.where(thresh_copy > 0) #thresh_A_copy中值为255的像素的坐标
  thresh_B[Xa_copy[0], Ya_copy[0]] = 255 #选取第一个点,并将thresh_B中对应像素值改为255

  #连通分量算法,先对thresh_B进行膨胀,再和thresh执行and操作(取交集)
  for i in range(200):
   dilation_B = cv2.dilate(thresh_B, kernel, iterations=1)
   thresh_B = cv2.bitwise_and(thresh, dilation_B)

  #取thresh_B值为255的像素坐标,并将thresh_copy中对应坐标像素值变为0
  Xb, Yb = np.where(thresh_B > 0)
  thresh_copy[Xb, Yb] = 0

  #循环,在thresh_B中只有一个像素点时停止
  while str(thresh_B.tolist()).count("255") > 1:
   thresh_B = cv2.erode(thresh_B, kernel, iterations=1) #腐蚀操作

  X_seed, Y_seed = np.where(thresh_B > 0) #取处种子坐标
  if X_seed.size > 0 and Y_seed.size > 0:
   seeds.append((X_seed[0], Y_seed[0]))#将种子坐标写入seeds
  thresh_B[Xb, Yb] = 0 #将thresh_B像素值置零
 return seeds

#区域生长
def regionGrow(gray, seeds, thresh, p):
 seedMark = np.zeros(gray.shape)
 #八邻域
 if p == 8:
  connection = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
 elif p == 4:
  connection = [(-1, 0), (0, 1), (1, 0), (0, -1)]

 #seeds内无元素时候生长停止
 while len(seeds) != 0:
  #栈顶元素出栈
  pt = seeds.pop(0)
  for i in range(p):
   tmpX = pt[0] + connection[i][0]
   tmpY = pt[1] + connection[i][1]

   #检测边界点
   if tmpX < 0 or tmpY < 0 or tmpX >= gray.shape[0] or tmpY >= gray.shape[1]:
    continue

   if abs(int(gray[tmpX, tmpY]) - int(gray[pt])) < thresh and seedMark[tmpX, tmpY] == 0:
    seedMark[tmpX, tmpY] = 255
    seeds.append((tmpX, tmpY))
 return seedMark


path = "_rg.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#hist = cv2.calcHist([gray], [0], None, [256], [0,256])#直方图

seeds = originalSeed(gray, th=253)
seedMark = regionGrow(gray, seeds, thresh=3, p=8)

#plt.plot(hist)
#plt.xlim([0, 256])
#plt.show()
cv2.imshow("seedMark", seedMark)
cv2.waitKey(0)

关于初始种子自动选取的区域生长实例(python+opencv)

关于初始种子自动选取的区域生长实例(python+opencv)

关于初始种子自动选取的区域生长实例(python+opencv)

关于初始种子自动选取的区域生长实例(python+opencv)

以上这篇关于初始种子自动选取的区域生长实例(python+opencv)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现豆瓣图片下载的方法
May 25 Python
详解Python中使用base64模块来处理base64编码的方法
Jul 01 Python
python 中的divmod数字处理函数浅析
Oct 17 Python
一篇文章了解Python中常见的序列化操作
Jun 20 Python
pandas 空数据处理方法详解
Nov 02 Python
在PyCharm中遇到pip安装 失败问题及解决方案(pip失效时的解决方案)
Mar 10 Python
Python+Appium实现自动化测试的使用步骤
Mar 24 Python
Python基于百度AI实现OCR文字识别
Apr 02 Python
python实现单机五子棋
Aug 28 Python
Python自动化办公Excel模块openpyxl原理及用法解析
Nov 05 Python
利用python爬取有道词典的方法
Dec 08 Python
讲解Python实例练习逆序输出字符串
May 06 Python
Python简单实现区域生长方式
Jan 16 #Python
python3.8与pyinstaller冲突问题的快速解决方法
Jan 16 #Python
Pycharm中Python环境配置常见问题解析
Jan 16 #Python
Python Numpy库常见用法入门教程
Jan 16 #Python
Python使用Pandas库常见操作详解
Jan 16 #Python
Python 日期的转换及计算的具体使用详解
Jan 16 #Python
Python使用循环神经网络解决文本分类问题的方法详解
Jan 16 #Python
You might like
PHP折半(二分)查找算法实例分析
2018/05/12 PHP
PHP与Perl之间知识点区别整理
2019/03/19 PHP
php面向对象程序设计中self与static的区别分析
2019/05/21 PHP
如何在PHP中使用AES加密算法加密数据
2020/06/24 PHP
JS将表单导出成EXCEL的实例代码
2013/11/11 Javascript
JavaScript实现继承的4种方法总结
2014/10/16 Javascript
javascript模拟map输出与去除重复项的方法
2015/02/09 Javascript
JavaScript资源预加载组件和滑屏组件的使用推荐
2016/03/10 Javascript
AngularJS模块详解及示例代码
2016/08/17 Javascript
利用Js的console对象,在控制台打印调式信息测试Js的实现
2016/11/26 Javascript
JS实现页面中所有img对象添加onclick事件及新窗口查看图片的方法
2016/12/27 Javascript
微信小程序 form组件详解及简单实例
2017/01/10 Javascript
vue实现点击选中,其他的不选中方法
2018/09/05 Javascript
vue项目刷新当前页面的三种方法
2018/12/04 Javascript
vue elementUI table 自定义表头和行合并的实例代码
2019/05/22 Javascript
js实现聊天对话框
2020/02/08 Javascript
[01:45]典藏宝瓶2+祈求者身心——这就是DOTA2TI9总奖金突破3000万美元的秘密
2019/07/21 DOTA
栈和队列数据结构的基本概念及其相关的Python实现
2015/08/24 Python
python 与GO中操作slice,list的方式实例代码
2017/03/20 Python
Python3网络爬虫之使用User Agent和代理IP隐藏身份
2017/11/23 Python
Python实现两个list求交集,并集,差集的方法示例
2018/08/02 Python
python的pytest框架之命令行参数详解(下)
2019/06/27 Python
Python如何使用Gitlab API实现批量的合并分支
2019/11/27 Python
python matplotlib库的基本使用
2020/09/23 Python
全球立体声:World Wide Stereo
2018/09/29 全球购物
.NET现在共支持多少种语言
2014/02/26 面试题
高职助产应届生自荐信
2013/09/24 职场文书
优秀毕业生推荐信
2013/11/02 职场文书
房地产还款计划书
2014/01/10 职场文书
学生安全承诺书
2014/05/22 职场文书
转让协议书
2015/01/27 职场文书
法律意见书范文
2015/05/20 职场文书
超强台风观后感
2015/06/09 职场文书
Python selenium的这三种等待方式一定要会!
2021/06/10 Python
动漫APP软件排行榜前十名,半次元上榜,第一款由腾讯公司推出
2022/03/18 杂记
安装Ruby和 Rails的详细步骤
2022/04/19 Ruby