Beispiel #1
1
def get_filtered_CSF_img(img_in):
    img_dft = cv2.dft(np.float32(img_in), flags=cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(img_dft)
    height = img_dft.shape[0]
    weight = img_dft.shape[1]
    M = weight / 2
    N = height / 2
    H_matrix = np.zeros((height, weight))

    for h_idx in range(height):
        for w_idx in range(weight):
            m = -M + w_idx + 0.5
            n = -N + h_idx + 0.5
            freq, theta = get_freq_dirc(m, n, weight, height)
            multiVal = freq_trans_func(freq, theta)
            H_matrix[h_idx][w_idx] = multiVal

    img_magi = cv2.magnitude(img_dft[:, :, 0], img_dft[:, :, 1])
    img_magi *= H_matrix
    img_phase = cv2.phase(img_dft[:, :, 0], img_dft[:, :, 1])

    img_re = img_magi * np.cos(img_phase)
    img_im = img_magi * (np.sin(img_phase))

    img_dft2 = np.dstack((img_re, img_im))

    imgback = cv2.idft(img_dft2)
    imgback = cv2.magnitude(imgback[:, :, 0], imgback[:, :, 1])

    return imgback
Beispiel #2
0
    def hybridImage(self):
        #Gaussian method
        trans1 = self.myGaussian(self.img1)
        trans2 = self.img2 - self.myGaussian(self.img2)
        result = trans2 + trans1
        # result = result.astype(np.uint16)
        cv.imwrite(OUTPUT_GAUSSIAN, result)

        #FFT method
        trans1 = self.myFFT(self.img1)
        trans2 = self.myFFT(self.img2)
        h = trans1.shape[0]
        w = trans1.shape[1]

        #中心点坐标
        center_w = w // 2
        center_h = h // 2

        window = np.zeros((h, w))

        for i in range(h):
            for j in range(w):
                r = ((i - center_h)**2 + (j - center_w)**2)**0.5
                window[i][j] = self.get_cv(r, SIGMA_FFT)

        #magnitude为幅值域,phase为相位域,幅值域与滤镜相乘,相位不变。
        magnitude1 = cv.magnitude(trans1.real, trans1.imag)
        magnitude2 = cv.magnitude(trans2.real, trans2.imag)
        phase1 = cv.phase(trans1.real, trans1.imag)
        phase2 = cv.phase(trans2.real, trans2.imag)
        for i in range(3):
            magnitude1[:, :, i] *= window
            magnitude2[:, :, i] -= magnitude2[:, :, i] * window

        #重新变为复数域
        result1_real, result1_imag = cv.polarToCart(magnitude1, phase1)
        result2_real, result2_imag = cv.polarToCart(magnitude2, phase2)
        # print(magnitude)
        # print(phase)
        trans1.real = result1_real
        trans1.imag = result1_imag
        trans2.real = result2_real
        trans2.imag = result2_imag
        # print(trans1)

        #反傅里叶变换
        img_back = self.myIFFT(trans1 + trans2)
        cv.imwrite(OUTPUT_FFT, img_back)
Beispiel #3
0
    def tensor_apply(self, image):
        # Convert to grayscale
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # Apply Sober filter
        Sx = cv2.Sobel(image, cv2.CV_32F, dx=1, dy=0, ksize=3)
        Sy = cv2.Sobel(image, cv2.CV_32F, dx=0, dy=1, ksize=3)
        Sxx = cv2.multiply(Sx, Sx)
        Syy = cv2.multiply(Sy, Sy)
        Sxy = cv2.multiply(Sx, Sy)

        # Apply a box filter
        filter_size = self.config['TENSOR_FILTSIZE']
        # Sxx = cv2.boxFilter(Sxx, cv2.CV_32F, (filter_size, filter_size))
        # Syy = cv2.boxFilter(Syy, cv2.CV_32F, (filter_size, filter_size))
        # Sxy = cv2.boxFilter(Sxy, cv2.CV_32F, (filter_size, filter_size))
        Sxx = cv2.GaussianBlur(Sxx, (filter_size, filter_size), 0)
        Syy = cv2.GaussianBlur(Syy, (filter_size, filter_size), 0)
        Sxy = cv2.GaussianBlur(Sxy, (filter_size, filter_size), 0)

        # Eigenvalue
        tmp1 = Sxx + Syy
        tmp2 = Sxx - Syy
        tmp2 = cv2.multiply(tmp2, tmp2)
        tmp3 = cv2.multiply(Sxy, Sxy)
        tmp4 = np.sqrt(tmp2 + 4.0 * tmp3)
        lambda1 = 0.5 * (tmp1 + tmp4)  # biggest eigenvalue
        lambda2 = 0.5 * (tmp1 - tmp4)  # smallest eigenvalue

        # Coherency
        coherency = cv2.divide(lambda1 - lambda2, lambda1 + lambda2)
        coherency = np.fmax(np.fmin(coherency, 1.0), 0.0)

        # Orientation angle
        orientation_Angle = cv2.phase(Syy - Sxx,
                                      2.0 * Sxy,
                                      angleInDegrees=True)
        orientation_Angle = 0.5 * orientation_Angle

        # Result
        histbin = self.config['TENSOR_HISTBIN']
        tensor_result = np.zeros(histbin * len(self.H_points) *
                                 len(self.W_points))
        index = 0
        scale = 255  # a scale to normalize the output
        for i in self.H_points:
            for j in self.W_points:
                for k in np.linspace(0.0, 180.0, histbin, endpoint=False):
                    tmp1 = (orientation_Angle[i:i + self.H_size,
                                              j:j + self.W_size] >=
                            k) & (orientation_Angle[i:i + self.H_size,
                                                    j:j + self.W_size] <
                                  k + 180. / histbin)
                    if tmp1.any():
                        tensor_result[index] = np.sum(
                            coherency[i:i + self.H_size,
                                      j:j + self.W_size][tmp1])
                    index += 1

        return tensor_result / scale
Beispiel #4
0
def fft_compute_color(img_col, center=False):
    assert img_col.shape[0] == 3, "Should be color image"
    _, h, w = img_col.shape
    lims_list = []
    idx_list_ = []
    x_mag = np.zeros((3, h, w))
    x_phase = np.zeros((3, h, w))
    x_fft = np.zeros((6, h, w))

    for i in range(3):
        img = img_col[i]
        dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
        if center:
            dft = np.fft.fftshift(dft)
        mag = cv2.magnitude(dft[:, :, 0], dft[:, :, 1])
        idx = (mag == 0)
        mag[idx] = 1.
        magnitude_spectrum = np.log(mag)
        phase_spectrum = cv2.phase(dft[:, :, 0], dft[:, :, 1])
        x_mag[i] = magnitude_spectrum
        x_phase[i] = phase_spectrum

        x_fft[2 * i] = dft[:, :, 0]
        x_fft[2 * i + 1] = dft[:, :, 1]

        idx_list_.append(idx)

    return x_fft, x_mag, x_phase, idx_list_
def calc_phase(img):
    max = np.max(img)
    img = np.sqrt(img / float(np.max(img)))
    gradient_values_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
    gradient_values_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
    gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5,
                                         gradient_values_y, 0.5, 0)
    gradient_angle = cv2.phase(gradient_values_x,
                               gradient_values_y,
                               angleInDegrees=True)

    bin_size = 16
    angle_unit = 360 / bin_size
    orientation_centers = [0] * bin_size

    for k in range(img.shape[0]):
        for l in range(img.shape[1]):
            gradient_strength = gradient_magnitude[k][l]
            g_angle = gradient_angle[k][l]
            min_angle = int(g_angle / angle_unit) % 16
            max_angle = (min_angle + 1) % bin_size
            mod = g_angle % angle_unit
            orientation_centers[min_angle] += (gradient_strength *
                                               (1 - (mod / angle_unit)))
            orientation_centers[max_angle] += (gradient_strength *
                                               (mod / angle_unit))
    center_angle = angle_unit * np.argmax(orientation_centers)
    return center_angle
Beispiel #6
0
def getGradientAngles(image):
    scale = 1
    delta = 0
    ddepth = cv2.CV_32FC1

    # see http://stackoverflow.com/questions/22381704/sobel-operator-for-gradient-angle
    # and http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#phase

    # X gradient
    xGradient = cv2.Sobel(image,
                          ddepth,
                          1,
                          0,
                          dst=None,
                          ksize=3,
                          scale=scale,
                          delta=delta)

    # Y gradient
    yGradient = cv2.Sobel(image,
                          ddepth,
                          0,
                          1,
                          dst=None,
                          ksize=3,
                          scale=scale,
                          delta=delta)

    gradientAngles = cv2.phase(xGradient,
                               yGradient,
                               angle=None,
                               angleInDegrees=True)

    return gradientAngles
Beispiel #7
0
def get_features(image, x, y, feature_width):

    features = np.array([])
    for row in range(x.shape[0]):
        box = image[x[row] - feature_width / 2:x[row] +
                    (feature_width + 1) / 2, y[row] -
                    feature_width / 2:y[row] + (feature_width + 1) / 2]

    columns = 0
    rows = 0
    phase_histogram_information = []
    while columns < feature_width:
        while rows < feature_width:
            sobel_x = np.array([])
            sobel_y = np.array([])
            smaller_box = box[rows:(rows + (feature_width / 4)),
                              columns:(columns + (feature_width / 4))]

            # DO THE DERIVATIVE
            sobel_x = cv2.Sobel(smaller_box, -1, 1, 0)
            sobel_y = cv2.Sobel(smaller_box, -1, 0, 1)

            phase_array = cv2.phase(sobel_x, sobel_y)
            phase_histogram_information.append(np.histogram(phase_array, 8))

            rows = rows + feature_width / 4
        columns = columns + feature_width / 4

    # append these features together ; 128 dimensions
    features = np.append(features, phase_histogram_information)
Beispiel #8
0
    def __GetGradientInfo(self, image):
        """
        Get the image gradient, the gradient magnitude and the gradient
        directions from an input image.
        """
        # Create the output variable.
        gradient = [np.zeros(image.shape[:2])] * 2
        orientation = np.zeros(image.shape[:2])
        magnitude = np.zeros(image.shape[:2])

        # Grayscale image.
        grayscale = image.copy()
        if len(grayscale.shape) == 3:
            grayscale = cv2.cvtColor(grayscale, cv2.COLOR_BGR2GRAY)

        #<!--------------------------------------------------------------------------->
        #<!--                            YOUR CODE HERE                             -->
        #<!--------------------------------------------------------------------------->
        sobelx = cv2.Sobel(grayscale, cv2.CV_64F, 1, 0, ksize=5)
        sobely = cv2.Sobel(grayscale, cv2.CV_64F, 0, 1, ksize=5)

        gradient = np.gradient(grayscale)
        magnitude = cv2.magnitude(sobelx, sobely)
        orientation = cv2.phase(sobelx, sobely, angleInDegrees=True)

        #<!--------------------------------------------------------------------------->
        #<!--                                                                       -->
        #<!--------------------------------------------------------------------------->

        # Return the final result.
        return gradient, orientation, magnitude
Beispiel #9
0
def get_phase_and_magnitude(img, sobel_kernel_size=7, magnitude_power=0.3):
    """
    Calculate phase/rotation angle from image gradient
    :param img: image to compute phase from
    :param sobel_kernel_size:
    :return: phase in float32 radian
    """
    # grayify
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype('float32')

    # gradient (along x and y axis)
    xg = cv2.Sobel(img_gray, cv2.CV_32F, 1, 0, ksize=sobel_kernel_size)
    yg = -cv2.Sobel(img_gray, cv2.CV_32F, 0, 1, ksize=sobel_kernel_size)

    # calculates the rotation angle of the 2D vectors gradients
    phase = cv2.phase(xg, yg)

    # calculates the magnitude of the 2D vectors gradients
    magnitude = cv2.magnitude(xg, yg)
    magnitude = magnitude / magnitude.max()  # normalize to [0, 1] range

    # make magnitude more uniform
    magnitude = np.power(magnitude, magnitude_power)

    return phase, magnitude
Beispiel #10
0
def get_hog(img):
    n, m = img.shape

    Ix = cv.Sobel(img, cv.CV_64F, 1, 0, ksize=5)
    Iy = cv.Sobel(img, cv.CV_64F, 0, 1, ksize=5)
    grad_magnitude = cv.addWeighted(Ix, 0.5, Iy, 0.5, 0)

    # Threshold
    for i in range(grad_magnitude.shape[0]):
        for j in range(grad_magnitude.shape[1]):
            if grad_magnitude[i, j] < 0:
                grad_magnitude[i, j] = 0
    gradient_angle = cv.phase(Ix, Iy, angleInDegrees=True)

    cell_size = 8
    bin_size = 6
    n = n // cell_size * cell_size
    m = m // cell_size * cell_size

    hog_mag = np.zeros((n // cell_size, m // cell_size, bin_size))

    # Get m x n x 6, 3D array store each
    for i in range(hog_mag.shape[0]):
        for j in range(hog_mag.shape[1]):
            cell_magnitude = grad_magnitude[i * cell_size:(i + 1) * cell_size,
                                            j * cell_size:(j + 1) * cell_size]
            cell_angle = gradient_angle[i * cell_size:(i + 1) * cell_size,
                                        j * cell_size:(j + 1) * cell_size]
            hog_mag[i][j] = cell_gradient(cell_magnitude, cell_angle)
    return hog_mag
def compute_simple_hog(img_color: numpy.ndarray, keypoints: list,
                       name: str) -> numpy.ndarray:
    """
    Computes a histogram of gradients over defined keypoints.

    Args:
        img_color (numpy.ndarray): image in RBG
        keypoints (list): a list of keypoints for which HOG will be calculated
        name (str): name of the image

    Returns:
        numpy.ndarray: an array of descriptors for each keypoint;
                       shape: (number_of_keypoints×number_of_bins)
    """

    # convert color to gray image and extract feature in gray
    img_gray = cv.cvtColor(img_color, cv.COLOR_BGR2GRAY)

    # compute x and y gradients (sobel kernel size 5)
    sobel_x64f = cv.Sobel(img_gray, cv.CV_64F, dx=1, dy=0, ksize=5)
    sobel_y64f = cv.Sobel(img_gray, cv.CV_64F, dx=0, dy=1, ksize=5)

    # compute magnitude and angle of the gradients
    magnitudes = cv.magnitude(sobel_x64f, sobel_y64f)
    angles = cv.phase(sobel_x64f, sobel_y64f)  # in radians

    # go through all keypoints and compute feature vector
    descr = np.zeros((len(keypoints), 8), np.float32)
    count = 0
    for kp in keypoints:
        # print kp.pt, kp.size
        # extract angle in keypoint sub window
        # extract gradient magnitude in keypoint subwindow
        print("Keypoint coordinates: ", kp.pt)
        print("Keypoint size: ", kp.size)
        j, i = np.uint8(kp.pt)
        radius = np.uint8(kp.size / 2)
        kp_magnitudes = magnitudes[i - radius:i + radius + 1, j - radius:j +
                                   radius + 1]  # shape: (11, 11)
        kp_angles = angles[i - radius:i + radius + 1,
                           j - radius:j + radius + 1]

        # create histogram of angle in subwindow BUT only where magnitude of gradients is non zero!
        # Why? Find an answer to that question:
        # zero magnitude means zero gradients means no change in the color
        # cv.phaze() will return 0 for such zero vectors
        # this will shift the values for the first bin [0, b)
        # therefore we want to exlude such vectors from the histogram
        kp_angles = kp_angles[kp_magnitudes > 0.0]
        (hist, bins) = np.histogram(kp_angles,
                                    bins=8,
                                    range=(0, 2 * np.pi),
                                    density=True)

        plot_histogram(hist, bins, name)

        descr[count] = hist

    return descr
Beispiel #12
0
def gethog(img):
    img = np.sqrt(img / float(np.max(img)))
    height, width = img.shape
    gradient_values_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
    gradient_values_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
    gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)
    gradient_angle = cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)
    print(gradient_magnitude.shape, gradient_angle.shape)
Beispiel #13
0
def calc_gradient(img):
    gradient_values_x = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=5)
    cv2.convertScaleAbs(gradient_values_x)
    gradient_values_y = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=5)
    cv2.convertScaleAbs(gradient_values_y)
    gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)
    gradient_angle = cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)
    return gradient_magnitude, gradient_angle
Beispiel #14
0
def gradient_orien(gray_image):
    dx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=3)
    dy = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=3)
    orien = cv2.phase(np.array(dx, np.float32),
                      np.array(dy, np.float32),
                      angleInDegrees=True)
    # mag = cv2.magnitude(dx, dy)
    return orien
    def gradient(self):
        self.img = np.sqrt(self.img / np.max(self.img))  # gamma normalization

        dx = cv2.Sobel(self.img, cv2.CV_64F, 1, 0, ksize=5)
        dy = cv2.Sobel(self.img, cv2.CV_64F, 0, 1, ksize=5)
        magnitude = cv2.addWeighted(dx, 0.5, dy, 0.5, 0)
        self.magnitude = abs(magnitude)
        self.phase = cv2.phase(dx, dy, angleInDegrees=True)
Beispiel #16
0
 def global_gradient(self):
     gradient_values_x = cv2.Sobel(self.img, cv2.CV_64F, 1, 0, ksize=5)
     #print (gradient_values_x.shape)
     gradient_values_y = cv2.Sobel(self.img, cv2.CV_64F, 0, 1, ksize=5)
     #print (gradient_values_y.shape)
     gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)
     gradient_angle = cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)
     return gradient_magnitude, gradient_angle
Beispiel #17
0
def get_gradient(src):

    sobelx = cv2.Sobel(src, cv2.CV_64F, 1, 0, ksize=31)
    sobely = cv2.Sobel(src, cv2.CV_64F, 0, 1, ksize=31)

    grad = num.sqrt(sobelx**2 + sobely**2)
    mag = cv2.magnitude(sobelx, sobely)
    ori = cv2.phase(sobelx, sobely, True)
    return grad, mag, ori
Beispiel #18
0
def idft_output_max(dft_shift, percentage=None):
    f_spec = spectrum_biggest_values(dft_shift, percentage)

    f_ishift = np.fft.ifftshift(f_spec)
    img_back = cv2.idft(f_ishift, flags=cv2.DFT_SCALE)
    magnitude_spectrum = (cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]))
    phase_spectrum = (cv2.phase(img_back[:, :, 0], img_back[:, :, 1], True))

    return phase_spectrum, magnitude_spectrum
Beispiel #19
0
def sobel_ksize(x):
    if x % 2 == 0:
        x += 1
    sobelSize = x
    sobel = cv2.Sobel(gray, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=sobelSize)
    buf = sobel.copy()
    phase = cv2.phase(sobel, buf)
    cv2.imshow("Gradient", phase)
    cv2.imshow("SOBEL", sobel)
Beispiel #20
0
def HOGCalc(img, cell_width, n):
    interval = 180 / n
    half_cell_width = cell_width / 2
    eps = 0.001
    (dx, dy) = Gradient(img)
    mag = sqrt(dx * dx + dy * dy)
    ang = cv2.phase(dx, dy, angleInDegrees=True) % 180
    size_y, size_x = img.shape
    # bins = zeros((n,size_y,size_x))
    bin_id = (floor(ang / interval) % n).astype(int16)
    hog = zeros((int(floor(size_y / cell_width)) + 2,
                 int(floor(size_x / cell_width)) + 2, n))
    for i in arange(size_y):

        for j in arange(size_x):
            cell_i = int(floor((i - half_cell_width) / cell_width)) + 1
            cell_j = int(floor((j - half_cell_width) / cell_width)) + 1
            cell_id = bin_id[i][j]
            cell_id2 = (cell_id + 1) % n
            #Calculate HOG in adjacent cells and bins
            w_i = (i / cell_width - cell_i) % 1
            w_j = (j / cell_width - cell_j) % 1

            w_id = ang[i][j] / interval - cell_id
            # print("%f,%f,%f\n"%(w_i,w_j,w_id))
            hog[cell_i, cell_j,
                cell_id] = hog[cell_i, cell_j, cell_id] + mag[i][j] * (
                    1 - w_i) * (1 - w_j) * (1 - w_id)
            hog[cell_i, cell_j,
                cell_id2] = hog[cell_i, cell_j, cell_id2] + mag[i][j] * (
                    1 - w_i) * (1 - w_j) * (w_id)
            hog[cell_i + 1, cell_j,
                cell_id] = hog[cell_i + 1, cell_j, cell_id] + mag[i][j] * (
                    w_i) * (1 - w_j) * (1 - w_id)
            hog[cell_i + 1, cell_j,
                cell_id2] = hog[cell_i + 1, cell_j, cell_id2] + mag[i][j] * (
                    w_i) * (1 - w_j) * (w_id)
            hog[cell_i, cell_j + 1,
                cell_id] = hog[cell_i, cell_j + 1, cell_id] + mag[i][j] * (
                    1 - w_i) * (w_j) * (1 - w_id)
            hog[cell_i, cell_j + 1,
                cell_id2] = hog[cell_i, cell_j + 1, cell_id2] + mag[i][j] * (
                    1 - w_i) * (w_j) * (w_id)
            hog[cell_i + 1, cell_j + 1,
                cell_id] = hog[cell_i + 1, cell_j + 1, cell_id] + mag[i][j] * (
                    w_i) * (w_j) * (1 - w_id)
            hog[cell_i + 1, cell_j + 1,
                cell_id2] = hog[cell_i + 1, cell_j + 1,
                                cell_id2] + mag[i][j] * (w_i) * (w_j) * (w_id)
    hog1 = zeros(hog.shape, float)
    for i in arange(1, hog.shape[0] - 2):
        for j in arange(1, hog.shape[1] - 2):
            blk_norm = sqrt(
                sum(hog[i:i + 2, j:j + 2].reshape(4 * n)**2) + eps**2)
            hog1[i:i + 2, j:j + 2, :] = hog[i:i + 2, j:j + 2, :] / blk_norm
    return hog1[1:-1, 1:-1]
def transform(img, visualize):
    # Uses discrete fourier transformation
    dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)

    # Shifts image to center
    dft_center = np.fft.fftshift(dft)

    # Gets magnitude and phase
    if visualize:
        # Adjust values to visualization
        magnitude = 20 * np.log(
            cv2.magnitude(dft_center[:, :, 0], dft_center[:, :, 1]))
        phase = 40 * np.log(
            cv2.phase(dft_center[:, :, 0], dft_center[:, :, 1], True))
    else:
        magnitude = cv2.magnitude(dft_center[:, :, 0], dft_center[:, :, 1])
        phase = cv2.phase(dft_center[:, :, 0], dft_center[:, :, 1], True)

    return magnitude, phase
Beispiel #22
0
def matGradient(mat: np.ndarray):
    matGrayscale = matToGrayscale(mat)

    dx = cv.Sobel(matGrayscale, cv.CV_64F, 1, 0)
    dy = cv.Sobel(matGrayscale, cv.CV_64F, 0, 1)

    magnitude = cv.magnitude(dx, dy)
    phase = cv.phase(dx, dy)

    return magnitude, phase
Beispiel #23
0
def fft_compute(img, center=False):
    dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
    if center:
        dft = np.fft.fftshift(dft)
    mag = cv2.magnitude(dft[:, :, 0], dft[:, :, 1])
    idx = (mag == 0)
    mag[idx] = 1.
    magnitude_spectrum = np.log(mag)
    phase_spectrum = cv2.phase(dft[:, :, 0], dft[:, :, 1])
    return dft, magnitude_spectrum, phase_spectrum, idx
def sobel(image):
    sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
    sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
    abs_grad_x = cv2.convertScaleAbs(sobelx)
    abs_grad_y = cv2.convertScaleAbs(sobely)

    grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
    phase = cv2.phase(sobelx, sobely, angleInDegrees=False)

    return grad, phase
Beispiel #25
0
 def global_gradient(self):
     '''
     获取全局的梯度
     包括梯度值和梯度方向
     '''
     gradient_values_x = cv2.Sobel(self.img, cv2.CV_64F, 1, 0, ksize=5)
     gradient_values_y = cv2.Sobel(self.img, cv2.CV_64F, 0, 1, ksize=5)
     gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)
     gradient_angle = cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)
     return gradient_magnitude, gradient_angle
Beispiel #26
0
    def compute_gradient(self, img):
        filtered_img = cv2.GaussianBlur(img, (3, 3), 0)

        norm_factor = 32
        gradx = cv2.Scharr(filtered_img, cv2.CV_32F, 1, 0, scale=1.0 / norm_factor)
        grady = cv2.Scharr(filtered_img, cv2.CV_32F, 0, 1, scale=1.0 / norm_factor)

        theta = np.zeros_like(gradx)

        ones_zeros = np.zeros_like(theta)
        ones_zeros[::2] = 1
        zeros_ones = np.ones_like(theta)
        zeros_ones[::2] = 0

        cv2.phase(gradx, grady, theta)

        gradx = gradx.flatten()
        grady = grady.flatten()
        theta = theta.flatten()

        cosTheta = np.cos(theta)
        sinTheta = np.sin(theta)

        sin_cos = -sinTheta * gradx - cosTheta * grady
        cos_sin = cosTheta * gradx - sinTheta * grady

        c = np.empty((cosTheta.size + sinTheta.size,), dtype=cosTheta.dtype)
        c[0::2] = sin_cos
        c[1::2] = cos_sin

        jacobian = np.array([ones_zeros, zeros_ones, c])
        # input("regthrb")

        # jacobian = np.array(
        #     [
        #         ones,
        #         gradx * sinTheta - gradx * cosTheta,
        #         -grady - grady * cosTheta,
        #     ]
        # )
        print(jacobian.transpose())
        # gradient = np.vstack([theta, gradx, grady])
        return jacobian.transpose()
Beispiel #27
0
def calc_gradient(img):
	gradient_values_x = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=5)
	cv2.convertScaleAbs(gradient_values_x)

	gradient_values_y = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=5)
	cv2.convertScaleAbs(gradient_values_y)

	gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)
	gradient_angle = cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)
	return gradient_magnitude, gradient_angle
Beispiel #28
0
def applySobel(gray):

    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
    abs_sobelx = np.absolute(sobelx)
    abs_sobely = np.absolute(sobely)
    magnitude = abs_sobelx + abs_sobely

    angle = cv2.phase(sobelx, sobely, angleInDegrees=True)

    return magnitude, angle.mean()
Beispiel #29
0
 def gradient(self, image=None):
     if image is None:
         gray = self.gray
     else:
         gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
     gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
     gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
     angles = cv2.phase(gx, gy)
     gradient = np.sqrt(np.power(gx, 2) + np.power(gy, 2))
     gradient = cv2.convertScaleAbs(gradient)
     return gradient, angles
Beispiel #30
0
 def calc_ang(x):
     gx = np.float32(sobel_vert)
     gy = np.float32(sobel_horz)
     phase = cv2.phase(gy, gx, angleInDegrees=True)
     rows, cols = phase.shape
     img = copy.deepcopy(sobel)
     for i in range(rows):
         for j in range(cols):
             if not (phase[i][j] > (x - 10) and phase[i][j] <
                     (x + 10)):
                 img[i][j] = 0
     cv2.imshow('Direction', img)
Beispiel #31
0
    def init_mag_angle(self):
        h, w = self.window.shape
        grad_y = cv2.Sobel(self.window, cv2.CV_64F, dx=0, dy=1, ksize=5)
        grad_x = cv2.Sobel(self.window, cv2.CV_64F, dx=1, dy=0, ksize=5)

        self.grad_magnitude = abs(
            cv2.addWeighted(grad_x, 0.5, grad_y, 0.5, gamma=0.0))
        #self.grad_magnitude = cv2.magnitude(grad_x, grad_y)
        self.grad_angle = cv2.phase(grad_x, grad_y, angleInDegrees=True) % 180
        self.cell_grad_vector = np.zeros(
            (int(h / self.cell_size), int(w / self.cell_size),
             int(self.bin_size)))
Beispiel #32
0
    def generate_sobel_filtered_image(self, ksize):

        figure = plt.figure()
        figure.add_subplot(1, 1, 1)
        set_dpi = figure.get_dpi()
        filtered_im = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)

        start = time()

        if ksize == 90:
            filtered_im = cv2.Sobel(filtered_im, cv2.CV_64F, 1, 0,
                                    ksize=3)  # x

        elif ksize == 0:
            filtered_im = cv2.Sobel(filtered_im, cv2.CV_64F, 0, 1,
                                    ksize=3)  # y

        elif ksize == -45:
            kernel_neg_45 = np.array([[0, 1, 2], [-1, 0, 1], [-2, -1, 0]])
            filtered_im = cv2.filter2D(filtered_im, cv2.CV_8U, kernel_neg_45)

        elif ksize == 45:
            kernel_plus_45 = np.array([[-2, -1, 0], [-1, 0, 1], [0, 1, 2]])
            filtered_im = cv2.filter2D(filtered_im, cv2.CV_8U, kernel_plus_45)

        elif ksize == 999:
            sobelx = cv2.Sobel(filtered_im, cv2.CV_64F, 1, 0, ksize=3)  # x
            sobely = cv2.Sobel(filtered_im, cv2.CV_64F, 0, 1, ksize=3)  # y
            filtered_im = cv2.magnitude(sobelx, sobely)

        elif ksize == -999:
            sobelx = cv2.Sobel(filtered_im, cv2.CV_64F, 1, 0, ksize=3)  # x
            sobely = cv2.Sobel(filtered_im, cv2.CV_64F, 0, 1, ksize=3)  # y
            filtered_im = cv2.phase(sobelx, sobely)

        end = time()

        ptime = end - start

        figure.set_size_inches(self.image.shape[1] / set_dpi,
                               self.image.shape[0] / set_dpi)
        disp_fig = plt.imshow(filtered_im, cmap='gray')
        plt.axis('off')
        disp_fig.axes.get_xaxis().set_visible(False)
        disp_fig.axes.get_yaxis().set_visible(False)

        plt.savefig(self.plotpath,
                    bbox_inches='tight',
                    pad_inches=0,
                    dpi=set_dpi * 1.3)

        return round(ptime, 6)
def get_orientation_map(img):
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    xder = cv2.Sobel(gray_img, cv2.CV_64F, 1, 0)
    yder = cv2.Sobel(gray_img, cv2.CV_64F, 0, 1)

    orientation_map = cv2.phase(xder, yder)
    for i, row in enumerate(orientation_map):
        for j, _ in enumerate(row):
            orientation_map[i][j] += math.pi / 2
            orientation_map[i][j] %= math.pi

    return orientation_map
Beispiel #34
0
def vizMag(u, v):
    res = v**2 + u**2
    res2 = np.ones_like(u)
    res3 = cv2.phase(u, v)
    res = cv2.merge([res, res2, res3])
    res = cv2.cvtColor(res, cv2.COLOR_HSV2BGR)

    res = cv2.normalize(res, 0, 255, cv2.NORM_MINMAX)
    while res.shape[0] < 200:
        res = cv2.pyrUp(res)

    cv2.imshow('', res)
    cv2.waitKey(10)
    def gradient_map(self, images):
        gmaps = []
        for i, image in enumerate(images):
            sx = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=-1)
            sy = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=-1)

            mag = cv2.magnitude(sx, sy)
            ori = cv2.phase(sx, sy, angleInDegrees=True)

            gmaps.append((ori, mag))

        (gmap, weight) = self.calc_gradient_map(gmaps)
        return (gmap, weight)
Beispiel #36
0
def grad_dir(img):
    # compute x and y derivatives
    # OpenCV's Sobel operator gives better results than numpy gradient
    sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=-1)
    sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=-1)

    # calculate gradient direction angles
    # phase needs 64-bit input
    angle = cv2.phase(sobelx, sobely)

    # truncates number
    gradir = np.fix(180 + angle)

    return gradir
Beispiel #37
0
def __filter_candidate(greyscale_image, coord, neighborhood_size):
    window = greyscale_image[
        coord[0] - neighborhood_size : coord[0] + neighborhood_size + 1,
        coord[1] - neighborhood_size : coord[1] + neighborhood_size + 1,
    ]
    grad_x = cv2.Sobel(window, cv2.CV_32FC1, dx=1, dy=0, ksize=3)
    grad_y = cv2.Sobel(window, cv2.CV_32FC1, dx=0, dy=1, ksize=3)
    grad_mag = np.abs(grad_x) + np.abs(grad_y)
    grad_mag_flat = grad_mag.flatten()
    orientations_flat = (cv2.phase(grad_x, grad_y) % pi).flatten()  # phase accuracy: about 0.3 degrees
    hist = np.histogram(orientations_flat, bins=64, range=(0, pi), weights=grad_mag_flat)[0] / (
        neighborhood_size * neighborhood_size
    )

    return hist, grad_mag
def calcGST(inputIMG, w):
## [calcGST_proto]
    img = inputIMG.astype(np.float32)

    # GST components calculation (start)
    # J =  (J11 J12; J12 J22) - GST
    imgDiffX = cv.Sobel(img, cv.CV_32F, 1, 0, 3)
    imgDiffY = cv.Sobel(img, cv.CV_32F, 0, 1, 3)
    imgDiffXY = cv.multiply(imgDiffX, imgDiffY)
    ## [calcJ_header]

    imgDiffXX = cv.multiply(imgDiffX, imgDiffX)
    imgDiffYY = cv.multiply(imgDiffY, imgDiffY)

    J11 = cv.boxFilter(imgDiffXX, cv.CV_32F, (w,w))
    J22 = cv.boxFilter(imgDiffYY, cv.CV_32F, (w,w))
    J12 = cv.boxFilter(imgDiffXY, cv.CV_32F, (w,w))
    # GST components calculations (stop)

    # eigenvalue calculation (start)
    # lambda1 = J11 + J22 + sqrt((J11-J22)^2 + 4*J12^2)
    # lambda2 = J11 + J22 - sqrt((J11-J22)^2 + 4*J12^2)
    tmp1 = J11 + J22
    tmp2 = J11 - J22
    tmp2 = cv.multiply(tmp2, tmp2)
    tmp3 = cv.multiply(J12, J12)
    tmp4 = np.sqrt(tmp2 + 4.0 * tmp3)

    lambda1 = tmp1 + tmp4    # biggest eigenvalue
    lambda2 = tmp1 - tmp4    # smallest eigenvalue
    # eigenvalue calculation (stop)

    # Coherency calculation (start)
    # Coherency = (lambda1 - lambda2)/(lambda1 + lambda2)) - measure of anisotropism
    # Coherency is anisotropy degree (consistency of local orientation)
    imgCoherencyOut = cv.divide(lambda1 - lambda2, lambda1 + lambda2)
    # Coherency calculation (stop)

    # orientation angle calculation (start)
    # tan(2*Alpha) = 2*J12/(J22 - J11)
    # Alpha = 0.5 atan2(2*J12/(J22 - J11))
    imgOrientationOut = cv.phase(J22 - J11, 2.0 * J12, angleInDegrees = True)
    imgOrientationOut = 0.5 * imgOrientationOut
    # orientation angle calculation (stop)

    return imgCoherencyOut, imgOrientationOut
Beispiel #39
0
 def __getGradientImageInfo(self, I):
     # Use sobel on the x and y axis
     sobelX = cv2.Sobel(I, cv2.CV_64F, 1, 0)
     sobelY = cv2.Sobel(I, cv2.CV_64F, 0, 1)        
     
     # Arrays for holding information about orientation and magnitude og each gradient
     #orientation = np.zeros(I.shape)
     #magnitude = np.zeros(I.shape)
     
     # Calculate orientation and magnitude of each gradient
     #for x in range(I.shape[0]):
         #for y in range(I.shape[1]):
             #orientation[x][y] = np.arctan2(sobelY[x][y], sobelX[x][y]) * (180 / m.pi)
             #magnitude[x][y] = m.sqrt(sobelY[x][y] ** 2 + sobelX[x][y] ** 2)
     
     magnitude = cv2.magnitude(sobelX, sobelY)
     orientation = cv2.phase(sobelX, sobelY)        
     
     return sobelX, sobelY, magnitude, orientation
Beispiel #40
0
def plotArrows(u, v):
    assert u.shape == v.shape
    u = cv2.pyrDown(cv2.pyrDown(cv2.pyrDown(u)))
    v = cv2.pyrDown(cv2.pyrDown(cv2.pyrDown(v)))
    angles = cv2.phase(u, v)
    #print angles
    '''angles = np.array([
            [0.5, 0.1, 0.3],
            [0.5, 0.1, 0.3],
            [1.5, 1.1, 1.3]
        ])'''
    #plt.arrow( 0.5, 0.8, 0.0, -0.2, fc="k", ec="k", head_width=0.05, head_length=0.1 )
    plt.xlim([0, angles.shape[0] + 1])
    plt.ylim([angles.shape[1] + 1, 0])
    #plt.arrow( 0.9, 0.1, 0.0, 0.2, fc="k", ec="k", head_width=0.05, head_length=0.1 )
    for i in xrange(angles.shape[0]):
        for j in xrange(angles.shape[1]):
            ni = 1.0 * np.cos(angles[i, j])
            nj = 1.0 * np.sin(angles[i, j])
            plt.arrow( i, j, ni, nj, fc="k", ec="k", head_width=0.05, head_length=0.1 )

    plt.show()
def gradient_map(images):
    gmaps = []
    for i, image in enumerate(images):
        sx = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=-1)
        sy = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=-1)

        mag = cv2.magnitude(sx, sy)
        ori = cv2.phase(sx, sy, angleInDegrees=True)

        # cv2.imshow('image', ori)
        # cv2.waitKey(0)
        # cv2.imshow('orientation', ori)
        # cv2.waitKey(0)
        # cv2.imshow('magnitude', mag)
        # cv2.waitKey(0)

        # ori_map = orientation_map(mag, ori)
        # # norm_ori = ori / 360.0
        # # norm_ori = ori * 255
        gmaps.append((ori, mag))
        # # print(ori_map)

    (gmap, weight) = calculate_gradient_map(gmaps)
    return (gmap, weight)
 cenCol = int(circles[0,0,0])
 cenRad = int(circles[0,0,2])
 cen = np.array([cenRow, cenCol])
 dscalefac = np.float32(eyerad)/(cenRad)
 nreye = int(sizeye[0]/dscalefac)
 nceye = int(sizeye[1]/dscalefac)
 smaleyeimg = cv2.resize(releyeI, (nceye, nreye))
 smaleyeimgOut = cp.copy(smaleyeimg)
 
 impg = cp.copy(eye_det_gray_hres_heq_med11)
 sobelxf = cv2.Sobel(impg,cv2.CV_64F,1,0,ksize=11)
 sobelyf = cv2.Sobel(impg,cv2.CV_64F,0,1,ksize=11)
 #maxgrad = 40000
 maxgrad = np.max([sobelxf, sobelyf])
 mag = cv2.magnitude(sobelxf/maxgrad, sobelyf/maxgrad)
 tet = cv2.phase(sobelxf/maxgrad, sobelyf/maxgrad)
 prj = np.zeros((180), dtype = np.float16)
 for idx, theta in enumerate(np.linspace(0, np.pi, 180)):
     p = np.int16(np.round(np.array([cenRow-cenRad*np.sin(theta), cenCol+cenRad*np.cos(theta)])))
     prj[idx] = mag[p[0], p[1]]*np.cos(np.abs((2*np.pi - tet[p[0], p[1]]) - theta))                    
 #pr = np.nonzero(prj<0.1)
 #numAngles = np.shape(pr)[1]
 prjext = np.hstack((np.tile(prj[0], 11), prj, np.tile(prj[179], 11)))
 y3 = sp.signal.convolve(prjext, (np.float16(1)/9)*np.ones((9,)))
 y4 = y3[15:210-15]
 y5 = y4[1:] - y4[0:179]
 zero_crossings = np.where(np.diff(np.sign(y5)))[0]
 y6 = y5[1:] - y5[0:178]
 #y7 = np.hstack((np.tile(y6[0], 5), y6, np.tile(y6[177], 5)))
 #y8 = sp.signal.convolve(y7, (np.float16(1)/5)*np.ones((5,)))
 #y9 = y8[7:192-7]
Beispiel #43
0
 def global_gradient(self):
     gradient_values_x = cv2.Sobel(self.img, cv2.CV_64F, 1, 0, ksize=5)
     gradient_values_y = cv2.Sobel(self.img, cv2.CV_64F, 0, 1, ksize=5)
     gradient_magnitude = cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)
     gradient_angle = cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)
     return gradient_magnitude, gradient_angle()
Beispiel #44
0
def mainpartickletrack(orig_img):
		global oldxrvals
		global oldxlvals
		#orig_img=orig_img[0:-300,:,:]
		# Scale down the image - Just for better display.
		orig_height,orig_width=orig_img.shape[:2]
		# orig_img=cv2.resize(orig_img,(orig_width/2,orig_height/2),interpolation = cv2.INTER_CUBIC)
		# orig_height,orig_width=orig_img.shape[:2]
		# Part of the image to be considered for lane detection
		upper_threshold=0.4
		lower_threshold=0.2
		# Copy the part of original image to temporary image for analysis.
		img=orig_img[int(upper_threshold*orig_height):int((1- lower_threshold)*orig_height),:]
		#img=orig_img.copy()
		# Convert temp image to GRAY scale
		img=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
		height,width=img.shape[:2]
		# Image processing to extract better information form images.
		# Adaptive Biateral Filter:
		#img = cv2.adaptiveBilateralFilter(img,ksize=(5,5),sigmaSpace=2)
		# Equalize the histogram to account for better contrast in the images.
		#img = cv2.equalizeHist(img);
		# Apply Canny Edge Detector to detect the edges in the image.
		bin_img = cv2.Canny(img,30,60,apertureSize = 5)
		cv2.imshow('edges',bin_img)
		#Thresholds for lane detection. Emperical values, detected from trial and error.
		xl_low = int(-1*orig_width) # low threshold for left x_intercept
		xl_high = int(0.8*orig_width) # high threshold for left x_intercept
		xr_low = int(0.2*orig_width)  # low threshold for right x_intercept
		xr_high = int(2*orig_width) # high threshold for right x_intercept
		xl_phase_threshold = 15  # Minimum angle for left x_intercept
		xr_phase_threshold = 14  # Minimum angle for right x_intercept
		xl_phase_upper_threshold = 80  # Maximum angle for left x_intercept
		xr_phase_upper_threshold = 80  # Maximum angle for right x_intercept

		# Arrays/Containers for intercept values and phase angles.
		xl_arr = np.zeros(xl_high-xl_low)
		xr_arr = np.zeros(xr_high-xr_low)
		xl_phase_arr = []
		xr_phase_arr = []
		# Intercept Bandwidth: Used to assign weights to neighboring pixels.
		intercept_bandwidth = 6

		# Run Probabilistic Hough Transform to extract line segments from Binary image.
		lines=cv2.HoughLinesP(bin_img,rho=1,theta=np.pi/180,threshold=10,minLineLength=10,maxLineGap=200)

		# Loop for every single line detected by Hough Transform
		# print len(lines[0])
		if(lines!=None):
			for x1,y1,x2,y2 in lines[0]:
				if(x1<x2 and y1>y2):
					norm = cv2.norm(float(x1-x2),float(y1-y2))
					phase = cv2.phase(np.array(x2-x1,dtype=np.float32),np.array(y1-y2,dtype=np.float32),angleInDegrees=True)
					# if(phase<xl_phase_threshold or phase > xl_phase_upper_threshold or x1 > 0.5 * orig_width): #Filter out the noisy lines
					#     continue
					xl = int(x2 - (height+lower_threshold*orig_height-y2)/np.tan(phase*np.pi/180))
					# Show the Hough Lines
					# cv2.line(orig_img,(x1,y1+int(orig_height*upper_threshold)),(x2,y2+int(orig_height*upper_threshold)),(0,0,255),2)

					# If the line segment is a lane, get weights for x-intercepts
					try:
						for i in range(xl - intercept_bandwidth,xl + intercept_bandwidth):
							xl_arr[i-xl_low] += (norm**0.5)*y1*(1 - float(abs(i - xl))/(2*intercept_bandwidth))*(phase**2)
					except IndexError:
						# print "Debug: Left intercept range invalid:", xl
						continue
					xl_phase_arr.append(phase[0][0])

				elif(x1<x2 and y1<y2):
					norm = cv2.norm(float(x1-x2),float(y1-y2))
					phase = cv2.phase(np.array(x2-x1,dtype=np.float32),np.array(y2-y1,dtype=np.float32),angleInDegrees=True)
					# if(phase<xr_phase_threshold or phase > xr_phase_upper_threshold or x2 < 0.5 * orig_width): #Filter out the noisy lines
					#     continue
					xr = int(x1 + (height+lower_threshold*orig_height-y1)/np.tan(phase*np.pi/180))
					# Show the Hough Lines
					# cv2.line(orig_img,(x1,y1+int(orig_height*upper_threshold)),(x2,y2+int(orig_height*upper_threshold)),(0,0,255),2)
					# If the line segment is a lane, get weights for x-intercepts
					try:
						for i in range(xr - intercept_bandwidth,xr + intercept_bandwidth):
							xr_arr[i-xr_low] += (norm**0.5)*y2*(1 - float(abs(i - xr))/(2*intercept_bandwidth))*(phase**2)
					except IndexError:
						# print "Debug: Right intercept range invalid:", xr
						continue
					xr_phase_arr.append(phase[0][0])
				else:
					pass # Invalid line - Filter out orizontal and other noisy lines.

			# Sort the phase array and get the best estimate for phase angle.
			try:
				xl_phase_arr.sort()
				if(len(xl_phase_arr)==0):
					xl_phase=[]
				else:
					xl_phase =  xl_phase_arr[-1] if (xl_phase_arr[-1] < np.mean(xl_phase_arr) + np.std(xl_phase_arr)) else np.mean(xl_phase_arr) + np.std(xl_phase_arr)
			except IndexError:
				# print "Debug: ", fname + " has no left x_intercept information"
				pass
			try:
				xr_phase_arr.sort()
				if(len(xr_phase_arr)==0):
					xr_phase=[]
				else:
					xr_phase =  xr_phase_arr[-1] if (xr_phase_arr[-1] < np.mean(xr_phase_arr) + np.std(xr_phase_arr)) else np.mean(xr_phase_arr) + np.std(xr_phase_arr)
			except IndexError:
				# print "Debug: ", fname + " has no right x_intercept information"
				pass

			# Get the index of x-intercept (700 is for positive numbers for particle filter.)
			pos_int = np.argmax(xl_arr)+xl_low+700
			# Apply Particle Filter.
			xl_int = xl_int_pf.filterdata(data=pos_int)
			if(str(xl_phase)!='[]'):
				xl_phs = xl_phs_pf.filterdata(data=xl_phase)
				oldxlvals=xl_phs.astype(np.int)
			else:
				xl_phs=oldxlvals
				# Draw lines for display
			cv2.line(orig_img,
					(int(xl_int-700), orig_height),
					(int(xl_int-700) + int(orig_height*0.3/np.tan(xl_phs*np.pi/180)),int(0.7*orig_height)),(0,255,255),2)
			# Apply Particle Filter.
			xr_int = xr_int_pf.filterdata(data=np.argmax(xr_arr)+xr_low)
			if(str(xr_phase)!='[]'):
				xr_phs = xr_phs_pf.filterdata(data=xr_phase)
				oldxrvals=xr_phs.astype(np.int)
			else:
				xr_phs=oldxrvals
			# Draw lines for display
			cv2.line(orig_img,
					(int(xr_int), orig_height),
					(int(xr_int) - int(orig_height*0.3/np.tan(xr_phs*np.pi/180)),int(0.7*orig_height)),(0,255,255),2)

			# print "Degbug: %5d\t %5d\t %5d\t %5d %s"%(xl_int-700,np.argmax(xl_arr)+xl_low,xr_int,np.argmax(xr_arr)+xr_low,fname)
			# intercepts.append((os.path.basename(fname), xl_int[0]-700, xr_int[0]))

			# Show image
		cv2.imshow('Lane Markers', orig_img)
        xl_arr = np.zeros(xl_high-xl_low)
        xr_arr = np.zeros(xr_high-xr_low)
        xl_phase_arr = []
        xr_phase_arr = []
        # Intercept Bandwidth: Used to assign weights to neighboring pixels.
        intercept_bandwidth = 6

        # Run Probabilistic Hough Transform to extract line segments from Binary image.
        lines=cv2.HoughLinesP(bin_img,rho=1,theta=np.pi/180,threshold=30,minLineLength=20,maxLineGap=5)

        # Loop for every single line detected by Hough Transform
        # print len(lines[0])
        for x1,y1,x2,y2 in lines[0]:
            if(x1<x2 and y1>y2 and x1 < 0.6*width  and x2 > 0.2*width):
                norm = cv2.norm(float(x1-x2),float(y1-y2))
                phase = cv2.phase(np.array(x2-x1,dtype=np.float32),np.array(y1-y2,dtype=np.float32),angleInDegrees=True)
                if(phase<xl_phase_threshold or phase > xl_phase_upper_threshold or x1 > 0.5 * orig_width): #Filter out the noisy lines
                    continue
                xl = int(x2 - (height+lower_threshold*orig_height-y2)/np.tan(phase*np.pi/180))
                # Show the Hough Lines           
                # cv2.line(orig_img,(x1,y1+int(orig_height*upper_threshold)),(x2,y2+int(orig_height*upper_threshold)),(0,0,255),2)

                # If the line segment is a lane, get weights for x-intercepts
                try:
                    for i in range(xl - intercept_bandwidth,xl + intercept_bandwidth):
                        xl_arr[i-xl_low] += (norm**0.5)*y1*(1 - float(abs(i - xl))/(2*intercept_bandwidth))*(phase**2)
                except IndexError:
                    # print "Debug: Left intercept range invalid:", xl
                    continue
                xl_phase_arr.append(phase[0][0])