实例介绍
【实例简介】
Waterpixel 非常高效的超像素分割方法
【实例截图】
【核心代码】
def demo_m_waterpixels(imin, step, d_weight, filter_ori):
"""
Compute m-waterpixels, i.e. superpixels based on the watershed transformation.
Flooding starts form the best minimum of each cell of a regular grid.
The gradient used to be flooded is regularized using the distance function to these minima.
Cells of the grid are chosen here to be squares.
Input :
imin (image UINT8): original image, to be segmented into superpixels
step (UINT8) : grid step, i.e. distance between two cell centers of the grid
d_weight (UINT8) : constant to be multiplied with function distance before addition to gradient image.
If d_weight <=0, then only the gradient is taken into account.
filter_ori (BOOLEAN) : do we filter the input image?
Output:
image (UINT16) : labelled superpixels
image (UINT8) : distance weighted gradient function
image (UINT16) : minima used in the computation
"""
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
# Connexity:
basicse = sp.CrossSE()
gridse = sp.SquSE()
# Ori filtering
if filter_ori is True:
my_area_filtering(imin, step*step/16)
imin.show()
# Gradient computation
im_grad = my_gradient(imin, basicse, True)
im_grad.show()
## Pool of working images:
imwrk0 = sp.Image(im_grad)
imwrk1 = sp.Image(im_grad, "UINT16")
imwrk2 = sp.Image(im_grad, "UINT16")
# Compute cell centers and cells
size = imin.getSize()
im_markers, im_cells = im_labelled_square_grid_points(size, step, step/6)
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
## Choice of the markers : one per grid cell
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
# Step 1 : Computation of the minima of the gradient
im_minima = sp.Image(im_grad)
sp.minima(im_grad, im_minima, basicse)
#Step 2 : Imposing minimum distance between minima (= Removing minima candidates which fall on cell margins )
sp.test(im_cells, im_minima, 0, imwrk0)
sp.label(imwrk0, imwrk1, basicse)
#Step 3 : Evaluation of the importance of minima ( = computation of their surfacic extinction)
im_minima_val = sp.Image(imwrk1)
sp.watershedExtinction( im_grad, imwrk1, im_minima_val, "a", basicse)
# Step 4 : Taking back at value 2 the minima of null-volumic extinction
sp.test( imwrk0, 2, 0, imwrk2)
sp.test( im_minima_val, im_minima_val, imwrk2, im_minima_val )
# Step 5 : Coping with the potential absence of minimum in cells (= choosing the minimum value inside this cell as its marker if necessary)
imwrk00=sp.Image(imwrk0)
blobs = sp.computeBlobs(im_cells)
sp.test(im_cells, im_grad, 0, imwrk00)
minVals = sp.measMinVals(imwrk00, blobs)
sp.applyLookup(im_cells, minVals, imwrk0)
sp.test(imwrk00==imwrk0, 1, 0, imwrk1)
maxVals = sp.measMaxVals(im_minima_val, blobs)
sp.applyLookup(im_cells, maxVals, imwrk2)
sp.test(imwrk2, im_minima_val, imwrk1, im_minima_val)
# Step 6 : Selection of one marker per cell
one_min_per_grid_cell(im_cells, blobs, im_minima_val, basicse)
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
## Spatial regularization of the gradient
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
#Step 1 : Computation of the distance function to the markers
immask = sp.Image(im_markers, "UINT8")
sp.test(im_minima_val, 0, 1, immask)
imdist = sp.Image(immask, "UINT8")
sp.dist(immask, imdist, gridse)
#Step 2 : Adding the distance function to the gradient
if d_weight > 0:
weight = d_weight * float(2)/step
sp.mul(imdist, weight, imdist)
sp.add(imdist, im_grad, im_grad)
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
## Superpixel segmentation : watershed transformation, flooding from selected minima on the regularized gradient
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
sp.copy(im_minima_val, imwrk1)
sp.label(imwrk1, im_minima_val, basicse)
imout = sp.Image(im_minima_val)
sp.basins(im_grad, im_minima_val, imout, basicse)
##-----------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------
return imout, im_grad, im_minima_val
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论