Example #1
0
def calculate_sky_energy(border, src_image):

    # 制作天空图像掩码和地面图像掩码
    sky_mask = make_sky_mask(src_image, border, 1)
    ground_mask = make_sky_mask(src_image, border, 0)

    # 扣取天空图像和地面图像
    sky_image_ma = np.ma.array(src_image, mask = cv2.cvtColor(sky_mask, cv2.COLOR_GRAY2BGR))
    ground_image_ma = np.ma.array(src_image, mask = cv2.cvtColor(ground_mask, cv2.COLOR_GRAY2BGR))

    # 计算天空和地面图像协方差矩阵
    sky_image = sky_image_ma.compressed()
    ground_image = ground_image_ma.compressed()

    sky_image.shape = (sky_image.size//3, 3)
    ground_image.shape = (ground_image.size//3, 3)

    sky_covar, sky_mean = cv2.calcCovarMatrix(sky_image, mean=None, flags=cv2.COVAR_ROWS|cv2.COVAR_NORMAL|cv2.COVAR_SCALE)
    sky_retval, sky_eig_val, sky_eig_vec = cv2.eigen(sky_covar)

    ground_covar, ground_mean = cv2.calcCovarMatrix(ground_image, mean=None,flags=cv2.COVAR_ROWS|cv2.COVAR_NORMAL|cv2.COVAR_SCALE)
    ground_retval, ground_eig_val, ground_eig_vec = cv2.eigen(ground_covar)

    gamma = 2  # 论文原始参数

    sky_det = cv2.determinant(sky_covar)
    #sky_eig_det = cv2.determinant(sky_eig_vec)
    ground_det = cv2.determinant(ground_covar)
    #ground_eig_det = cv2.determinant(ground_eig_vec)

    sky_energy = 1 / ((gamma * sky_det + ground_det) + (gamma * sky_eig_val[0,0] + ground_eig_val[0,0]))

    return sky_energy
Example #2
0
def calcJm(img, H):
    h, w, c = img.shape
    skysample = np.zeros((sum(H), 3))
    gndsample = np.zeros((w * h - sum(H), 3))
    ks = 0
    kg = 0
    for x in range(w):
        for y in range(H[x]):
            skysample[ks, 0] = img[y, x, 0]
            skysample[ks, 1] = img[y, x, 1]
            skysample[ks, 2] = img[y, x, 2]
            ks = ks + 1
        for y in range(h - H[x]):
            gndsample[kg, 0] = img[y + H[x], x, 0]
            gndsample[kg, 1] = img[y + H[x], x, 1]
            gndsample[kg, 2] = img[y + H[x], x, 2]
            kg = kg + 1
    skycov, skymean = cv2.calcCovarMatrix(skysample,
                                          cv2.COVAR_ROWS | cv2.COVAR_NORMAL)
    gndcov, gndmean = cv2.calcCovarMatrix(gndsample,
                                          cv2.COVAR_ROWS | cv2.COVAR_NORMAL)
    skyret, skyeigval, skyeigvec = cv2.eigen(skycov, 1)
    gndret, gndeigval, gndeigvec = cv2.eigen(gndcov, 1)
    skyeigvalsum = sum(skyeigval)
    gndeigvalsum = sum(gndeigval)
    skydet = cv2.determinant(skycov)
    gnddet = cv2.determinant(gndcov)
    Jm = 1.0 / (skydet + gnddet + skyeigvalsum * skyeigvalsum +
                gndeigvalsum * gndeigvalsum)
    return (Jm)
Example #3
0
def calculate_pca(contour):
    # Re-organize data set (Matrix X = rowvector n x p variabels)
    contour = np.reshape(contour, (contour.shape[0], contour.shape[2]))

    # Calculate d-dimensional empirical mean vector
    mean_x = np.mean(contour[:, 0])
    mean_y = np.mean(contour[:, 1])
    mean_vec = np.array([[mean_x], [mean_y]]).reshape(1, -1)

    # Calculate deviations from mean
    mean_dev = []
    for row in contour:
        mean_dev.append(row - mean_vec)

    mean_dev = np.asarray(mean_dev)
    mean_dev = np.reshape(mean_dev, (mean_dev.shape[0], mean_dev.shape[2]))

    # Get the covariance matrix
    cov_mat = np.cov([mean_dev[:, 0], mean_dev[:, 1]])

    # Find eigenvectors and eigenvalues of covariance matrix
    mean, eigenvectors = cv2.PCACompute(cov_mat, mean_vec)
    retval, eigenvalues, eigenvectors2 = cv2.eigen(cov_mat)
    eig_val_cov, eig_vec_cov = np.linalg.eig(cov_mat)

    # Pair vectors and values and sort desc (get biggest first)
    eig_pairs = [(np.abs(eig_val_cov[i]), eig_vec_cov[:, i])
                 for i in range(len(eig_val_cov))]
    eig_pairs.sort(key=lambda x: x[0], reverse=True)
    eig_pairs = eig_pairs[0:2]

    return eig_pairs, mean_vec
Example #4
0
def task_4_a():
    print("Task 4 (a) ...")
    W = np.array([[0, 1, 0.2, 1, 0, 0, 0, 0], [1, 0, 0.1, 0, 1, 0, 0, 0],
                  [0.2, 0.1, 0, 1, 0, 1, 0.3, 0], [1, 0, 1, 0, 0, 1, 0, 0],
                  [0, 1, 0, 0, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0, 1, 0],
                  [0, 0, 0.3, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 0]])
    D_vector = np.sum(W, axis=0)
    # a)
    D = np.diag(D_vector)
    D_sqrt = np.diag(np.sqrt(D_vector))
    D_inv_sqrt = np.diag(1 / np.sqrt(D_vector))
    _, eigen_values, eigen_vectors = cv.eigen(
        np.dot(np.dot(D_inv_sqrt, D - W), D_inv_sqrt))
    second_smallest = eigen_vectors[-2]
    y = np.dot(D_sqrt, second_smallest
               )  # this is the generalized eigen vector of (D-W)y=lambda D y
    # b)
    names = np.array(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
    C_1 = names[y >= 0]
    C_2 = names[y < 0]
    print('C_1:', C_1)
    print('C_2:', C_2)

    # Instead of counting the edges etc. we are using the reformulated version
    normalized_cut_value = np.dot(np.dot(y, (D - W)), y) / (np.dot(
        np.dot(y, W), y))
    print('Normalized cut value:', normalized_cut_value)
Example #5
0
def harrisdetector(image, k, t):
    ptr_x = []
    ptr_y = []

    kernel = np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]])
    Ix = cv2.filter2D(image, -1, kernel)
    kernel = np.array([[0, -1, 0], [0, 0, 0], [0, 1, 0]])
    Iy = cv2.filter2D(image, -1, kernel)

    XX = Ix * Ix
    XY = Ix * Iy
    YY = Iy * Iy

    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            x1 = i - k if i - k >= 0 else 0
            x2 = i + k + 1 if i + k + 1 < image.shape[0] else image.shape[0] - 1
            y1 = j - k if j - k >= 0 else 0
            y2 = j + k + 1 if j + k + 1 < image.shape[1] else image.shape[1] - 1
            A = np.sum(XX[x1:x2, y1:y2])
            B = np.sum(XY[x1:x2, y1:y2])
            C = np.sum(YY[x1:x2, y1:y2])

            M = np.array([[A, B], [B, C]])
            eVal, eVec = cv2.eigen(M, True)[1:]

            if (eVal > t).all():
                ptr_x.append(j)
                ptr_y.append(i)

    result = [ptr_x, ptr_y]
    return result
Example #6
0
def get_max_eigenvalue_node(node: ColorNode) -> ColorNode:
    if node.left is None and node.right is None:
        return node

    max_eigen_value = -1
    # eigenvalues = np.zer
    queue = deque()
    queue.append(node)

    output = node
    while len(queue) > 0:
        tmp_node: ColorNode = queue.popleft()

        if tmp_node.right and tmp_node.left:
            queue.append(tmp_node.right)
            queue.append(tmp_node.left)
            continue

        retval, eigenvalues, eigenvectors = cv2.eigen(tmp_node.covariance)

        if (eigenvalues[0] > max_eigen_value):
            max_eigen_value = eigenvalues[0]
            output = tmp_node

    return output
Example #7
0
def task_4_a():
    print("Task 4 (a) ...")
    start_vertex = 'A'
    W = np.array([[0., 1., 0.2, 1., 0., 0., 0., 0.],
                  [1., 0., 0.1, 0., 1., 0., 0., 0.],
                  [0.2, 0.1, 0., 1., 0., 1., 0.3, 0.],
                  [1., 0., 1., 0., 0., 1., 0., 0.],
                  [0., 1., 0., 0., 0., 0., 1., 1.],
                  [0., 0., 1., 1., 0., 0., 1., 0.],
                  [0., 0., 0.3, 0., 1., 1., 0., 1.],
                  [0., 0., 0., 0., 1., 0., 1., 0.]])  # construct the W matrix
    d = W.sum(axis=1)
    D = np.diag(d)
    L = D - W
    D_sqrt = np.diag(np.power(d, -1. / 2.))
    eigenEquation = np.dot(D_sqrt, np.dot(L, D_sqrt))
    lambdas, Z = cv.eigen(eigenEquation)[1:]
    eigen_value = np.amin(lambdas[lambdas != np.amin(lambdas)])
    print('Second smallest eigen value: {}'.format(eigen_value))
    y = Z[np.where(lambdas == eigen_value)[0]].T
    y = np.dot(D_sqrt, y)
    print('Corresponding eigen vector: {}'.format(y))
    min_N_cut = np.dot(y.T, np.dot(L, y)) / np.dot(y.T, np.dot(D, y))
    print('Minimum NCut: {}'.format(min_N_cut[0][0]))
    c1 = np.where(y > 0)[0]
    c2 = np.where(y < 0)[0]
    cluster1, cluster2 = [], []
    for i in range(len(c1)):
        cluster1.append(chr(ord(start_vertex) + c1[i]))
        cluster2.append(chr(ord(start_vertex) + c2[i]))
    print('Vertices in cluster - 1: {}'.format(cluster1))
    print('Vertices in cluster - 2: {}'.format(cluster2))
Example #8
0
def harrisdetector(image, k, t):
    # TODO Write harrisdetector function based on the illustration in specification.
    # Return corner points x-coordinates in result[0] and y-coordinates in result[1]
    ptr_x = []
    ptr_y = []

    img = image.astype('double')  #(300L, 300L, 3L)
    Ix = cv2.filter2D(img, -1, np.array((-1, 1)))
    Iy = cv2.filter2D(img, -1, np.array(((-1, 1), )))
    w = np.ones((2 * k + 1, 2 * k + 1))
    Ixx = cv2.filter2D(np.multiply(Ix, Ix), -1, w)  #(300L, 300L, 3L)
    #    print ('Ixx shape: ',np.shape(Ixx))
    Ixy = cv2.filter2D(np.multiply(Ix, Iy), -1, w)
    Iyy = cv2.filter2D(np.multiply(Iy, Iy), -1, w)

    print 'starting to looking for the feature...\n'

    for i in range(k, np.size(image, 0) - k):
        for j in range(k, np.size(image, 1) - k):
            _, eig, _ = cv2.eigen(
                np.array(((Ixx[i, j, 0], Ixy[i, j, 0]), (Ixy[i, j,
                                                             0], Iyy[i, j,
                                                                     0]))))
            if all(eig > t):
                ptr_x += [j]
                ptr_y += [i]

    result = [ptr_x, ptr_y]
    return result
Example #9
0
    def _get_orientation(self, pts):
        # Construct a buffer used by the PCA analysis
        data_pts = np.squeeze(np.array(pts, dtype=np.float64))

        # Perform PCA analysis
        # https://stackoverflow.com/questions/22612828/python-opencv-pcacompute-eigenvalue
        covar, mean = cv2.calcCovarMatrix(
            data_pts, np.mean(data_pts, axis=0),
            cv2.COVAR_SCALE | cv2.COVAR_ROWS | cv2.COVAR_SCRAMBLED)
        eigenvalues, eigenvectors = cv2.eigen(covar)[1:]
        eigenvectors = cv2.gemm(eigenvectors, data_pts - mean, 1, None, 0)
        eigenvectors = np.apply_along_axis(
            lambda n: cv2.normalize(n, dst=None).flat, 1, eigenvectors)

        # Store the centre of the object
        cntr = np.array([int(mean[0, 0]), int(mean[0, 1])])
        self.centres.append(cntr)

        # Draw the principal components
        cv2.circle(self.img, (cntr[0], cntr[1]), 3, (255, 0, 255), 1)
        p1 = cntr + 0.02 * eigenvectors[0] * eigenvalues[0]
        p2 = cntr - 0.02 * eigenvectors[1] * eigenvalues[1]
        # self._draw_axis(copy.copy(cntr), p1, (0, 255, 0), 2)
        # self._draw_axis(copy.copy(cntr), p2, (255, 255, 0), 10)

        return np.arctan2(eigenvectors[0, 1],
                          eigenvectors[0, 0])  # orientation in radians
Example #10
0
def eccentricityCitra(kontur):
    luasKontur = 0
    terluasKontur = 0
    indeks = 0
    myu20 = 0.00
    myu11 = 0.00
    myu02 = 0.00
    eccentricityCitra = 0.00

    for i in range(len(kontur)):
        luasKontur = cv2.contourArea(kontur[i], False)
        if luasKontur > terluasKontur:
            terluasKontur = luasKontur
            indeks = i
    mu = cv2.moments(kontur[indeks], False)
    myu20 = myu20 + mu['mu20']
    myu02 = myu02 + mu['mu02']
    myu11 = myu11 + mu['mu11']

    matriks = np.array([[myu20, myu11], [myu11, myu02]], dtype=np.float32) 
    ret, eigenv, eigenvct = cv2.eigen(matriks) # error: (-215) type == CV_32F || type == CV_64F
    eigenv1 = eigenv[0, 0]
    eigenv2 = eigenv[1, 0]

    # Hitung eigen value
    if eigenv1 >= eigenv2:
        eccentricityCitra = eigenv2 / eigenv1
    else:
        eccentricityCitra = eigenv1 / eigenv2
    return eccentricityCitra
Example #11
0
def task_4_a():
    print("Task 4 (a) ...")
    # construct the D matrix
    D = np.array([[2.2, 0, 0, 0, 0, 0, 0, 0], [0, 2.1, 0, 0, 0, 0, 0, 0],
                  [0, 0, 2.6, 0, 0, 0, 0, 0], [0, 0, 0, 3, 0, 0, 0, 0],
                  [0, 0, 0, 0, 3, 0, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0],
                  [0, 0, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 0, 2]])
    # construct the W matrix
    W = np.array([[0, 1, 0.2, 1, 0, 0, 0, 0], [1, 0, 0.1, 0, 1, 0, 0, 0],
                  [0.2, 0.1, 0, 1, 0, 1, 0.3, 0], [1, 0, 1, 0, 0, 1, 0, 0],
                  [0, 1, 0, 0, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0, 1, 0],
                  [0, 0, 0.3, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1, 0]])
    '''
    ...
    your code ...
    ...
    '''
    # compute D^(1/2)
    D_rs_inv = np.sqrt(D)
    # Get D^(-1/2)
    for i in range(D_rs_inv.shape[0]):
        for j in range(D_rs_inv.shape[1]):
            if D_rs_inv[i][j] != 0:
                D_rs_inv[i][j] = 1. / D_rs_inv[i][j]

    # A = D^(-1/2) * (D-W) * D^(-1/2)
    A = np.dot(D_rs_inv, np.dot(D - W, D_rs_inv))

    # Get the eigen value, eigen vector of z
    bool, eigenValues_z, eigenVectors_z = cv.eigen(A)
    eigenVectors_y = np.dot(D_rs_inv, eigenVectors_z)

    # 0 = (z1^T)(Z0) = (y1^T)D1, after print out the result, we get the vector we want
    print("second smallest: " + str(eigenVectors_y[6]))
def StegerLine(img):
    gray_origin = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray_origin, (5, 5), 0)
    Ix = cv2.Scharr(
        gray,
        cv2.CV_32F,
        1,
        0,
    )
    Iy = cv2.Scharr(gray, cv2.CV_32F, 0, 1)
    Ixx = cv2.Scharr(Ix, cv2.CV_32F, 1, 0)
    Ixy = cv2.Scharr(Ix, cv2.CV_32F, 0, 1)
    Iyy = cv2.Scharr(Iy, cv2.CV_32F, 0, 1)
    Iyx = cv2.Scharr(Iy, cv2.CV_32F, 1, 0)
    # Hessian矩阵
    row = gray_origin.shape[0]
    col = gray_origin.shape[1]
    CenterPoint = []
    for i in range(col):
        for j in range(row):
            if gray_origin[j, i] > 200:
                hessian = np.zeros((2, 2), np.float32)
                hessian[0, 0] = Ixx[j, i]
                hessian[0, 1] = Ixy[j, i]
                hessian[1, 0] = Iyx[j, i]
                hessian[1, 1] = Iyy[j, i]
                ret, eigenVal, eigenVec = cv2.eigen(hessian)
                lambda1 = 0.
                lambda2 = 0.
                nx, ny, fmaxD = 0.0, 0., 0.
                if ret:
                    # print(eigenVal.shape,eigenVec.shape)
                    if np.abs(eigenVal[0, 0]) >= np.abs(eigenVal[1, 0]):
                        lambda1 = eigenVal[1, 0]
                        lambda2 = eigenVal[0, 0]
                        nx = eigenVec[0, 0]
                        ny = eigenVec[0, 1]
                        famxD = eigenVal[0, 0]
                    else:
                        lambda1 = eigenVal[0, 0]
                        lambda2 = eigenVal[1, 0]
                        nx = eigenVec[1, 0]
                        ny = eigenVec[1, 1]
                        famxD = eigenVal[1, 0]
                    #if lambda1<15 and lambda2<-50:
                    t = -(nx * Ix[j, i] + ny * Iy[j, i]) / (
                        nx * nx * Ixx[j, i] + 2 * nx * ny * Ixy[j, i] +
                        ny * ny * Iyy[j, i])
                    if np.abs(t * nx) <= 0.5 and np.abs(t * ny) <= 0.5:
                        #CenterPoint.append([i, j])
                        CenterPoint.append([i, j])
    cv2.namedWindow("Steger_origin", 0)
    new_img = np.zeros((row, col), np.uint8)
    cv2.imshow("Steger_origin", gray_origin)
    for point in CenterPoint:
        cv2.circle(img, (point[0], point[1]), 1, 255)
    cv2.namedWindow("res", 0)
    cv2.imshow("res", img)
def computeH(M, m):
    """
    计算H变换矩阵
    """
    count = len(M)
    dtype = M.dtype
    cM = np.sum(M, 0) / count
    cMs = np.array([cM], dtype=dtype).repeat(count, 0)
    cm = np.sum(m, 0) / count
    cms = np.array([cm], dtype=dtype).repeat(count, 0)

    sM = np.sum(abs(M - cMs), 0)
    sm = np.sum(abs(m - cms), 0)
    if (sM < epsilon).any() or (sm < epsilon).any():
        return None
    sm = count / sm
    sM = count / sM

    invHorm = np.array([
        [1 / sm[0], 0, cm[0]],
        [0, 1 / sm[1], cm[1]],
        [0, 0, 1],
    ],
                       dtype=dtype)
    Hnorm2 = np.array([
        [sM[0], 0, -cM[0] * sM[0]],
        [0, sM[1], -cM[1] * sM[1]],
        [0, 0, 1],
    ],
                      dtype=dtype)

    LtL = np.zeros((9, 9), dtype=dtype)

    for i in range(0, count):
        (x, y) = (m[i] - cm) * sm
        (X, Y) = (M[i] - cM) * sM
        Lx = np.array([[X, Y, 1, 0, 0, 0, -x * X, -x * Y, -x]], dtype=dtype)
        Ly = np.array([[0, 0, 0, X, Y, 1, -y * X, -y * Y, -y]], dtype=dtype)

        LtL = LtL + Lx.transpose() @ Lx + Ly.transpose() @ Ly

    LtL = cv2.completeSymm(LtL)
    ok, _, V = cv2.eigen(LtL)
    if not ok:
        return None

    H0 = V[8].reshape((3, 3))
    HTemp = V[7].reshape((3, 3))
    HTemp = invHorm @ H0
    H0 = HTemp @ Hnorm2 * 1 / H0[2, 2]
    return H0
Example #14
0
def newRotation(strg):
    # Make a rotation, given by the inclination
    # of the word.
    # There're some restrictions of the long words
    # because the angle between the principal components
    # is so small or just too big.
    dst = 0
    pos = []
    t = cv.imread(strg, -1)

    t[t == 1] = 255
    t[t == 0] = 1
    t[t == 255] = 0

    X, Y = np.shape(t)
    whole_test = np.zeros((X * 2, Y * 3))
    X_1, Y_1 = np.shape(whole_test)
    whole_test[X // 2:3 * X // 2, Y // 2:3 * Y // 2] = t

    for i in range(X_1):
        for j in range(Y_1):
            if whole_test[i, j] == 1:
                pos.append([np.abs(i - Y_1), j])

    data = np.array(pos)
    cvm = np.cov(data.T)
    _, eVec = cv.eigen(cvm)[1:]

    [m1, m2] = eVec[0]
    angle = np.int(np.abs(np.arctan(m1 / m2) * 180 // 3.1415))

    if angle < 15:
        angle = 15
    elif angle > 25:
        angle = 25

    rot_mat = cv.getRotationMatrix2D((X // 2, Y // 2), angle, 1)

    dst = cv.warpAffine(t, rot_mat, (Y_1, X_1))
    dst[dst == 1] = 255
    dst[dst == 0] = 1
    dst[dst == 255] = 0
    dst.reshape(Y_1, X_1)

    kernel = np.ones((2, 2))
    cl2 = cv.morphologyEx(dst, cv.MORPH_CLOSE, kernel)
    op2 = cv.morphologyEx(cl2, cv.MORPH_OPEN, kernel)

    return op2
Example #15
0
def calcNormEigenVals(pts):
    """
    descriptor of the binary image by using eigen values
    :param pts: inpoints
    :return:
    """
    covar, mean = cv2.calcCovarMatrix(pts,
                                      mean=None,
                                      flags=cv2.COVAR_NORMAL | cv2.COVAR_ROWS
                                      | cv2.COVAR_SCALE)
    ret, eVal, eVec = cv2.eigen(covar)
    eSum = eVal[0] + eVal[1]
    eVal1 = eVal[0] / eSum
    eVal2 = eVal[1] / eSum
    return eVal1, eVal2
def computeTensors(A, B, C, p1, p2):
    bar = tensorsTools.Bar("Compute Tensors", A.shape[0] * A.shape[1])
    T = np.zeros(A.shape, Tensor)
    for i in range(A.shape[0]):
        for j in range(A.shape[1]):
            # create symetric matrix 2x2 [[A,C][C,B]]
            tmp = np.zeros((2, 2), np.float64)
            tmp[0, 0] = A[i, j]
            tmp[0, 1] = C[i, j]
            tmp[1, 0] = C[i, j]
            tmp[1, 1] = B[i, j]
            # extract eigenValues and eigenVectors to compute tensor
            T[i, j] = Tensor(cv.eigen(tmp), p1, p2)
            bar.next()
    return T
Example #17
0
def getLsHomography(a,b):
    alen = a.shape[0]
    A = np.zeros((9,9))
    for i in range(alen):
        l1 = np.array([a[i,0],a[i,1],1,0,0,0,-a[i,0]*b[i,0],-a[i,1]*b[i,0],-b[i,0]])
        l2 = np.array([0,0,0,a[i,0],a[i,1],1,-a[i,0]*b[i,1],-a[i,1]*b[i,1],-b[i,1]])
        l1 = l1.reshape(1,9)
        l2 = l2.reshape(1,9)
        A = A + np.dot(l1.T,l1)+np.dot(l2.T,l2)
    retval,_,e_vecs = cv2.eigen(A)
    if retval:
        H = e_vecs[-1,:]
        H = H.reshape(3,3)
        H = H/H[2,2]
    return H
Example #18
0
def task_4_a():
    print("Task 4 (a) ...")
    vertices = 8
    W = np.array([[0, 1, .2, 1, 0, 0, 0, 0], [1, 0, .1, 0, 1, 0, 0, 0],
                  [.2, .1, 0, 1, 0, 1, .3, 0], [1, 0, 1, 0, 0, 1, 0, 0],
                  [0, 1, 0, 0, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0, 1, 0],
                  [0, 0, .3, 0, 1, 1, 0, 1], [0, 0, 0, 0, 1, 0, 1,
                                              0]])  # construct the W matrix
    D = np.zeros(shape=(vertices, vertices))  # construct the D matrix
    for i in range(vertices):
        D[i, i] = np.sum(W[i])

    L = D - W
    D_sqrt = np.sqrt(D)
    D_sqrt_inv = np.linalg.inv(D_sqrt)
    _, eigen_values, eigen_vectors = cv.eigen(
        np.matmul(np.matmul(D_sqrt_inv, L), D_sqrt_inv))
    y2 = np.dot(np.linalg.inv(D_sqrt), eigen_vectors[-2])
    print(y2)

    ##4_b
    print("Task 4 (b) ...")

    characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    C1 = set()
    C2 = set()
    for index, value in enumerate(y2):
        if value < 0:
            C1.add(characters[index])
        else:
            C2.add(characters[index])
    print("Cluster 1: ", C1)
    print("Cluster 2: ", C2)
    weight_sum = 0
    for index, weight in np.ndenumerate(W):
        if characters[index[0]] in C1 and \
                characters[index[1]] in C2:
            weight_sum += weight
    volume_1 = 0
    volume_2 = 0
    for index, char in enumerate(characters):
        if char in C1:
            volume_1 += np.sum(W[index])
        else:
            volume_2 += np.sum(W[index])

    cost = (weight_sum) / volume_1 + (weight_sum) / volume_2
    print("Cost: ", cost)
Example #19
0
    def pose(self, intrinsic, radius):
        fx = intrinsic[0, 0]
        fy = intrinsic[1, 1]
        f = (fx + fy) / 2.0
        self.Q = self.Q_2d * np.array([[1,   1,   1/f],
                                       [1,   1,   1/f],
                                       [1/f, 1/f, 1/(f*f)]])

        ret, E, V = cv2.eigen(self.Q)
        V = V.T
        e1 = E[0, 0]
        e2 = E[1, 0]
        e3 = E[2, 0]
        S1 = [+1, +1, +1, +1, -1, -1, -1, -1]
        S2 = [+1, +1, -1, -1, +1, +1, -1, -1]
        S3 = [+1, -1, +1, -1, +1, -1, +1, -1]
        g = sqrt((e2-e3)/(e1-e3))
        h = sqrt((e1-e2)/(e1-e3))

        k = 0
        for i in range(8):
            z0 = S3[i] * (e2 * radius) / sqrt(-e1*e3)

            # Rotated center vector
            Tx = S2[i] * e3/e2 * h
            Ty = 0.0
            Tz = -S1[i] * e1/e2 * g

            # Rotated normal vector
            Nx = S2[i] * h
            Ny = 0.0
            Nz = -S1[i] * g

            t = z0 * V @ np.array([Tx, Ty, Tz])  # Center of circle in CCS
            n = V @ np.array([Nx, Ny, Nz])  # Normal vector unit in CCS

            # identify the two possible solutions
            if (t[2] > 0) and (n[2] < 0):  # Check facing constrain
                if k > 1:
                    continue
                self.translations[k] = t
                self.normals[k] = n

                # Projection
                Pc = intrinsic @ t
                self.projections[k, 0] = Pc[0]/Pc[2]
                self.projections[k, 1] = Pc[1]/Pc[2]
                k += 1
Example #20
0
def pca(X, numOfPca=0):
    [n,m] = X.shape
    print X
    Pusai = X.mean(axis=0)
    print Pusai
    X = X - Pusai
    print X
    Cov = cv.mulTransposed(X, False)
    print Cov
    [retval, eigenvalues, eigenvectors] = cv.eigen(Cov, True)
    indexes = np.argsort(-eigenvalues)
    eigenvalues = eigenvalues[indexes]
    eigenvectors = eigenvectors[:,indexes]
    eigenvalues = eigenvalues[0:numOfPca].copy()
    eigenvectors = eigenvectors[:,0:numOfPca].copy()
    return [eigenvalues, eigenvectors, Pusai]
def Steger(img):
    gray_origin = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.namedWindow("gray_origin", 0)
    cv2.imshow("gray_origin", gray_origin)
    gray = cv2.GaussianBlur(gray_origin, (5, 5), 0)
    Ix = cv2.Scharr(gray, cv2.CV_32F, 1, 0)
    Iy = cv2.Scharr(gray, cv2.CV_32F, 0, 1)
    Ixx = cv2.Scharr(Ix, cv2.CV_32F, 1, 0)
    Ixy = cv2.Scharr(Ix, cv2.CV_32F, 0, 1)
    Iyy = cv2.Scharr(Iy, cv2.CV_32F, 0, 1)
    Iyx = cv2.Scharr(Iy, cv2.CV_32F, 1, 0)
    # Hessian矩阵
    row = img.shape[0]
    col = img.shape[1]
    CenterPoint = []
    for i in range(col):
        for j in range(row):
            if gray_origin[j, i] > 200:
                hessian = np.zeros((2, 2), np.float32)
                hessian[0, 0] = Ixx[j, i]
                hessian[0, 1] = Ixy[j, i]
                hessian[1, 0] = Iyx[j, i]
                hessian[1, 1] = Iyy[j, i]
                ret, eigenVal, eigenVec = cv2.eigen(hessian)
                nx, ny, fmaxD = 0.0, 0., 0.
                if ret:
                    #print(eigenVal.shape,eigenVec.shape)
                    if np.abs(eigenVal[0, 0] >= eigenVal[1, 0]):
                        nx = eigenVec[0, 0]
                        ny = eigenVec[0, 1]
                        famxD = eigenVal[0, 0]
                    else:
                        nx = eigenVec[1, 0]
                        ny = eigenVec[1, 1]
                        famxD = eigenVal[1, 0]
                    t = -(nx * Ix[j, i] + ny * Iy[j, i]) / (
                        nx * nx * Ixx[j, i] + 2 * nx * ny * Ixy[j, i] +
                        ny * ny * Iyy[j, i])
                    if np.abs(t * nx) <= 0.5 and np.abs(t * ny) <= 0.5:
                        CenterPoint.append([i, j])
    cv2.namedWindow("Steger_origin", 0)
    cv2.imshow("Steger_origin", img)
    for point in CenterPoint:
        #cv2.circle(img,(point[0],point[1]),1,(0,255,0))
        img[point[1], point[0]] = (0, 255, 0)
    cv2.namedWindow("res", 0)
    cv2.imshow("res", img)
Example #22
0
def get_orientation(pts, img):
    sz = len(pts)
    data_pts = np.empty((sz, 2), dtype=np.float64)
    for i in range(data_pts.shape[0]):
        data_pts[i, 0] = pts[i, 0, 0]
        data_pts[i, 1] = pts[i, 0, 1]
    # Perform PCA analysis
    mean = np.empty(0)
    # mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean)

    covar, mean = cv2.calcCovarMatrix(data_pts, mean, cv2.COVAR_SCALE |
                                      cv2.COVAR_ROWS |
                                      cv2.COVAR_SCRAMBLED)

    eVal, eVec = cv2.eigen(covar)[1:]

    # Conversion + normalisation required due to 'scrambled' mode
    eVec = cv2.gemm(eVec, data_pts - mean, 1, None, 0)
    # apply_along_axis() slices 1D rows, but normalize() returns 4x1 vectors
    eVec = np.apply_along_axis(lambda n: cv2.normalize(n, n).flat, 1, eVec)

    # Store the center of the object
    # cntr2 = (int(mean[0, 0]), int(mean[0, 1]))
    M = cv2.moments(pts)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    cntr = (cX, cY)

    cv2.circle(img, cntr, 3, (255, 0, 255), 2)
    # p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0],
    #       cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0])
    # p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0],
    #       cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0])

    p1 = (cntr[0] + 0.02 * eVec[0, 0] * eVal[0, 0],
          cntr[1] + 0.02 * eVec[0, 1] * eVal[0, 0])
    p2 = (cntr[0] - 0.02 * eVec[1, 0] * eVal[1, 0],
          cntr[1] - 0.02 * eVec[1, 1] * eVal[1, 0])
    # draw_axis(img, cntr, p1, (0, 255, 0), 1)
    # draw_axis(img, cntr, p2, (255, 255, 0), 5)
    # angle = atan2(eigenvectors[0, 1], eigenvectors[0, 0])  # orientation in radians
    angle = atan2(eVec[0, 1], eVec[0, 0])
    return angle, cntr, eVec[0, 0], eVec[0, 1], eVal[0, 0]
Example #23
0
def compute_projection_matrix(obj, img):
    A = []
    for i in range(0, 10):
        row1 = numpy.array([
            obj[i][0], obj[i][1], obj[i][2], 1, 0, 0, 0, 0,
            -img[i][0][0] * obj[i][0], -img[i][0][0] * obj[i][1],
            -img[i][0][0] * obj[i][2], -img[i][0][0]
        ])
        A.append(row1)
        row2 = numpy.array([
            0, 0, 0, 0, obj[i][0], obj[i][1], obj[i][2], 1,
            -img[i][0][1] * obj[i][0], -img[i][0][1] * obj[i][1],
            -img[i][0][1] * obj[i][2], -img[i][0][1]
        ])
        A.append(row2)
    A = numpy.asarray(A)
    prod = numpy.dot(A.T, A)
    ret, eigenvalues, eigenvectors = cv2.eigen(prod, True)
    decompose_projection_matrix(eigenvectors)
def PCA(PCAInput):
    # The following mimics PCA::operator() implementation from OpenCV's
    # matmul.cpp() which is wrapped by Python cv2.PCACompute(). We can't
    # use PCACompute() though as it discards the eigenvalues.

    # Scrambled is faster for nVariables >> nObservations. Bitmask is 0 and
    # therefore default / redundant, but included to abide by online docs.
    covar, mean = cv2.calcCovarMatrix(
        PCAInput, cv2.cv.CV_COVAR_SCALE | cv2.cv.CV_COVAR_ROWS
        | cv2.cv.CV_COVAR_SCRAMBLED)

    eVal, eVec = cv2.eigen(covar, computeEigenvectors=True)[1:]

    # Conversion + normalisation required due to 'scrambled' mode
    eVec = cv2.gemm(eVec, PCAInput - mean, 1, None, 0)
    # apply_along_axis() slices 1D rows, but normalize() returns 4x1 vectors
    eVec = np.apply_along_axis(lambda n: cv2.normalize(n).flat, 1, eVec)

    return mean, eVec
def PCA(PCAInput):
    # The following mimics PCA::operator() implementation from OpenCV's
    # matmul.cpp() which is wrapped by Python cv2.PCACompute(). We can't
    # use PCACompute() though as it discards the eigenvalues.

    # Scrambled is faster for nVariables >> nObservations. Bitmask is 0 and
    # therefore default / redundant, but included to abide by online docs.
    covar, mean = cv2.calcCovarMatrix(PCAInput, cv2.cv.CV_COVAR_SCALE |
                                                cv2.cv.CV_COVAR_ROWS  |
                                                cv2.cv.CV_COVAR_SCRAMBLED)

    eVal, eVec = cv2.eigen(covar, computeEigenvectors=True)[1:]

    # Conversion + normalisation required due to 'scrambled' mode
    eVec = cv2.gemm(eVec, PCAInput - mean, 1, None, 0)
    # apply_along_axis() slices 1D rows, but normalize() returns 4x1 vectors
    eVec = np.apply_along_axis(lambda n: cv2.normalize(n).flat, 1, eVec)

    return mean,  eVec    
Example #26
0
def train(img_vec, eig_vec_num):
    mean = np.mean(img_vec, 1)

    # Calculate covariance matrix
    pca_data = np.transpose(np.transpose(img_vec) - mean)
    pca_mat = np.dot(np.transpose(pca_data), pca_data)
    cov, temp_mean = cv2.calcCovarMatrix(pca_mat, mean,
                                         cv2.COVAR_NORMAL | cv2.COVAR_ROWS)

    # Eigenvectors and eigenvalues
    eig_val_temp, eig_vec_temp = cv2.eigen(cov, eigenvectors=True)[1:]
    eig_vec = np.dot(eig_vec_temp, np.transpose(img_vec))
    eig_vec = (eig_vec - eig_vec.min()) / (eig_vec.max() - eig_vec.min())

    eig_vec_mat = []
    for idx in range(eig_vec_num):
        eig_vec_mat.append(eig_vec[idx])

    return mean, eig_vec_mat
Example #27
0
def task_4_a():
    print("Task 4 (a) ...")
    print('-------------------------------------------------')
    D = np.zeros((8, 8))
    W = np.array((
        [0, 1, 0.2, 1, 0, 0, 0, 0],  # A
        [1, 0, 0.1, 0, 1, 0, 0, 0],  # B
        [0.2, 0.1, 0, 1, 0, 1, 0.3, 0],  # C
        [1, 0, 1, 0, 0, 1, 0, 0],  # D
        [0, 1, 0, 0, 0, 0, 1, 1],  # E
        [0, 0, 1, 1, 0, 0, 1, 0],  # F
        [0, 0, 0.3, 0, 1, 1, 0, 1],  # G
        [0, 0, 0, 0, 1, 0, 1, 0]  # H
    ))  # construct the W matrix

    for i in range(W.shape[0]):
        D[i, i] = np.sum(W[i, :])  # construct the D matrix
    '''
    ...
    your code ...
    ...
    '''
    invSqrtD = np.linalg.inv(np.sqrt(D))
    L = D - W

    op = np.matmul(np.matmul(invSqrtD, L), invSqrtD)
    _, _, eigenVecs = cv.eigen(op)
    secMinEigenVec = eigenVecs[eigenVecs.shape[1] - 2, :]

    C1 = 0
    C2 = 0
    for i in range(secMinEigenVec.shape[0]):
        if secMinEigenVec[i] < 0:
            C1 += D[i, i]
        else:
            C2 += D[i, i]

    print('Eigen Vec: ' + str(np.round(secMinEigenVec, 3)))

    # Figure in pdf
    minNormCut = (1 / C1 + 1 / C2) * 2.4
    print('Min Norm Cut = ' + str(minNormCut))
    print('=================================================')
def harrisdetector(image, k, t):
    # TODO Write harrisdetector function based on the illustration in specification.
    # Return corner points x-coordinates in result[0] and y-coordinates in result[1]
    ptr_x = []
    ptr_y = []
    
    Im= np.zeros((image.shape[0],image.shape[1],1),dtype=np.float32)
    Im[:,:,0] = image[:,:,0]*0.114+image[:,:,1]*0.587+image[:,:,2]*0.299
    Im.astype(np.float)
    Im = np.pad(Im, ((k, k), (k, k), (0,0)),'edge') 
    
    Weight = np.ones((2*k+1,2*k+1),dtype=np.float)/9
    horrizonKer = np.array([[1.0,0.0,-1.0]],dtype=np.float)
    verticalKer = np.array([[1.0],[0.0],[-1.0]])
    image_Blur = cv2.filter2D(Im ,-1, Weight)
    
    I_x = cv2.filter2D(image_Blur, -1, horrizonKer)
    I_y = cv2.filter2D(image_Blur, -1, verticalKer)
    
    I_xx = cv2.filter2D(I_x, -1, horrizonKer)
    I_yy = cv2.filter2D(I_y, -1, verticalKer)
    I_xy = cv2.filter2D(I_x, -1, verticalKer)
    
    I_xx = I_xx[k : -k, k : -k]
    I_yy = I_yy[k : -k, k : -k]   
    I_xy = I_xy[k : -k, k : -k]   
    
    for i in xrange(image.shape[0]):
        for j in xrange(image.shape[1]):
            A = np.zeros((2,2,1),dtype=np.float)
            A[0,0,0] = I_xx[i,j]
            A[0,1,0] = I_xy[i,j]
            A[1,0,0] = I_xy[i,j]
            A[1,1,0] = I_yy[i,j]
            
            a,eigens,vectors = cv2.eigen(A,True)
            if eigens[0,0]>t and eigens[1,0]>t:
                ptr_x.append(j)
                ptr_y.append(i)

    result = [ptr_x, ptr_y]
    return result
Example #29
0
def calc_homography(src_points, dst_points):

    iter_num = len(point_pairs)
    A = np.zeros((2 * iter_num, 9))
    for i, (first, second) in enumerate(zip(src_points, dst_points)):
        u1 = first[0]
        v1 = first[1]

        u2 = second[0]
        v2 = second[1]

        A[2 * i, 0] = u1
        A[2 * i, 1] = v1
        A[2 * i, 2] = 1.
        A[2 * i, 3] = 0.
        A[2 * i, 4] = 0.
        A[2 * i, 5] = 0.
        A[2 * i, 6] = -u2 * u1
        A[2 * i, 7] = -u2 * v1
        A[2 * i, 8] = -u2

        A[2 * i + 1, 0] = 0.
        A[2 * i + 1, 1] = 0.
        A[2 * i + 1, 2] = 0.
        A[2 * i + 1, 3] = u1
        A[2 * i + 1, 4] = v1
        A[2 * i + 1, 5] = 1.
        A[2 * i + 1, 6] = -v2 * u1
        A[2 * i + 1, 7] = -v2 * v1
        A[2 * i + 1, 8] = -v2

    _, _, eigen_vecs = cv.eigen(A.T @ A)
    # print(eigen_vecs[8, :])
    # print(eigen_vecs)

    H = eigen_vecs[8, :].reshape(3, 3)

    H /= H[-1, -1]

    # print(H)

    return H
Example #30
0
def LSQ(points: np.ndarray, inc_inliers: np.ndarray, threshold: float,
        **kwargs) -> Tuple[np.ndarray, np.ndarray]:
    masspoint = np.sum(inc_inliers, axis=0)

    masspoint /= inc_inliers.shape[0]
    normalized_points = inc_inliers - masspoint
    avg_distance = np.average(np.linalg.norm(normalized_points, axis=1))

    ratio = sqrt(3) / avg_distance

    normalized_points *= ratio

    A = normalized_points

    _, _, eigenvecs = cv.eigen(A.T @ A)
    a, b, c = eigenvecs[2, :]
    d = -(a * masspoint[0] + b * masspoint[1] + c * masspoint[2])
    plane = np.array([a, b, c, d])
    distances_mask = distance(points, plane) < threshold
    inliers = points[distances_mask]
    return inliers, plane
def harrisdetector(image, k, t):
    if np.size(image, 2) > 1:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    h = np.size(image, 0)
    w = np.size(image, 1)
    # padding
    image = np.lib.pad(image, ((k, k), (k, k)), "edge")

    # derivatives
    fx = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
    fy = np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
    Ix = cv2.filter2D(image, -1, fx)
    Iy = cv2.filter2D(image, -1, fy)
    Ix2 = Ix * Ix
    Iy2 = Iy * Iy
    IxIy = Ix * Iy

    # find corners
    ptr_x = []
    ptr_y = []

    for i in xrange(k, h + k):
        for j in xrange(k, w + k):
            # find A
            A11 = 0
            A12 = 0
            A22 = 0
            for u in xrange(i - k, i + k + 1):
                for v in xrange(j - k, j + k + 1):
                    A11 += Ix2[u, v]
                    A12 += IxIy[u, v]
                    A22 += Iy2[u, v]
            A = np.array([[A11, A12], [A12, A22]])
            retval, eigenvalues, eigenvectors = cv2.eigen(A)
            if eigenvalues[0] > t and eigenvalues[1] > t:
                ptr_x.append(j - k)
                ptr_y.append(i - k)

    result = [ptr_x, ptr_y]
    return result
Example #32
0
def harrisdetector(image, k, t):
    # TODO Write harrisdetector function based on the illustration in specification.
    # Return corner points x-coordinates in result[0] and y-coordinates in result[1]
    ptr_x = []
    ptr_y = []
    img = image.astype('double')
    Ix = cv2.filter2D(img, -1, np.array((-1, 1)))
    Iy = cv2.filter2D(img, -1, np.array(((-1, 1),)))
    w = np.ones((2*k+1, 2*k+1))
    Ixx = cv2.filter2D(np.multiply(Ix, Ix), -1, w)
    Ixy = cv2.filter2D(np.multiply(Ix, Iy), -1, w)
    Iyy = cv2.filter2D(np.multiply(Iy, Iy), -1, w)
    
    for i in range(k, np.size(image, 0) - k):
        for j in range(k, np.size(image, 1) - k):
            _, eig, _ = cv2.eigen(np.array(((Ixx[i,j,0], Ixy[i,j,0]), (Ixy[i,j,0], Iyy[i,j,0]))))
            if all(eig > t):
                ptr_x += [j]
                ptr_y += [i]

    result = [ptr_x, ptr_y]
    return result
Example #33
0
def pca(X, nb_components=0):
    '''
    Do a PCA analysis on X
    @param X:                np.array containing the samples
                             shape = (nb samples, nb dimensions of each sample)
    @param nb_components:    the nb components we're interested in
    @return: return the nb_components largest eigenvalues and eigenvectors of the covariance matrix and return the average sample 
    '''
    [n,d] = X.shape
    if (nb_components <= 0) or (nb_components>n):
        nb_components = n

    
    #calculate scrambled covariance matrix for increased performance
    [covar, mean] = cv2.calcCovarMatrix(X, cv.CV_COVAR_SCALE |  cv.CV_COVAR_ROWS | cv.CV_COVAR_SCRAMBLED)
    #compute eigenvalues and vectors of scrambled covariance matrix
    [retval,eigenvals,eigenvects] = cv2.eigen(covar, True)
    #calculate the normal eigenvactors from the scrambled eigenvectors
    eigenvects = cv2.gemm(eigenvects, X - mean, 1, None, 0)
    eigenvects = np.apply_along_axis(lambda n: cv2.normalize(n).flat, 1, eigenvects)
    # return the nb_components largest eigenvalues and eigenvectors
    return [eigenvals[:n], np.transpose(eigenvects[:n]), np.transpose(mean)]
def getOrientation(pts, img):
    
    sz = len(pts)
    data_pts = np.empty((sz, 2), dtype=np.float64)
    for i in range(data_pts.shape[0]):
        data_pts[i,0] = pts[i,0,0]
        data_pts[i,1] = pts[i,0,1]
    # Perform PCA analysis
    mean = np.empty((0))
    mean, eigenvectors = cv2.PCACompute(data_pts, mean)
    # Store the center of the object
    cntr = (int(mean[0,0]), int(mean[0,1]))
    
    
    cv2.circle(img, cntr, 3, (255, 0, 255), 2)
    ret,eigenvalues,eigenvectors=cv2.eigen(thresh,True) 
    p1 = (cntr[0] + 0.02 * eigenvectors[0,0] * eigenvalues[0,0], cntr[1] + 0.02 * eigenvectors[0,1] * eigenvalues[0,0])
    p2 = (cntr[0] - 0.02 * eigenvectors[1,0] * eigenvalues[1,0], cntr[1] - 0.02 * eigenvectors[1,1] * eigenvalues[1,0])
    drawAxis(img, cntr, p1, (0, 255, 0), 1)
    drawAxis(img, cntr, p2, (255, 255, 0), 5)
    angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians
    
    return angle
Example #35
0
def LSQ_fundamental_matrix(src_points, dst_points) -> np.ndarray:
    num_points = len(src_points)
    A = np.empty((num_points, 9))
    for ind, (src, dst) in enumerate(zip(src_points, dst_points)):

        x1, y1 = src.ravel()
        x2, y2 = dst.ravel()

        A[ind, 0] = x1 * x2
        A[ind, 1] = x2 * y1
        A[ind, 2] = x2
        A[ind, 3] = y2 * x1
        A[ind, 4] = y2 * y1
        A[ind, 5] = y2
        A[ind, 6] = x1
        A[ind, 7] = y1
        A[ind, 8] = 1

    _, _, eigen_vecs = cv.eigen(A.T @ A)

    F = eigen_vecs[8, :].reshape(3, 3)

    return F
def Train(path):
	global m,et,wt_matrix
	im = Image.open(path+"/1")
	m= im.size[0]													#dimensions of image 
	n=im.size[1]	
	print m,n
	L=[]
	lst = os.listdir(path) 				#dir is path of Train database
	l = len(lst)																		#read image and append it as a column to L
	for i in range(1,l+1):
		path2=path + "/"+str(i)#".jpg"
		im = Image.open(path2)
		im2=im.save("try.jpg")
		data=cv2.imread("try.jpg",0)
		data=data.reshape(m*n,1)
		data=np.array(data[:,0])
		L.append(data)

	a=np.asarray(L)												 #convert list to np.array 
	ImageVector=a.T 											 #Transpose since list is appended as a row ..we need it as a column.

	#----------------Eigen_faces------------------------------------

	m=np.mean(ImageVector,axis=1)								 #mean about columns (axis=1 gives mean about column)

	A=ImageVector-m[:,None]    							         #Difference matrix(Unique features)
	At=A.T 														 #Transpose of Difference matrix				
	covar=At.dot(A)												 #covariance = A transpose into A

	eival, eivec = cv2.eigen(covar, computeEigenvectors=True)[1:] #eigen values and vectors of covariance matrix

	pri=[]

	column_count=len(ImageVector[0])

	t=0;

	for i in range(0,column_count):      						 #ignore eigen vectors less than threshold
		if(eival[i]>1):
			pri.append(eivec[:,i])
			t=t+1	

	prim=np.asarray(pri)				 						 #convert to np.array as pri is list
	primary=prim.transpose()

	Eiganfaces=A.dot(primary)			 					   	 #to get original dataset in terms of eigen vectors

	#-------------------Recognition---------------------------------

	wt=[]
	counter=len(Eiganfaces[0])									#number of eigen vectors in eigenfaces.

	et=Eiganfaces.transpose()

	for i in range(0,counter):
		wt.append(et.dot(A[:,i]))								#multiply a image column(A(:,i)) with every vector in (Eigenfaces)

	wt1=np.asarray(wt)
	wt_matrix=wt1.transpose()
	print "Trained!"
	return et,wt_matrix