0

I'd like to use the equivalent of skimage.filters.threshold_multiotsu in OpenCV to detect object in video.

I can get only one threshold in OpenCV, not multiple like in skimage.

OpenCV usually runs faster than skimage for many reasons.

Full code:

#!/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()

The input image:

enter image description here

The output images:

enter image description here

Redhwan
  • 927
  • 1
  • 9
  • 24
  • Could you please provide the image and the expected thresholds? So I can test my code. – Prefect May 20 '20 at 21:52
  • Sure, I updated my question as you want. – Redhwan May 21 '20 at 00:36
  • Hey again, I really tried to make it but something is still wrong with the code. I was following this code [here](https://stackoverflow.com/questions/22706742/multi-otsumulti-thresholding-with-opencv) @Antoni4. I think I will stop here, sorry for not giving you good news. Would you want me to provide the code, so maybe you can go ahead? – Prefect May 21 '20 at 19:02
  • Thanks @Iammuratc, I already tried with the same code. – Redhwan May 22 '20 at 00:46

0 Answers0