使用鼠标交互实现区域生长算法
© Fu Xianjun. All Rights Reserved
素材

代码
import cv2
import numpy as np
class Point(object):
def init(self,x,y):
self.x = x
self.y = y
def getX(self):
return self.x
def getY(self):
return self.y
#计算像素之间的偏差
def getGrayDiff(img,currentPoint,tmpPoint):
return abs(int(img[currentPoint.x,currentPoint.y]) - int(img[tmpPoint.x,tmpPoint.y]))
#设定八邻域或四邻域(我对原程序进行改动,只选择了八邻域)
#def selectConnects§:
def selectConnects():
#if p != 0:
connects = [Point(-1, -1), Point(0, -1), Point(1, -1), Point(1, 0), Point(1, 1),
Point(0, 1), Point(-1, 1), Point(-1, 0)]#八邻域
#else:
#connects = [ Point(0, -1), Point(1, 0),Point(0, 1), Point(-1, 0)]#四邻域
return connects
#生长函数
def regionGrow(img,seeds,thresh,p = 1):
#读取图像的宽

本文介绍了如何使用Python和OpenCV实现鼠标交互式的区域生长算法。通过点击图像上的点作为种子点,根据像素间的灰度差值判断相邻像素是否属于同一区域,以此实现图像分割。用户可以实时调整阈值并观察分割效果。
最低0.47元/天 解锁文章
2616

被折叠的 条评论
为什么被折叠?



