1. 超像素超像素是把一张图片中具有相似特征的像素进行聚类形成一个更具有代表性的大“像素”。这个新的像素可以作为其他图像处理算法的基本单位可以减低图像的维度和异常像素点。目前常用的超像素分割算法有SLIC、SEEDS和LSC。下面来说说这些算法基于OpenCV的Python实现。2. SLIC算法retval cv2.ximgproc.createSuperpixelSLIC(image[, algorithm[, region_size[, ruler]]])其中各个参数意义如下image输入图像algorithm选择要使用的算法变体SLIC、SLICO默认和MSLIC三种可选region_size平均超像素大小默认10ruler超像素平滑度默认10python具体实现如下import cv2 import numpy as np img cv2.imread(mao.jpg) #初始化slic项超像素平均尺寸20默认为10平滑因子20 slic cv2.ximgproc.createSuperpixelSLIC(img,region_size20,ruler 20.0) slic.iterate(10) #迭代次数越大效果越好 mask_slic slic.getLabelContourMask() #获取Mask超像素边缘Mask1 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) #在原图上绘制超像素边界 cv2.imshow(img_slic,img_slic) cv2.waitKey(0) cv2.destroyAllWindows()3. SEEDS算法retval cv.ximgproc.createSuperpixelSEEDS(image_width, image_height, image_channels, num_superpixels, num_levels[, prior[, histogram_bins[, double_step]]])其中各个参数意义如下image_width输入图像宽度image_height输入图像高度image_channels输入图像通道数num_superpixels期望超像素数目num_levels块级别数值越高分段越准确形状越平滑但需要更多的内存和CPU时间。histogram_bins直方图bins数默认5double_step如果为true则每个块级别重复两次以提高准确性默认false。python具体实现如下import cv2 import numpy as np img cv2.imread(mao.jpg) #初始化seeds项注意图片长宽的顺序 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) cv2.imshow(img_seeds,img_seeds) cv2.waitKey(0) cv2.destroyAllWindows()4. LSC算法retval cv.ximgproc.createSuperpixelLSC(image[, region_size[, ratio]])其中各个参数意义如下image输入图像region_size平均超像素大小默认10ratio超像素紧凑度因子默认0.075python具体实现相似如下所示import cv2 import numpy as np img cv2.imread(mao.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) cv2.imshow(img_lsc, img_lsc) cv2.waitKey(0) cv2.destroyAllWindows()参考文献Python - Opencv实现图像超像素分割SLIC、SEEDS、LSC_cv2.ximgproc.createsuperpixelslic_苏格拉-的博客-CSDN博客Opencv 图像超像素分割SLIC、SEEDS、LSC - 腾讯云开发者社区-腾讯云