资源简介
简单线性非迭代算法产生超像素,与SLIC效果差不多,但是效率高
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Tue Mar 20 11:10:24 2018
@author: Administrator
“““
import numpy as np
from PIL import Image
from skimage import color
import math
from Queue import PriorityQueue
import cv2
def findseeds(im_labwidthheightk):
gridstep_x=int(math.sqrt(height*width/k)+0.5)
gridstep_y=int(math.sqrt(height*width/k)+0.5)#compute the size of gridstep
#compute the location of seeds in grid
halfstep_x=int(gridstep_x/2)
halfstep_y=int(gridstep_y/2)
xsteps=int(width/gridstep_x)
ysteps=int(height/gridstep_y)
#compute the error of numberof the seeds
err1=abs(xsteps*ysteps-k)
err2=abs(int(width/(gridstep_x-1))*int(height/(gridstep_y-1))-k)
if err1 gridstep_x=gridstep_x-1
gridstep_y=gridstep_y-1
xsteps=int(width/gridstep_x)
ysteps=int(height/gridstep_y)
#compute the new number
numk=xsteps*ysteps
ck=np.zeros((numk5))
i=0;j=0;k=0
for i in range(ysteps):
for j in range(xsteps):
temp_x=halfstep_x+j*gridstep_x
temp_y=halfstep_y+i*gridstep_y
ck[k][0]=temp_x
ck[k][1]=temp_y
ck[k][2]=im_lab[temp_y][temp_x][0]
ck[k][3]=im_lab[temp_y][temp_x][1]
ck[k][4]=im_lab[temp_y][temp_x][2]
k=k+1
return(numkck)
def initial_label(heightwidth):
labels=np.zeros((heightwidth))
for i in range(height):
for j in range(width):
labels[i][j]=-1
return(labels)
class NODE(object):
def __init__(selfprioritydescription1description2description3):
distance=int()
xk=int()
yk=int()
k=int()
self.priority=distance
self.description1=xk
self.description2=yk
self.description3=k
def __cmp__(selfother):
return cmp(self.priorityother.priority)
def putinQ(ckQnumk):
for k in range(numk):
tempnode=NODE(0.0111)
tempnode.priority=0.0
tempnode.description1=ck[k][0]
tempnode.description2=ck[k][1]
tempnode.description3=k
Q.put(tempnode)
return(Q)
def runsnic(Qim_lablabelsnumkm):
Qlength=Q.qsize()
ksize=np.zeros((numk1))
connectivity=4 #or 8
p4_x=[1-100]
p4_y=[001-1]
LABXk=np.zeros((numk5))
s=math.sqrt((height*width)/numk)
#在这里选择while的原因在于最终循环的截止条件为队列长度为0,但是如果将Qlength作为循环
#终止条件,它本身就是变的,我看资料说是最好使用while才能不出错
fnum=0
while Qlength>0:
temp=Q.get()
i=int(temp.description2)
j=int(temp.description1)
k=int(temp.description3)
if labels[i][j]==-1:
labels[i][j]=k
fnum=fnum+1
LABXk[k][0]=LABXk[k][0]+im_lab[i][j][0]
LABXk[k][1]=LABXk[k][1]+im_lab[i][j][1]
LABXk[k][2]=LABXk[k][2]+im_lab[i][j][2]
LABXk[k][3]=LABXk[k][3]+j
LABXk[k][4]=LABXk[k][4
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4940240 2018-03-20 22:01 snic_超像素\Superpixels and Polygons using Simple Non-Iterative Clustering.pdf
文件 5385 2018-03-23 10:43 snic_超像素\snic.py
目录 0 2018-04-01 16:26 snic_超像素\
评论
共有 条评论