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

yipeiwu_com5年前Python基础

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

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

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设计】。

相关文章

django框架实现一次性上传多个文件功能示例【批量上传】

django框架实现一次性上传多个文件功能示例【批量上传】

本文实例讲述了django框架实现一次性上传多个文件功能。分享给大家供大家参考,具体如下: 在用django 写文件上传的时候,从request.FILES['myfiles'] 获取到...

python实现本地图片转存并重命名的示例代码

//有1-22个文件夹,各文件夹下有Detect_0文件夹,此文件夹下有source与mask文件夹,目的是将需要获取图片的 文件夹下的图片复制到新的文件夹下并按顺序重命名 impo...

你还在@微信官方?聊聊Python生成你想要的微信头像

你还在@微信官方?聊聊Python生成你想要的微信头像

今天早上@微信官方突然火了, 一句“请给我一面国旗@微信官方” 刷遍朋友圈。 到底是什么呢? 我们先来看看朋友圈 当然,这只是零零散散的部分截图, 看到这些,一股热血洒了出来, 我兴...

在pytorch 中计算精度、回归率、F1 score等指标的实例

pytorch中训练完网络后,需要对学习的结果进行测试。官网上例程用的方法统统都是正确率,使用的是torch.eq()这个函数。 但是为了更精细的评价结果,我们还需要计算其他各个指标。在...

跟老齐学Python之编写类之三子类

关于类,看官想必已经有了感觉,看下面的代码,请仔细阅读,并看看是否能够发现点什么问题呢? 复制代码 代码如下: #!/usr/bin/env python #coding:utf-8 c...