python-opencv实现图像超像素分割(SLIC、SEEDS、LSC)

本文介绍了如何使用Python和OpenCV库实现图像的超像素分割,包括SLIC、SEEDS和LSC三种算法。通过调用OpenCV中的相应函数,详细展示了每种算法的参数设置和实现步骤,并提供了代码示例。通过超像素分割,可以将图像中具有相似特征的像素聚类,降低图像处理的复杂度并增强视觉效果。
摘要由CSDN通过智能技术生成

转载自:苏格拉- PYTHON - OPENCV实现图像超像素分割(SLIC、SEEDS、LSC)


超像素

超像素是把一张图片中具有相似特征的像素进行聚类,形成一个更具有代表性的大“像素”。这个新的像素可以作为其他图像处理算法的基本单位,可以减低图像的维度和异常像素点。目前常用的超像素分割算法有SLIC、SEEDS和LSC。下面来说说这些算法基于Opencv的Python实现。

SLIC算法

算法具体原理可参考博客:SLIC超像素算法原理

利用opencv中ximgproc类下的子类SuperpixelSLIC,实现SLIC算法。

python调用方法:

retval=cv.ximgproc.createSuperpixelSLIC(image[, algorithm[, region_size[, ruler]]])

其中各个参数意义如下:

  1. image:输入图像
  2. algorithm:选择要使用的算法变体:SLIC、SLICO(默认)和MSLIC三种可选
  3. region_size:平均超像素大小,默认10
  4. 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]]])

其中各个参数意义如下:

  1. image_width:输入图像宽度
  2. image_height:输入图像高度
  3. image_channels:输入图像通道数
  4. num_superpixels:期望超像素数目
  5. num_levels:块级别数,值越高,分段越准确,形状越平滑,但需要更多的内存和CPU时间
  6. histogram_bins:直方图bins数,默认5
  7. 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]])

其中各个参数意义如下:

  1. image:输入图像
  2. **region_size **:平均超像素大小,默认10
  3. 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)

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值