SuperPixel 的样例代码

总说

这个给自己看的,对读者应该没啥用。。
超像素的例子

import sys
import os
import cv2
import numpy as np
import math

img = cv2.imread('people.png')

#------------- SP Generation -------------------------
num_superpixels = 50  # desired number of superpixels
num_iterations = 4     # number of pixel level iterations. The higher, the better quality
prior = 2              # for shape smoothing term. must be [0, 5]
num_levels = 4
num_histogram_bins = 5 # number of histogram bins
height, width, channels = img.shape



converted_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
t_h, t_w, t_c = converted_img.shape
seeds = cv2.ximgproc.createSuperpixelSEEDS(t_w, t_h, t_c, num_superpixels, num_levels, prior, num_histogram_bins)
seeds.iterate(converted_img, num_iterations)
num_of_superpixels_result = seeds.getNumberOfSuperpixels()
print('Final number of superpixels: %d' % num_of_superpixels_result)

# draw contour
mask = seeds.getLabelContourMask(False)
cv2.imwrite('mask.png', mask)
cv2.imshow('MaskWindow', mask)
cv2.waitKey(0)

# draw color coded image
color_img = np.zeros((height, width, 3), np.uint8)
color_img[:] = (0, 0, 255)
mask_inv = cv2.bitwise_not(mask)
result_bg = cv2.bitwise_and(img, img, mask=mask_inv)
result_fg = cv2.bitwise_and(color_img, color_img, mask=mask)
result = cv2.add(result_bg, result_fg)
cv2.imwrite('result.png', result)
cv2.imshow('ColorCodedWindow', result)
cv2.waitKey(0)


cv2.destroyAllWindows()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值