Dajing method (Python implementation)

Source: Internet
Author: User

In computer vision and image processing, the Dajing method is used to automatically obtain the threshold of an image, or to convert a grayscale image to a two-valued image. The algorithm assumes that the image contains two categories of pixels (foreground and background pixels) and then calculates an optimal threshold for separating the foreground and background, minimizing the variance between the foreground and background classes.

The following defines the formula for calculating the inter-class variance:
Suppose we use a threshold T to split a grayscale image into a foreground and a background size: Total number of pixels in the image
U: Average grayscale of images
W0: The ratio of the foreground pixel to the full image size u0: The average of the foreground pixel points
W1: The ratio of the background pixel to the whole image size
U0: Average of background pixel points
G: Inter-class variance

u = w0 * u0 + W1 * U1 (1)
g = w0* (u-u0) ^2 + w1* (U-U1) ^2 (2)
Substituting (1) into (2) the following:
g = w0 * W1 * (U0-U1) ^2

Using the traversal method, all thresholds are traversed, and when G is the maximum, the threshold is what we think is the most appropriate threshold value.
Secretly tell you, OpenCV has its own method of thresholding using the Dajing method, as follows:
Use the parameter cv2. After the Thresh_otsu, the Th1 in front is void.

The following is a Python version of the function I wrote myself:
Import NumPy as Npdef otsu_enhance (Img_gray, th_begin=0, th_end=256, th_step=1):    assert Img_gray.ndim = = 2, "Must Inpu T a gary_img "    max_g = 0    suitable_th = 0 for    threshold in xrange (Th_begin, Th_end, th_step):        bin_img = Img_ Gray > Threshold        BIN_IMG_INV = Img_gray <= threshold        Fore_pix = np.sum (bin_img)        Back_pix = Np.sum (bin_ IMG_INV)        If 0 = = Fore_pix: Break        if 0 = = Back_pix:            continue        w0 = float (fore_pix)/img_gray.size< C13/>u0 = Float (np.sum (Img_gray * bin_img))/Fore_pix        w1 = float (back_pix)/img_gray.size        u1 = float (Np.sum (IM G_gray * BIN_IMG_INV))/Back_pix        # intra-class Variance        g = w0 * W1 * (U0-U1) * (U0-U1)        if g > Max_ G:            max_g = g            suitable_th = threshold    return suitable_th



Dajing method (Python implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.