خرید بک لینک

Vote count: 0

I am writing a code which will use Kittlers method of adaptive thresholding to threshold my image.. I think I am nearly there. I have copied some matlab code and am having trouble converting it to python. Any help is appreciated. :D

import cv2
import numpy as np
import math 

#%%

def kittlerMinimimErrorThresholding(img):
#%KITTLERMINIMIMERRORTHRESHOLDING Compute an optimal image threshold.
#%   Computes the Minimum Error Threshold as described in
#%   
#%   'J. Kittler and J. Illingworth, "Minimum Error Thresholding,"      Patte
#%   Recognition 19, 41-47 (1986)'.
#%   
#%   The image 'img' is expected to have integer values from 0 to 255.
#%   'optimalThreshold' holds the found threshold. 'J' holds the values of
#%   the criterion function.
     #Initialize the criterion function
     J = np.ones(shape=(255,1),dtype = np.uint8)

     #Compute the relative histogram
     histogram,bins = np.histogram(img.flatten(),255,[0,255])

#%Walk through every possible threshold. However, T is interpreted
#%differently than in the paper. It is interpreted as the lower boundary of
#%the second class of pixels rather than the upper boundary of the first
#%class. That is, an intensity of value T is treated as being in the same
#%class as higher intensities rather than lower intensities.

    for T in np.arange(1,255,0):
        #Split the hostogram at the threshold T.
        histogram1 = histogram[1:T]
        histogram2 = histogram[(T+1):255]

        #Compute the number of pixels in the two classes.
        P1 = np.sum(histogram1)
        P2 = np.sum(histogram2)
        #Only continue if both classes contain at least one pixel.
        if ((P1 > 0) & (P2 > 0)):

            #Compute the standard deviations of the classes.
            mean1 = np.sum(histogram1 * [1:T]) / P1
            mean2 = np.sum(histogram2 * [1:(256-T)]) / P2

            sigma1 = math.sqrt(np.sum(histogram1 * (([1:T] - mean1) **2) ) / P1)
            sigma2 = math.sqrt(np.sum(histogram2 * (([1:(256-T)] - mean2) **2) ) / P2)
            #Only compute the criterion function if both classes contain at
            #least two intensity values.
            if ((sigma1 > 0) & (sigma2 > 0)):
                #Compute the criterion function.
                J[T] = 1 + 2 * (P1 * log(sigma1) + P2 * log(sigma2)) - 2 * (P1 * log(P1) + P2 * log(P2))

#Find the minimum of J.

optimalThreshold = np.amin(J)
optimalThreshold = optimalThreshold - 0.5

retu(optimalThreshold, J)

Im having problems with the means and standard deviation and don't really understand how these things work in python as I am knew to python.

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 6 ارديبهشت 1395 ساعت: 23:44

صفحه بندی