超像素
超像素是把一张图片中具有相似特征的像素进行聚类,形成一个更具有代表性的大“像素”。这个新的像素可以作为其他图像处理算法的基本单位,可以减低图像的维度和异常像素点。目前常用的超像素分割算法有SLIC、SEEDS和LSC。下面来说说这些算法基于Opencv的Python实现。
SLIC算法
算法具体原理可参考博客:SLIC超像素算法原理
利用opencv中ximgproc类下的子类SuperpixelSLIC,实现SLIC算法。
python调用方法:
retval=cv.ximgproc.createSuperpixelSLIC(image[, algorithm[, region_size[, ruler]]])
其中各个参数意义如下:
- image:输入图像
- algorithm:选择要使用的算法变体:SLIC、SLICO(默认)和MSLIC三种可选
- region_size:平均超像素大小,默认10
- ruler:超像素平滑度,默认10
python具体实现如下:
import cv2
import numpy as np
img = cv2.imread("cat.jpg")
### 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("./cat_SLIC.png", result)
SEEDS算法
利用opencv中ximgproc类下的子类createSuperpixelSEEDS,实现SEEDS算法。
python调用方法:
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数,默认5
- double_step:如果为true,则每个块级别重复两次以提高准确性默认false
python具体实现如下:
import cv2
import numpy as np
img = cv2.imread("cat.jpg")
### SEEDS 算法
# 初始化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)
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_seeds, result_)
cv2.imwrite("./cat_SEEDS.png", result)
LSC算法
利用opencv中ximgproc类下的子类createSuperpixelLSC,实现LSC算法。
python调用方法:
retval=cv.ximgproc.createSuperpixelLSC(image[, region_size[, ratio]])
其中各个参数意义如下:
- image:输入图像
- **region_size **:平均超像素大小,默认10
- ratio:超像素紧凑度因子,默认0.075
python具体实现如下:
import cv2
import numpy as np
img = cv2.imread("cat.jpg")
### SEEDS 算法
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_slic)
result = cv2.add(img_lsc, result_)
cv2.imwrite("./cat_LSC.png", result)