之前做SLIC分割的过程中在网上找到的代码都只能得到标注分割线的图但不能得到分割后的超像素块,所以写了一个能够保存分割后的超像素块的python程序。
进行SLIC分割部分:
import skimage
from skimage.segmentation import slic,mark_boundaries
from skimage import io
import matplotlib.pyplot as plt
from PIL import Image, ImageEnhance
import numpy as np
import cv2
#
# np.set_printoptions(threshold=np.inf)
path = 'C:\\Users\\Administrator\\Desktop\\SLIC\\'
img_name = 'test.png'
img = io.imread(path + img_name,as_gray=True) #as_gray是灰度读取,得到的是归一化值
segments = slic(img, n_segments=10, compactness=0.2,start_label = 1)#进行SLIC分割
out=mark_boundaries(img,segments)
out = out*255 #io的灰度读取是归一化值,若读取彩色图片去掉该行
img3 = Image.fromarray(np.uint8(out))
img3.show()
seg_img_name = 'seg.png'
img3.save(path +'\\' +seg_img_name)#显示并保存加上分割线后的图片
对分割得到的超像素块逐一保存
maxn = max(segments.reshape(int(segments.shape[0]*segments.shape[1]),))
for i in range(1,maxn+1):
##### 保存灰度图片的部分,as_gray=True
a = np.array(segments == i)
b = img * a
w,h = [],[]
for x in range(b.shape[0]):
for y in range(b.shape[1]):
if b[x][y] != 0:
w.append(x)
h.append(y)
c = b[min(w):max(w),min(h):max(h)]
c = c*255
d = c.reshape(c.shape[0],c.shape[1],1)
e = np.concatenate((d,d),axis=2)
e = np.concatenate((e,d),axis=2)
###########
'''
##### 保存彩色图片的部分,as_gray=True
a = np.array(segments == i)
a = a.reshape(a.shape[0],a.shape[1],1)
a1 = np.concatenate((a,a),axis=2)
a = np.concatenate((a1, a), axis=2)
b = img * a
w,h = [],[]
for x in range(b.shape[0]):
for y in range(b.shape[1]):
if b[x][y][0] != 0:
w.append(x)
h.append(y)
c = b[min(w):max(w),min(h):max(h)]
e = c.reshape(c.shape[0],c.shape[1],3)
###################
'''
img2 = Image.fromarray(np.uint8(e))
img2.save(path +'\\'+str(i)+'.png')
print('已保存第' + str(i) + '张图片')
由于超像素块太小了不好找,做了一个在带分割线的图上的标记,标记值为保存下的图片的名称,虽然这个标记不太准但能起到辅助作用
img = io.imread(path+'\\'+seg_img_name)#读取分割的图片
for i in range(1,maxn+1):
w,h = [],[]
for x in range(segments.shape[0]):
for y in range(segments.shape[1]):
if segments[x][y] == i:
w.append(x)
h.append(y)
font=cv2.FONT_HERSHEY_SIMPLEX#使用默认字体
#print((min(w),min(h)))
img=cv2.putText(img,str(i),(h[int(len(h)/(2))],w[int(len(w)/2)]),font,1,(255,255,255),2)#添加文字,1.2表示字体大小,(0,40)是初始的位置,(255,255,255)表示颜色,2表示粗细
img = Image.fromarray(np.uint8(img))
img.show()
img.save(path +'\\'+seg_img_name+'_label.png')
https://github.com/LarkMi/SLIC