How to automatically select the best result from try_all_threshold?

I apply a threshold value on a text value. Using skimage.filters.try_all_thresholdleads to the application of 7 applied threshold algorithms. I can get the result, but I’m thinking about how I can only select 1 result to pass the result to the next process / dynamically select 1 best result.

+4
source share
1 answer

You need to determine the measure of similarity between the original image and the binary images, and then choose a threshold method that maximizes this measure.

Demo

, . , similarity , . .

import numpy as np
from skimage.data import text
import skimage.filters
import matplotlib.pyplot as plt

threshold_methods = [skimage.filters.threshold_otsu,
                     skimage.filters.threshold_yen,
                     skimage.filters.threshold_isodata,
                     skimage.filters.threshold_li,
                     skimage.filters.threshold_mean,
                     skimage.filters.threshold_minimum,
                     skimage.filters.threshold_mean,
                     skimage.filters.threshold_triangle,
                     ]

def similarity(img, threshold_method):
    """Similarity measure between the original image img and and the
    result of applying threshold_method to it.
    """
    return np.random.random()

results = np.asarray([similarity(text(), f) for f in threshold_methods])    
best_index = np.nonzero(results == results.min())[0][0]    
best_method = thresholding_methods[best_index]
threshold = best_method(text())
binary = text() >= threshold

fig, ax = plt.subplots(1, 1)
ax.imshow(binary, cmap=plt.cm.gray)
ax.axis('off')
ax.set_title(best_method.__name__)
plt.show(fig)

isodata

, ( ). , . , , , , , , . , , , :

best_index = np.nonzero(results == results.min())[0][0]

.

+1

Source: https://habr.com/ru/post/1696177/


All Articles