什么是OpenCV中skimage的“阈值”的等价物?

2024-05-28 23:05:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在OpenCV中使用skimage.filters.threshold_multiotsu的等价物来检测视频中的对象

我在OpenCV中只能获得一个阈值,而不像skimage中的多个阈值

由于许多原因,OpenCV通常比skimage运行得更快

完整代码:

#!/usr/bin/env python
__author__ = "Sreenivas Bhattiprolu"
__license__ = "Feel free to copy, I appreciate if you acknowledge Python for Microscopists"

# https://youtu.be/YdhhiXDQDl4




import matplotlib.pyplot as plt
import numpy as np

from skimage import data, io, img_as_ubyte
from skimage.filters import threshold_multiotsu

# Read an image
image = io.imread("images/BSE.jpg")


# Apply multi-Otsu threshold 
thresholds = threshold_multiotsu(image, classes=5)

# Digitize (segment) original image into multiple classes.
#np.digitize assign values 0, 1, 2, 3, ... to pixels in each class.
regions = np.digitize(image, bins=thresholds)
output = img_as_ubyte(regions)  #Convert 64 bit integer values to uint8

plt.imsave("images/Otsu_Segmented.jpg", output)


#Let us look at the input image, thresholds on thehistogram and final segmented image
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(10, 3.5))

# Plotting the original image.
ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original')
ax[0].axis('off')

# Plotting the histogram and the two thresholds obtained from
# multi-Otsu.
ax[1].hist(image.ravel(), bins=255)
ax[1].set_title('Histogram')
for thresh in thresholds:
    ax[1].axvline(thresh, color='r')

# Plotting the Multi Otsu result.
ax[2].imshow(regions, cmap='Accent')
ax[2].set_title('Multi-Otsu result')
ax[2].axis('off')

plt.subplots_adjust()

plt.show()

输入图像:

enter image description here

输出图像:

enter image description here


Tags: thetofromimageimportthresholdasnp

热门问题