Exemplo n.º 1
0
def calculateOrientation21(image, w):

    # smooth image with gaussian blur
    kernel = getgauss(3, 5)
    smoothed_im = applyfil(image, kernel)

    # calculates gradients with sobel filter
    dx, dy = gradientSobel(smoothed_im)

    # smooth gradients
    Gx = applyfil(dx, kernel)
    Gy = applyfil(dy, kernel)

    # compute gradient magnitude
    vx = 2 * Gx * Gy
    vy = (Gx**2) - (Gy**2)
    #G = np.sqrt(Gxx + Gyy)

    # calculate theta
    theta = 0.5 * np.arctan2(vx, vy)

    # smoothens the angles

    tabx = np.zeros((w, w), np.uint8)
    taby = np.zeros((w, w), np.uint8)
    for p in range(0, w - 1):
        for q in range(0, w - 1):
            tabx[p, q] = math.cos(2 * theta[p, q])
            taby[p, q] = math.sin(2 * theta[p, q])
            theta[p, q] = np.pi + math.atan2(tabx[p, q], taby[p, q])

    return theta[w / 2, w / 2]
Exemplo n.º 2
0
def calculateOrientation3(image, w):
    """
    :param image: image segment
    :param w: blocksize
    :return: block orientation
    """
    # smooth image with gaussian blur
    kernel = getgauss(5, 5)
    smoothed_im = applyfil(image, kernel)

    # calculates gradients with sobel filter
    dx, dy = gradientSobel(smoothed_im)

    # smooth gradients
    Gx = applyfil(dx, kernel)
    Gy = applyfil(dy, kernel)
    vx = vy = 0
    # compute gradient magnitude
    for p in range(0, w - 1):
        for q in range(0, w - 1):

            if Gx[p, q] == 0:
                Gx[p, q] = 1

            if Gy[p, q] == 0:
                Gy[p, q] = 1

            if Gx[p, q] == 0 and Gy[p, q] == 0:
                Gx[p, q] = Gy[p, q] = 1

            vx += 2 * Gx[p, q] * Gy[p, q]
            vy += (Gx[p, q]**2) - (Gy[p, q]**2)
    # G = np.sqrt(Gxx + Gyy)

    # calculate theta
    theta = 0.5 * np.arctan2(vx, vy)
    # print(theta)
    # smoothens the angles

    if (theta < 0 and vx < 0) or (vx > 0 and theta > 0):
        k = 0.5

    if (theta < 0 and vx > 0):
        k = 1

    if (theta > 0 and vx < 0):
        k = 0

    tabx = math.cos(2 * theta)
    taby = math.sin(2 * theta)

    theta = k * np.pi + math.atan2(tabx, taby)

    return theta * 180 / np.pi
Exemplo n.º 3
0
def binarize(img):
    gray = sharpen(img)
    kernal = getgauss(3, -20)
    img2 = applyfil(img, kernal)
    img3 = backtoim(img2)
    th3 = cv2.adaptiveThreshold(img3, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 11, 2)  #best
    return th3
Exemplo n.º 4
0
def binarize3(img):
    img = sharpen(img)
    kernal = getgauss(3, -20)
    img = applyfil(img, kernal)
    img = backtoim(img)
    #th3 = cv2.adaptiveThreshold(img3, 254, cv2.ADAPTIVE_THRESH_GAUSSIAN_C ,cv2.THRESH_BINARY,11,2 )    #best
    th3 = cv2.adaptiveThreshold(img, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 255, 0)
    return th3
Exemplo n.º 5
0
def binarize2(img):
    gray = sharpen(img)
    #print(type(gray))
    kernal = getgauss(3, -20)
    img2 = applyfil(img, kernal)
    #img2 = cv2.GaussianBlur(gray, (3, 3), -20)
    img3 = backtoim(img2)
    #th3 = cv2.adaptiveThreshold(img3, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 255, 0) #original
    th3 = cv2.adaptiveThreshold(img3, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 11, 2)  #best
    #blur = cv2.GaussianBlur(img, (5, 5), 0)
    #ret3, th3 = cv2.threshold(img3, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    #ret, th3 = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY)
    #th3 = cv2.adaptiveThreshold(img3, 1, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 255, 0)

    #print(img2.shape)

    return th3