immediate neighborhood to improve segmentation results. At each pixel, the average gray-level value of the neighborhood is calculated. Let the gray level of the given pixel be divided into discrete values and the average gray level is also divided into the same values. Then a pair is formed: the pixel gray level and the average of the neighborhood . Each pair belongs to one of the possible 2-dimensional bins. The total number of occurrences (frequency), , of a pair , divided by the total number of pixels in the image , defines the joint probability mass function in a 2-dimensional histogram: And the 2-dimensional Otsu's method is developed based on the 2-dimensional histogram as follows. The probabilities of two classes can be denoted as: The intensity mean value vectors of two classes and total mean vector can be expressed as follows: In most cases the probability off-diagonal will be negligible, so it is easy to verify: The inter-class discrete matrix is defined as The trace of the discrete matrix can be expressed as where Similar to one-dimensional Otsu's method, the optimal threshold is obtained by maximizing . Algorithm The and is obtained iteratively which is similar with one-dimensional Otsu's method. The values of and are changed till we obtain the maximum of , that is max,s,t = 0; for ss: 0 to L-1 do for tt: 0 to L-1 do evaluate tr(S_b); if tr(S_b) > max max = tr(S,b); s = ss; t = tt; end if end for end for return s,t; Notice that for evaluating , we can use a fast recursive dynamic programming algorithm to improve time performance. However, even with the dynamic programming approach, 2d Otsu's method still has large time complexity. Therefore, much research has been done to reduce the computation cost. If summed area tables are used to build the 3 tables, sum over , sum over , and sum over , then the runtime complexity is the maximum of (O(N_pixels), O(N_bins*N_bins)). Note that if only coarse resolution is needed in terms of threshold, N_bins can be reduced. MATLAB implementation function inputs and output: hists is a 2D-histogram of grayscale value and neighborhood average grayscale value pair. total is the number of pairs in the given image.it is determined by the number of the bins of 2D-histogram at each direction. threshold is the threshold obtained. function threshold = otsu_2D(hists, total) maximum = 0.0; threshold