本文主要是介绍超像素SLIC SEEDS LSC 算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
超像素
超像素是把一张图片中具有相似特征的像素进行聚类,形成一个更具有代表性的大“像素”。这个新的像素可以作为其他图像处理算法的基本单位,可以减低图像的维度和异常像素点。目前常用的超像素分割算法有SLIC、SEEDS和LSC。下面来说说这些算法基于Opencv的Python实现。
简单线性迭代聚类(SLIC)
同时显示三种算法处理结果
# coding=utf-8#############################slic######
#参考 https://blog.csdn.net/xhtchina/article/details/129611149
import cv2
import numpy as npimg = cv2.imread("./data/banma3.jpg")
if img is None: print("Image not loaded")
else: cv2.imshow("img", img)### SLIC 算法
# 初始化slic项,超像素平均尺寸20(默认为10),平滑因子20
slic = cv2.ximgproc.createSuperpixelSLIC(img, region_size=20, ruler=20.0)
slic.iterate(10) # 迭代次数,越大效果越好
mask_slic = slic.getLabelContourMask() # 获取Mask,超像素边缘Mask==1
label_slic = slic.getLabels() # 获取超像素标签
number_slic = slic.getNumberOfSuperpixels() # 获取超像素数目
mask_inv_slic = cv2.bitwise_not(mask_slic)
img_slic = cv2.bitwise_and(img, img, mask=mask_inv_slic) #在原图上绘制超像素边界color_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
color_img[:] = (0, 255 , 0)
result_ = cv2.bitwise_and(color_img, color_img, mask=mask_slic)
result = cv2.add(img_slic, result_)#cv2.imwrite("./data/cat_SLIC.png", result)
cv2.imshow("SLIC.png",result)
#cv2.waitKey(5000)
#cv2.destroyAllWindows()############################SEEDS算法#######################
# import cv2
# import numpy as np# img = cv2.imread("./data/banma3.jpg")
#初始化seeds项,注意图片长宽的顺序
#retval=cv.ximgproc.createSuperpixelSEEDS(image_width, image_height, image_channels, num_superpixels, num_levels[, prior[, histogram_bins[, double_step]]])
seeds = cv2.ximgproc.createSuperpixelSEEDS(img.shape[1],img.shape[0],img.shape[2],2000,15,3,5,True)
seeds.iterate(img,10) #输入图像大小必须与初始化形状相同,迭代次数为10
mask_seeds = seeds.getLabelContourMask()
label_seeds = seeds.getLabels()
number_seeds = seeds.getNumberOfSuperpixels()
mask_inv_seeds = cv2.bitwise_not(mask_seeds)
img_seeds = cv2.bitwise_and(img,img,mask = mask_inv_seeds)color_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
color_img[:] = (0, 255 , 0)
result_ = cv2.bitwise_and(color_img, color_img, mask=mask_seeds)
result = cv2.add(img_seeds, result_)
#cv2.imshow("result_",result_)
#cv2.imshow("seeds",result)
#cv2.imwrite("./data/banma3_seeds.png", result)
#cv2.waitKey(5000)
#cv2.destroyAllWindows()##################################LSC 算法
# import cv2
# import numpy as np# img = cv2.imread("./data/banma3.jpg")
lsc = cv2.ximgproc.createSuperpixelLSC(img)
lsc.iterate(10)
mask_lsc = lsc.getLabelContourMask()
label_lsc = lsc.getLabels()
number_lsc = lsc.getNumberOfSuperpixels()
mask_inv_lsc = cv2.bitwise_not(mask_lsc)
img_lsc = cv2.bitwise_and(img,img,mask = mask_inv_lsc)color_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
color_img[:] = (0, 255 , 0)
result_ = cv2.bitwise_and(color_img, color_img, mask=mask_lsc)
result = cv2.add(img_lsc, result_)cv2.imshow("ours",result)
#cv2.imwrite("./data/banma3_lcs.png", result)
cv2.waitKey(8000)
cv2.destroyAllWindows()
这篇关于超像素SLIC SEEDS LSC 算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!