Пример #1
0
def energy(b_tmp, image):
    sky_mask = make_mask(b_tmp, image)

    ground = np.ma.array(
        image,
        mask=cv2.cvtColor(cv2.bitwise_not(sky_mask), cv2.COLOR_GRAY2BGR)
    ).compressed()
    sky = np.ma.array(
        image,
        mask=cv2.cvtColor(sky_mask, cv2.COLOR_GRAY2BGR)
    ).compressed()
    ground.shape = (ground.size//3, 3)
    sky.shape = (sky.size//3, 3)

    sigma_g, mu_g = cv2.calcCovarMatrix(
        ground,
        None,
        cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE
    )
    sigma_s, mu_s = cv2.calcCovarMatrix(
        sky,
        None,
        cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE
    )

    y = 2

    return 1 / (
        (y * np.linalg.det(sigma_s) + np.linalg.det(sigma_g)) +
        (y * np.linalg.det(np.linalg.eig(sigma_s)[1]) +
            np.linalg.det(np.linalg.eig(sigma_g)[1]))
    )
Пример #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)
Пример #3
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
Пример #4
0
def refine_sky(bopt, image):
    sky_mask = make_mask(bopt, image)
    ground = np.ma.array(image,mask=cv2.cvtColor(cv2.bitwise_not(sky_mask), cv2.COLOR_GRAY2BGR)).compressed()
    sky = np.ma.array(image,mask=cv2.cvtColor(sky_mask, cv2.COLOR_GRAY2BGR)).compressed()
    ground.shape = (ground.size//3, 3)
    sky.shape = (sky.size//3, 3)
    ret, label, center = cv2.kmeans(np.float32(sky),2,None,(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0),10,cv2.KMEANS_RANDOM_CENTERS)
    sigma_s1, mu_s1 = cv2.calcCovarMatrix(sky[label.ravel() == 0],None,cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE)
    ic_s1 = cv2.invert(sigma_s1, cv2.DECOMP_SVD)[1]
    sigma_s2, mu_s2 = cv2.calcCovarMatrix(sky[label.ravel() == 1],None,cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE)
    ic_s2 = cv2.invert(sigma_s2, cv2.DECOMP_SVD)[1]
    sigma_g, mu_g = cv2.calcCovarMatrix(ground,None,cv2.COVAR_NORMAL | cv2.COVAR_ROWS | cv2.COVAR_SCALE)
    icg = cv2.invert(sigma_g, cv2.DECOMP_SVD)[1]
    if cv2.Mahalanobis(mu_s1, mu_g, ic_s1) > cv2.Mahalanobis(mu_s2, mu_g, ic_s2):
        mu_s = mu_s1
        sigma_s = sigma_s1
        ics = ic_s1
    else:
        mu_s = mu_s2
        sigma_s = sigma_s2
        ics = ic_s2
    for x in range(image.shape[1]):
        cnt = np.sum(np.less(spatial.distance.cdist(image[0:bopt[x], x],mu_s,'mahalanobis',VI=ics),spatial.distance.cdist(image[0:bopt[x], x],mu_g,'mahalanobis',VI=icg)))
        if cnt < (bopt[x] / 2):bopt[x] = 0
    return bopt
Пример #5
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
Пример #6
0
def performPCA(images):

    #  Allocate space for all images in one data matrix. The size of the data matrix is
    # ( w  * h  * c, numImages ) where, w = width of an image in the dataset.
    # h = height of an image in the dataset. c is for the number of color channels.

    numImages = len(images)
    sz = images[0].shape
    channels = 1 # grayescale
    data = np.zeros((numImages, sz[0] * sz[1] * channels), dtype=np.float32)

    # store images as floating point vectors normalized 0 -> 1

    for i in range(0, numImages):
        image = np.float32(images[i])/255.0
        data[i,:] = image.flatten() # N.B. data is stored as rows

    # compute the eigenvectors from the stack of image vectors created

    mean, eigenVectors = cv2.PCACompute(data, mean=None, maxComponents=args.eigenfaces)

    # use the eigenvectors to project the set of images to the new PCA space representation

    coefficients = cv2.PCAProject(data, mean, eigenVectors)

    # calculate the covariance and mean of the PCA space representation of the images
    # (skipping the first N eigenfaces that often contain just illumination variance, default N=3 )

    covariance_coeffs, mean_coeffs = cv2.calcCovarMatrix(coefficients[:,args.eigenfaces_to_skip:args.eigenfaces], mean=None, flags=cv2.COVAR_NORMAL | cv2.COVAR_ROWS, ctype = cv2.CV_32F)

    return (mean, eigenVectors, coefficients, mean_coeffs, covariance_coeffs)
Пример #7
0
def true_sky(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))

    #将天空和地面区域shape转换为n*3
    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)

    # k均值聚类调整天空区域边界--2类
    sky_image_float = np.float32(sky_image)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    flags = cv2.KMEANS_RANDOM_CENTERS
    compactness, labels, centers = cv2.kmeans(sky_image_float, 2, None, criteria, 10, flags)

    sky_label_0 = sky_image[labels.ravel() == 0]
    sky_label_1 = sky_image[labels.ravel() == 1]

    sky_covar_0, sky_mean_0 = cv2.calcCovarMatrix(sky_label_0, mean= None, flags= cv2.COVAR_ROWS + cv2.COVAR_NORMAL + cv2.COVAR_SCALE)
    sky_covar_1, sky_mean_1 = cv2.calcCovarMatrix(sky_label_1, mean= None, flags= cv2.COVAR_ROWS + cv2.COVAR_NORMAL + cv2.COVAR_SCALE)
    ground_covar, ground_mean = cv2.calcCovarMatrix(ground_image, mean= None, flags= cv2.COVAR_ROWS + cv2.COVAR_NORMAL + cv2.COVAR_SCALE)

    ic_s0 = cv2.invert(sky_covar_0, cv2.DECOMP_SVD)[1]
    ic_s1 = cv2.invert(sky_covar_1, cv2.DECOMP_SVD)[1]
    ic_g = cv2.invert(ground_covar, cv2.DECOMP_SVD)[1]

    #推断真实的天空区域
    if cv2.Mahalanobis(sky_mean_0, ground_mean, ic_s0) > cv2.Mahalanobis(sky_mean_1, ground_mean, ic_s1):
        sky_mean = sky_mean_0
        sky_covar = sky_covar_0
        ic_s = ic_s0
    else:
        sky_mean = sky_mean_1
        sky_covar = sky_covar_1
        ic_s = ic_s1


    return sky_covar,sky_mean,ic_s,ground_covar, ground_mean,ic_g
def similarity_transform(shape10,shape20):
    shape1=np.zeros((shape10.shape[0]))
    shape1[:]=shape10[:]
    
    shape2=np.zeros((shape20.shape[0]))
    shape2[:]=shape20[:]
    center_x1=np.mean(shape1[::2])
    center_y1=np.mean(shape1[1::2])
    center_x2=np.mean(shape2[::2])
    center_y2=np.mean(shape2[1::2])
    shape1[::2]=shape1[::2]-center_x1
    shape1[1::2]=shape1[1::2]-center_y1
    shape2[::2]=shape2[::2]-center_x2
    shape2[1::2]=shape2[1::2]-center_y2
    temp1=np.zeros((2,shape10.shape[0]/2))
    temp2=np.zeros((2,shape20.shape[0]/2))
    
    temp1[0,:]=shape1[::2]
    temp1[1,:]=shape1[1::2]
    temp2[0,:]=shape2[::2]
    temp2[1,:]=shape2[1::2]
    covar1,m1=cv2.calcCovarMatrix(temp1.T,cv2.cv.CV_COVAR_COLS)    
    covar2,m2=cv2.calcCovarMatrix(temp2.T,cv2.cv.CV_COVAR_COLS)
    #covar1=np.cov(temp1,rowvar=1)
    #covar2=np.cov(temp2,rowvar=1)
    s1=np.sqrt(np.linalg.norm(covar1))
    s2=np.sqrt(np.linalg.norm(covar2))
    #s11=np.sqrt(np.linalg.norm(haha1))
    #s22=np.sqrt(np.linalg.norm(haha2))
    scale = s1 / s2 
    """ 
    aaa= np.sum(np.abs(shape1-shape2 )) 
    bbb= np.sum(np.abs(scale*shape2-shape1 )) 
    if (aaa>bbb):
        print "right"
    else: print "wrong"
    """
    shape1=shape1/s1
    shape2=shape2/s2
    num=np.sum(shape1[::2]*shape2[1::2]-shape1[1::2]*shape2[::2])
    den=np.sum(shape1[::2]*shape2[::2]+shape1[1::2]*shape2[1::2])
    norm=np.sqrt(num*num+den*den)
    sin_theta = num/norm
    cos_theta = den/norm
    tran=np.array([[scale*cos_theta,scale*sin_theta],[-scale*sin_theta,scale*cos_theta]])
    return tran
Пример #9
0
def get_similarity_transform(shape_from, shape_to):
    rotation = np.zeros((2, 2))
    scale = 0.
    center_x_1, center_y_1 = np.mean(shape_to, axis=0)
    center_x_2, center_y_2 = np.mean(shape_from, axis=0)

    center_x_1 /= shape_to.shape[0]
    center_y_1 /= shape_to.shape[0]
    center_x_2 /= shape_from.shape[0]
    center_y_2 /= shape_from.shape[0]

    temp1 = shape_to.copy()
    temp2 = shape_from.copy()

    temp1 -= np.array([center_x_1, center_y_1]).reshape((1, 2))
    temp2 -= np.array([center_x_2, center_y_2]).reshape((1, 2))

    covariance1, mean1 = cv.calcCovarMatrix(temp1, None, cv.COVAR_COLS)
    # print("cov mean:", covariance1, mean1)
    covariance2, mean2 = cv.calcCovarMatrix(temp2, None, cv.COVAR_COLS)

    s1, s2 = np.sqrt(np.linalg.norm(covariance1)), np.sqrt(
        np.linalg.norm(covariance2))

    scale = s1 / s2
    temp1 /= s1
    temp2 /= s2

    num = 0.
    den = 0.

    for t1, t2 in zip(temp1, temp2):
        num += t1[1] * t2[0] - t1[0] * t2[1]
        den += t1[0] * t2[0] + t1[1] * t2[1]

    norm = np.sqrt(num * num + den * den)
    sin_theta = num / norm
    cos_theta = den / norm
    rotation[0, 0] = cos_theta
    rotation[0, 1] = -sin_theta
    rotation[1, 0] = sin_theta
    rotation[1, 1] = cos_theta
    # print("rot scale", rotation, scale)
    return rotation, scale
Пример #10
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
Пример #11
0
def getModel(imgs, points, directions, n):
    profiles = np.zeros((points.shape[0], points.shape[1], 2 * n + 1))

    for p in range(points.shape[1]):
        profiles[:, p, :] = getProfilesForPerson(imgs[p], points[:, p, :],
                                                 directions[:, p, :], n)

    covars = np.zeros((points.shape[0], 2 * n + 1, 2 * n + 1))
    means = np.zeros((points.shape[0], 2 * n + 1))

    for l in range(points.shape[0]):
        [covars[l], means[l]
         ] = cv2.calcCovarMatrix(profiles[l, :, :] * 1.0,
                                 cv2.cv.CV_COVAR_NORMAL | cv2.cv.CV_COVAR_ROWS)

    return covars, means
Пример #12
0
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    
Пример #13
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
Пример #14
0
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
Пример #15
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]
Пример #16
0
    def examine(self, frame, verticalTest=True, horizontalTest=True):
        try:
            frame_size = (frame.shape[1], frame.shape[0])
            # frame1 = np.array(frame,dtype=np.float)
            # rowSum = cv2.reduce(frame1,0,rtype = cv2.REDUCE_SUM)
            # try:
            #     startX = frame_size[0]//2 - rowSum[:frame_size[0]//2][::-1].tolist().index(0)
            # except:
            #     startX = 0
            #     pass
            # try:
            #     endX = frame_size[0]//2 + rowSum[frame_size[0]//2:].tolist().index(0)
            # except:
            #     endX = frame_size[0]
            #     pass

            # if startX == endX:
            #     startX = 0
            #     endX = frame_size[0]
            # points = cv2.findNonZero(frame[:,startX:endX])
            points = cv2.findNonZero(frame)
            stdDev = cv2.meanStdDev(points)[1]
            points = np.array(points[:, 0, :], dtype=np.float)
            mean = None
            res2 = cv2.calcCovarMatrix(points, mean,
                                       cv2.COVAR_ROWS + cv2.COVAR_NORMAL)
            covM = res2[0]
            corrCoef = covM[0, 1] / covM[0, 0]

            # point = res2[1][0,0]+startX,res2[1][0,1]
            point = res2[1][0, 0], res2[1][0, 1]
            # Verifying the Pearson correlation coffecients, the vertical line or the horizontal line
            isLinear = (np.abs(corrCoef) > self.inferiorCorrLimit) or (
                horizontalTest and stdDev[0] < self.lineThinkness * 0.341
                and stdDev[1] > frame_size[1] * 0.22) or (
                    verticalTest and stdDev[0] > frame_size[0] * 0.22
                    and stdDev[1] < self.lineThinkness * 0.341)
            return isLinear, point, len(points)
        except Exception as e:
            print("Except", e)
            return False, (0, 0)
Пример #17
0
def plotVariations(toothId=0, nbModes=15):
    # Read data (images and landmarks)
    landmarks = lm.readLandmarksOfTooth(toothId, trainingPersonIds)
    # Initialization of mean vector (xStriped), covariance matrix, x-vector, x-striped-vector (mean), eigenvectors (P) and eigenvalues.
    processedLandmarks = procrustes.procrustesMatrix(landmarks, 100)
    mean, eigvec = cv2.PCACompute(np.transpose(
        stackPoints(processedLandmarks)))
    # Calculate the eigenvalues and sort them
    covar, _ = cv2.calcCovarMatrix(
        stackPoints(processedLandmarks), cv2.cv.CV_COVAR_SCRAMBLED
        | cv2.cv.CV_COVAR_SCALE | cv2.cv.CV_COVAR_COLS)
    eigval = np.sort(np.linalg.eigvals(covar), kind='mergesort')[::-1]

    for mode in range(nbModes):
        for var in [-3, 0, 3]:
            tooth = mean + var * (eigval[mode]**0.5) * eigvec[mode]
            tooth = unstackPointsForPerson(np.transpose(tooth))
            # plot the tooth
            pt.plotTooth(tooth)
        # show the variations
        pt.show()
Пример #18
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)]
Пример #19
0
def ellipse_variance(contour):
    '''
    Calculate the ellipse variance
    Parameters
    ----------
    contour:    OpenCV contour : Contour of the ROI

    Returns
    -------
    ellipse variance

    '''
    ellipse = cv2.fitEllipse(contour)
    poly =\
        cv2.ellipse2Poly((int(ellipse[0][0]), int(ellipse[0][1])), (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2)), int(ellipse[2]), 0, 360, 5)
    poly = poly.astype('float64')
    covar, _ = cv2.calcCovarMatrix(poly,
                                   0,
                                   flags=cv2.COVAR_NORMAL + cv2.COVAR_ROWS)
    inv_covar = np.linalg.inv(covar + np.finfo(float).eps)
    gravity = center_of_gravity(contour)
    di = np.zeros((1, poly.shape[0]))
    i = 0
    mr = 0
    for point in poly:
        vt = point - gravity
        v = np.transpose(vt)
        d = np.sqrt(np.matmul(np.matmul(vt, inv_covar), v))
        di[0, i] = d
        mr += d
        i += 1

    mr = mr / i

    di = di - mr
    di = di * di
    dr = np.sqrt(np.sum(di) / i)
    return dr
def featureEng(inp):
    '''
    input: np array. #samples x #channels x widht and height
    return: for each sample,
            total brightness and std along the principle component
    '''
    Nf = 2  # num of features
    (NN, Nl) = inp.shape[:2]
    out = np.zeros((NN, Nf * Nl))
    for i in range(NN):
        if not (i % 10000):
            print('Processing', i, '/', NN)
        for j in range(Nl):
            mat, _, _, ss = frame2matrix(inp[i, j, :, :])
            if ss == 0:
                out[i, j * Nf:j * Nf + Nf] = (0, 0)
                continue
            covar, _ = cv2.calcCovarMatrix(mat, np.array([0., 0.]),
                                           cv2.COVAR_ROWS | cv2.COVAR_NORMAL)
            _, _, eVec = cv2.eigen(covar)
            proj = np.dot(eVec[0], mat.T)
            out[i, j * Nf:j * Nf + Nf] = (ss, proj.std())
    return out
Пример #21
0
#!/usr/bin/end python

import numpy as np
import cv2
import cv

if __name__ == '__main__':
	m = np.asmatrix([[1,2],[1,2]], np.float32)
	print np.cov(m)
	
	covmat,mean = cv2.calcCovarMatrix(m, cv2.cv.CV_COVAR_NORMAL | cv2.cv.CV_COVAR_COLS, ctype=cv2.CV_32F)
	print covmat
	print mean
Пример #22
0
# 0423.py
import cv2
import numpy as np

X = np.array([[0, 0, 0, 100, 100, 150, -100, -150],
              [0, 50, -50, 0, 30, 100, -20, -100]],
             dtype=np.float64)

# 전치행렬 변경
X = X.transpose()  # X = X.T

cov, mean = cv2.calcCovarMatrix(X,
                                mean=None,
                                flags=cv2.COVAR_NORMAL + cv2.COVAR_ROWS)
print('mean=', mean)
print('cov=', cov)

# 공분산 행렬 cov 의 역행렬 icov 계산
ret, icov = cv2.invert(cov)
print('icov=', icov)

v1 = np.array([[0], [0]], dtype=np.float64)
v2 = np.array([[0], [50]], dtype=np.float64)

# 벡터 v1, v2 사이의 마하라노비스 통계적 거리는 공분산 행렬의
#   역행렬을 이용해서 계산
dist = cv2.Mahalanobis(v1, v2, icov)
print('dist = ', dist)

cv2.waitKey()
cv2.destroyAllWindows()
Пример #23
0
#
# Q4: Calculate the Covariance Matrix of each of the groups.
#

flags = cv2.COVAR_NORMAL | cv2.COVAR_ROWS

grouped_data = []
icovariance = []
for c in range(0, 10):
    grouped_data.append([])
    for i in range(0, len(digits)):
        if labels[i] == c:
            grouped_data[c].append(digits[i])
    print 'group:', c, len(grouped_data[c])
    x = np.array(grouped_data[c])
    covar, mean = cv2.calcCovarMatrix(x, flags)
    icovar = np.empty(covar.shape, covar.dtype)
    cv2.invert(covar, icovar, cv2.DECOMP_SVD)
    icovariance.append(icovar)


#
# Q5: For each group, calculate the Mahalanobis distance of every element
#     to the centroid, then draw the centroid followed by the three farthest
#     elements (showing their Mahalanobis distance).
#

for index, centroid in enumerate(centroids):
    points = np.float32(grouped_data[index])
    icovar = np.float32(icovariance[index])
Пример #24
0
#
# Q4: Calculate the Covariance Matrix of each of the groups.
#

flags = cv2.COVAR_NORMAL | cv2.COVAR_ROWS

grouped_data = []
icovariance = []
for c in range(0, 10):
    grouped_data.append([])
    for i in range(0, len(digits)):
        if labels[i] == c:
            grouped_data[c].append(digits[i])
    print 'group:', c, len(grouped_data[c])
    x = np.array(grouped_data[c])
    covar, mean = cv2.calcCovarMatrix(x, flags)
    icovar = np.empty(covar.shape, covar.dtype)
    cv2.invert(covar, icovar, cv2.DECOMP_SVD)
    icovariance.append(icovar)

#
# Q5: For each group, calculate the Mahalanobis distance of every element
#     to the centroid, then draw the centroid followed by the three farthest
#     elements (showing their Mahalanobis distance).
#

for index, centroid in enumerate(centroids):
    points = np.float32(grouped_data[index])
    icovar = np.float32(icovariance[index])

    mdistance = []
    key = cv2.waitKey(10)
    if key == 27:
        break

    elif key == ord('s'):
        cv2.imwrite('eigenimages/image' + str(id) + '.png', image_toshow)
        np.savetxt('eigenimages/image' + str(id) + '.txt', np.array(weights))

    elif key == ord('r'):
        for v in range(N):
            cv2.setTrackbarPos("weight" + str(v), "Eigenimages", 100)

cv2.destroyAllWindows()

print('please wait ...')
covariance_matrix, mean = cv2.calcCovarMatrix(
    images, None, cv2.COVAR_ROWS | cv2.COVAR_NORMAL | cv2.COVAR_SCALE)
_, eigenvalues, eigenvectors = cv2.eigen(covariance_matrix)
print('... done')
eigenvalues = np.reshape(eigenvalues, (eigenvalues.shape[0], ))
eigenvalues = np.abs(eigenvalues)
eigenvalues = np.flip(np.sort(eigenvalues))

plt.figure(figsize=[8, 6])
plt.plot(eigenvalues, 'r', linewidth=2.0)
plt.legend(['abs(eigenvalues)'], fontsize=18)
plt.xlabel('order ', fontsize=16)
plt.ylabel('abs', fontsize=16)
plt.title('Eigenvalues', fontsize=16)
plt.savefig('eigenimages\eigenvalues.png')

plt.figure(figsize=[8, 6])
Пример #26
0
    dirName = "./step1/data/"
    images = readImages(dirName)
    print(len(images))
    #Size of images
    sz = images[0].shape
    print(sz)
    #Create data matrix for PCA
    data = createDataMatrix(images)
    print("Data")
    print(data)
    print("Calculating PCA ")
    mean, eigenVectors = cv2.PCACompute(data,
                                        mean=None,
                                        maxComponents=NUM_EIGEN_FACES)

    covar, mean2 = cv2.calcCovarMatrix(
        data, 0, cv2.COVAR_SCALE | cv2.COVAR_ROWS | cv2.COVAR_SCRAMBLED)
    print("Mean 1")
    print(mean)
    print("Mean 2")
    print(mean2)
    print("DONE")
    print("Covar ", covar)
    averageFace = mean2.reshape(sz)

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

    eigenFaces = []
    for eigenVector in eigenVectors:
        eigenFace = eigenVector.reshape(sz)
        eigenFaces.append(eigenFace)
Пример #27
0
def plow(img):
    diffImg = nonLocalGrouping(img)
    diffImg = diffImg.reshape((-1, 1))
    diffImg = np.float32(diffImg)
    # Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
    criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    K = 15
    ret, label, center = cv.kmeans(diffImg, K, None, criteria, 10,
                                   cv.KMEANS_RANDOM_CENTERS)

    # Now convert back into uint8, and make original image
    center = np.uint8(center)
    res = center[label.flatten()]
    kMeansRes = res.reshape((img.shape))

    roi = cv.selectROI("Select ROI", img)
    cv.destroyAllWindows()
    noiseImg = img[int(roi[1]):int(roi[1] + roi[3]),
                   int(roi[0]):int(roi[0] + roi[2])]

    #standard deviation of noise
    std = np.std(noiseImg)
    kernel = np.zeros((2, 2))
    kernel[0][0] = 2
    kernel[1][0] = -1
    kernel[0][1] = -1
    filtered = cv.filter2D(noiseImg, -1, kernel)
    filtered = filtered.reshape((-1, 1))
    filtered = np.float32(filtered)
    filtered = filtered / math.sqrt(6)
    delMed = np.median(filtered)
    while delMed == 0:
        min, max, minLoc, maxLoc = cv.minMaxLoc(filtered)
        x = minLoc[0]
        y = minLoc[1]
        filtered[y, x] = max + 1
        delMed = np.median(filtered)
    delMed = abs(delMed)
    # filtered = noiseImg - delMed
    med = np.median(delMed)
    sigma = 1.4826 * med
    if sigma == 0:
        return
    h2 = 1.75 * pow(sigma, 2)
    filtred = median(img.copy(), 5)
    n = 5

    identity = pow(sigma, 2) * np.identity(n)
    height, width = img.shape

    window = (n - 1) / 2
    meanPatches = {}
    divideWith = {}
    covarianceSamples = {}
    filteredWithBorder = np.zeros([int(height + n), int(width + n)], np.uint32)
    filteredWithBorder[window:height + window, window:width + window] = img
    imgWithBorder = filteredWithBorder

    for x in range(width):
        for y in range(height):
            currX = x + window
            currY = y + window
            currentPatch = filteredWithBorder[int(currY -
                                                  window):int(currY + window +
                                                              1),
                                              int(currX -
                                                  window):int(currX + window +
                                                              1)]
            ret, divideWithCoeffs = cv.threshold(np.float32(currentPatch), 2,
                                                 1, cv.THRESH_BINARY)
            if kMeansRes[y, x] in meanPatches:
                meanPatches[kMeansRes[y, x]] = np.add(
                    meanPatches[kMeansRes[y, x]], currentPatch)
                divideWith[kMeansRes[y,
                                     x]] = np.add(divideWith[kMeansRes[y, x]],
                                                  divideWithCoeffs)
                covHeight = -1
                covWidth = -1
                if (kMeansRes[y, x] in covarianceSamples):
                    covHeight, covWidth = covarianceSamples[kMeansRes[y,
                                                                      x]].shape
                if (covWidth < n and currX > 2 * window
                        and currX < width - (2 * window) and currY > 2 * window
                        and currY < height - (2 * window)):
                    currentPatch = currentPatch.reshape((-1, 1))
                    if kMeansRes[y, x] in covarianceSamples:
                        covarianceSamples[kMeansRes[y, x]] = np.hstack(
                            (covarianceSamples[kMeansRes[y, x]], currentPatch))
                        # covariances[kMeansRes[y, x]] = np.append(covariances[kMeansRes[y, x]], currentPatch, 1)
                    else:
                        covarianceSamples[kMeansRes[y, x]] = currentPatch
            else:
                meanPatches[kMeansRes[y, x]] = currentPatch
                divideWith[kMeansRes[y, x]] = divideWithCoeffs
                if (currX > 2 * window and currX < width - (2 * window)
                        and currY > 2 * window
                        and currY < height - (2 * window)):
                    currentPatch = currentPatch.reshape((-1, 1))
                    covarianceSamples[kMeansRes[y, x]] = currentPatch

    toDel = []
    for i in covarianceSamples:
        covheight, covwidth = covarianceSamples[i].shape
        if covwidth < n:
            toDel.append(i)
    for i in list(toDel):
        del covarianceSamples[i]
        del meanPatches[i]

    covariances = {}
    for key in meanPatches.keys():
        currentPatch = meanPatches[key]
        divideWithPatch = divideWith[key]
        heightPatch, widthPatch = currentPatch.shape
        for x in range(widthPatch):
            for y in range(heightPatch):
                currentPatch[y, x] = currentPatch[y, x] / divideWithPatch[y, x]
        meanPatches[key] = currentPatch

        meanOut = np.zeros(covarianceSamples[key].shape)
        covMat, test2 = cv.calcCovarMatrix(np.float32(covarianceSamples[key]),
                                           np.float32(meanOut),
                                           cv.COVAR_NORMAL | cv.COVAR_ROWS)
        covMat = covMat - identity
        eigenVals, eigenVect = la.eig(covMat)
        for x in range(eigenVals.size):
            if eigenVals[x] < 0:
                eigenVals[x] = 0.0001
        eigenVals = np.real(eigenVals)
        eigenVect = np.real(eigenVect)
        covariances[key] = eigenVect * eigenVals * np.transpose(eigenVect)

    resultImg = np.zeros(imgWithBorder.shape, np.uint64)
    divideWith = np.zeros(filteredWithBorder.shape, np.uint64)

    similarPatches = {}
    #half of the actual size
    searchWindow = 5
    for x in range(width):
        for y in range(height):
            currX = x + window
            currY = y + window
            currClass = kMeansRes[y, x]
            if currClass not in meanPatches:
                continue
            currentPatch = filteredWithBorder[int(currY -
                                                  window):int(currY + window +
                                                              1),
                                              int(currX -
                                                  window):int(currX + window +
                                                              1)]

            weights = []
            patches = []
            meanPatch = np.zeros(currentPatch.shape)
            divideWithCoeffs = np.ones(currentPatch.shape)
            sumWeights = np.ones(currentPatch.shape)
            for k in range(width):
                if k < x - searchWindow:
                    continue
                if k > x + searchWindow:
                    break
                for l in range(height):
                    if l < y - searchWindow:
                        continue
                    if l > y + searchWindow:
                        break
                    if (k != x and l != y and kMeansRes[l, k] == currClass):
                        currK = k + window
                        currL = l + window
                        comparePatch = filteredWithBorder[
                            int(currL - window):int(currL + window + 1),
                            int(currK - window):int(currK + window + 1)]

                        patchHeight, patchWidth = comparePatch.shape
                        for patchX in range(patchWidth):
                            for patchY in range(patchHeight):
                                if comparePatch[patchY, patchX] == 0:
                                    comparePatch[patchY,
                                                 patchX] = currentPatch[patchY,
                                                                        patchX]
                                elif currentPatch[patchY, patchX] == 0:
                                    currentPatch[patchY,
                                                 patchX] = comparePatch[patchY,
                                                                        patchX]

                        truePatch = imgWithBorder[int(currY -
                                                      window):int(currY +
                                                                  window + 1),
                                                  int(currX -
                                                      window):int(currX +
                                                                  window + 1)]
                        ret, divideWith = cv.threshold(np.float32(truePatch),
                                                       2, 1, cv.THRESH_BINARY)
                        meanPatch = meanPatch + truePatch
                        divideWithCoeffs = divideWithCoeffs + divideWith
                        weight = np.zeros(comparePatch.shape, np.float32)
                        for patchX in range(patchWidth):
                            for patchY in range(patchHeight):
                                val1 = currentPatch[patchY, patchX]
                                val1 = val1.astype(np.int32)
                                val2 = comparePatch[patchY, patchX]
                                val2 = val2.astype(np.int32)
                                test = pow(val1 - val2, 2)
                                test = math.exp(-test / h2)
                                weight[patchY, patchX] = test
                        val = (1 / pow(sigma, 2))
                        weight = val * weight
                        # weight = weight.reshape((-1, 1))
                        weights.append(weight)
                        sumWeights = sumWeights + weight
                        patches.append(comparePatch)

            firstSum = 0
            secondSum = 0
            meanPatch = meanPatch / divideWithCoeffs
            # meanPatch = meanPatch.reshape((-1, 1))
            mat = la.inv(sumWeights * covariances[currClass] + np.identity(n))
            for i in range(len(weights)):
                wp = (weights[i] * patches[i])
                ws = wp / sumWeights
                firstSum = firstSum + ws
                firstTerm = (weights[i] / sumWeights)
                thirdTerm = meanPatch - patches[i]
                secondSum = secondSum + firstTerm * mat * thirdTerm
            newPatch = firstSum + secondSum
            currentPatch = resultImg[int(currY - window):int(currY + window +
                                                             1),
                                     int(currX - window):int(currX + window +
                                                             1)]
            newPatch = newPatch + currentPatch
            resultImg[int(currY - window):int(currY + window + 1),
                      int(currX - window):int(currX + window + 1)] = newPatch
        print(x)

    resultImg = util.normalize(resultImg.copy(), 255)
    resultImg = np.uint8(resultImg)

    return resultImg
Пример #28
0
    dirName = "Netural_Images"

    # Read FaceImages
    images = readImages(dirName)

    # Size of FaceImages, 为了让后续生成的一系列row vector通过reshape变回图像的(193,162,3)的np.array
    sz = images[0].shape
    # print('shape of sz is', sz)

    # Create original faceimages matrix for PCA, each faceimage is a row vector
    matrix_faces = createDataMatrix(images)
    matrix_faces_T = matrix_faces.T
    print('Shape of matrix_faces is ', np.shape(matrix_faces))
    # covar, mean_1 = cv2.calcCovarMatrix(matrix_faces, mean=None, flags=cv2.COVAR_ROWS|cv2.COVAR_SCRAMBLED)
    covar, mean = cv2.calcCovarMatrix(matrix_faces,
                                      mean=None,
                                      flags=cv2.COVAR_SCALE | cv2.COVAR_ROWS
                                      | cv2.COVAR_SCRAMBLED)
    # mean_x is same as mean_1,  cv2.COVAR_SCALE 相当于1/M, because the default is 1

    print("shape of mean is ", np.shape(mean))
    print("shape of covar is ", np.shape(covar))
    print('covar is', covar)

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

    # 对eVec进行一系列操作,(ui)T * (A)T,再进行normalization,因为OpenCV都是row vector运算,所以跟论文中相当于都要进行转置
    eVec = cv2.gemm(eVec, matrix_faces - mean_x, 1, None, 0)
    eVec = np.apply_along_axis(lambda n: cv2.normalize(n, n).flat, 1, eVec)
    #     print("shape of eVal is ", np.shape(eVal))
    #     # print(eVal)
    #     # mean_SE = pd.DataFrame.from_dict(mean_SE, orient='index', columns=['values'])
Пример #29
0
def findSegments():
    # load training radiographs
    images = rg.readRadiographs(trainingPersonIds)
    if debugFB: print 'DB: Training radiographs loaded'

    for personToFitId in personToFitIds:
        # load radiograph to determine segments for
        imageToFit = rg.readRadioGraph(personToFitId)
        if debugFB:
            print 'DB: Radiograph to fit (#' + str(personToFitId +
                                                   1) + ') loaded'

        contourImage = imageToFit.copy()
        segmentsImage = np.zeros_like(np.array(imageToFit))

        if autoInitPoints:
            init_points = ip.getModelPointsAutoWhole(personToFitId)

        for i in range(toothIds.shape[0]):
            toothId = toothIds[i]

            # Read data (images and landmarks)
            landmarks = lm.readLandmarksOfTooth(toothId, trainingPersonIds)
            if debugFB:
                print '   DB: Landmarks loaded for tooth #' + str(toothId + 1)

            # Initialization of mean vector (xStriped), covariance matrix, x-vector, x-striped-vector (mean), eigenvectors (P) and eigenvalues.
            processedLandmarks = procrustes.procrustesMatrix(landmarks, 100)
            if debugFB:
                print '   DB: Procrustes ready for tooth #' + str(toothId + 1)
            pcMean, pcEigv = cv2.PCACompute(
                np.transpose(stackPoints(processedLandmarks)))
            if debugFB: print '   DB: PCA ready for tooth #' + str(toothId + 1)
            xStacked = xStriped = np.transpose(pcMean)

            covar, _ = cv2.calcCovarMatrix(
                stackPoints(processedLandmarks), cv2.cv.CV_COVAR_SCRAMBLED
                | cv2.cv.CV_COVAR_SCALE | cv2.cv.CV_COVAR_COLS)
            eigval = np.sort(np.linalg.eigvals(covar), kind='mergesort')[::-1]

            # Number of modes
            coverage = 0
            nbModes = 0
            eigval_total = np.sum(eigval)
            for value in eigval:
                coverage += value / eigval_total
                nbModes += 1
                if coverage >= 0.99:
                    break

            P = np.transpose(pcEigv[:nbModes])  # normalized
            eigval = eigval[:nbModes]

            # Initialization of the initial points
            if autoInitPoints: X = init_points[:, toothId, :]
            else: X = ip.getModelPointsManually(personToFitId, toothId)

            # Draw the initial points
            initialImage = imageToFit.copy()
            cv2.polylines(initialImage, np.int32([X]), True, 255, thickness=2)
            #showScaled(initialImage, windowscale, 'initial', False)

            # Initialize the model
            directions = profile.getDirections(landmarks)
            model = profile.getModel(images, landmarks, directions, nModel)

            ## Protocol 1: step 1 (initialize shape parameters)
            b = np.zeros((nbModes, 1))
            prev_b = b

            stop = False
            it = 1
            while (not stop):
                # Protocol 2: step 1 (examine region around each point to find best nearby match)
                Y = profile.getNewModelPoints(imageToFit, X, model, nSample)

                # Protocol 2: step 3 = protocol 1

                ## Protocol 1: step 2 (generate model point positions)
                xStacked = xStriped + np.dot(P, b)

                ## Protocol 1: step 3 & 4 (project Y into the model coordinate frame)
                y, translation = procrustes.procrustesTranslateMatrixForPerson(
                    Y)
                scale, rotation = procrustes.alignShapes(
                    unstackPointsForPerson(xStacked), y)
                translation = -translation

                y = procrustes.rotateMatrixForPerson(y, -rotation)
                y = procrustes.scaleMatrixForPerson(y, 1 / scale)

                ## Protocol 1: step 5 --> NOT NEEDED??
                #yStacked = stackPointsForPerson(y)
                #yStacked = yStacked/np.dot(np.transpose(yStacked), xStriped)
                #y = unstackPointsForPerson(yStacked)

                ## Protocol 1: step 6 (update model parameters)
                b = np.dot(np.transpose(P),
                           (stackPointsForPerson(y) - xStriped))

                # Protocol 2: step 3 (apply constraints to b)
                #mahalonobis = np.sqrt(np.sum(b**2)/np.sum(eigval))
                #if mahalonobis > 3.0:
                #   b = b*(3.0/mahalonobis)

                for i in range(b.shape[0]):
                    if np.abs(b[i, 0]) > 3 * np.sqrt(eigval[i]):
                        b[i, 0] = np.sign(b[i, 0]) * 3 * np.sqrt(eigval[i])

                # Calculate difference with previous result
                # and stop iterating after a certain threshold
                differences = prev_b - b
                prev_b = b

                stop = True
                for diff in differences:
                    if np.abs(diff) > 0.01:
                        stop = False

                it += 1

            if debugFB:
                print '   DB: Tooth #' + str(
                    toothId + 1) + ' finished in ' + str(it) + ' iterations.'

            # Draw the model on the radiograph
            x = unstackPointsForPerson(xStriped + np.dot(P, b))
            X = procrustes.rotateMatrixForPerson(x, rotation)
            X = procrustes.scaleMatrixForPerson(X, scale)
            X = procrustes.translateMatrixForPerson(X, translation)

            cv2.polylines(contourImage, np.int32([X]), True, 255, thickness=2)
            #showScaled(contourImage, windowscale, 'contours', True)

            cv2.fillPoly(segmentsImage, np.int32([[X]]), 128)
            #showScaled(segmentsImage, windowscale, 'segments', True)

        #cv2.imwrite('C:/Users/samue_000/Desktop/' + str(personToFitId+1) + 'i.jpg', initialImage)
        #cv2.imwrite('C:/Users/samue_000/Desktop/' + str(personToFitId+1) + 'c.jpg', contourImage)
        #cv2.imwrite('C:/Users/samue_000/Desktop/' + str(personToFitId+1) + 's.jpg', segmentsImage)
        showScaled(contourImage, windowscale, 'contours', True)

        print 'DB: Radiograph #' + str(personToFitId + 1) + ' is segmented.'
Пример #30
0
def check_building(img, H):
    h, w, c = img.shape
    #	buildingflag = np.zeros((w,1))
    buildingflag = np.array([0 for i in range(w)])
    for x in range(w - 1):
        if abs(H[x] - H[x + 1]) > h / 4:
            if H[x] < H[x + 1]:
                buildingflag[x] = 1
            else:
                buildingflag[x + 1] = 1
    if sum(buildingflag) < 1:
        return H

    total_sample = sum(H)
    num_building = sum(H[buildingflag == 1])
    buildingsample = np.zeros((num_building, 3))
    skysample = np.zeros((total_sample - num_building, 3))
    skyidx = 0
    buildingidx = 0
    for x in range(w):
        if buildingflag[x] == 0:
            for y in range(H[x]):
                skysample[skyidx, 0] = img[y, x, 0]
                skysample[skyidx, 1] = img[y, x, 1]
                skysample[skyidx, 2] = img[y, x, 2]
                skyidx = skyidx + 1
        else:
            for y in range(H[x]):
                buildingsample[buildingidx, 0] = img[y, x, 0]
                buildingsample[buildingidx, 1] = img[y, x, 1]
                buildingsample[buildingidx, 2] = img[y, x, 2]
                buildingidx = buildingidx + 1
    skycov, skymean = cv2.calcCovarMatrix(skysample,
                                          cv2.COVAR_ROWS | cv2.COVAR_NORMAL)
    buildingcov, buildingmean = cv2.calcCovarMatrix(
        buildingsample, cv2.COVAR_ROWS | cv2.COVAR_NORMAL)
    ret, skycovInv = cv2.invert(skycov)
    ret, buildingcovInv = cv2.invert(buildingcov)
    for x in range(w):
        if buildingflag[x] == 1:
            continue
        idx = 0
        sample = np.zeros((H[x], 3))
        for y in range(H[x]):
            sample[idx, 0] = img[y, x, 0]
            sample[idx, 1] = img[y, x, 1]
            sample[idx, 2] = img[y, x, 2]
            idx = idx + 1
        num_sample = sample.shape[0]
        ss = sample - np.tile(skymean, (num_sample, 1))
        dsky = np.dot(np.dot(ss, skycovInv), ss.T)
        ss = sample - np.tile(buildingmean, (num_sample, 1))
        dbuilding = np.matrix(ss) * np.matrix(buildingcovInv) * np.matrix(ss).T
        dsky = np.diag(dsky)
        dbuilding = np.diag(dbuilding)
        num_sky = sum(dsky < dbuilding)
        if num_sky * 2 < num_sample:
            buildingflag[x] = 1
    for x in range(w):
        if buildingflag[x] == 1:
            H[x] = 0
    return (H)
Пример #31
0
def getOrientation(contours, img):
    array = contours.flatten()
    cv2.calcCovarMatrix(array, cv2.cv.CV_COVAR_SCALE | cv2.cv.CV_COVAR_ROWS)
Пример #32
0
 def fit_quality(self, point_index, model_factors, test_image, test_shape):
     model_patch = self.generate_grey(point_index, model_factors)
     test_patch = self._get_normal_grey_levels_for_single_point_single_image(test_image, test_shape, point_index)
     return cv2.Mahalanobis(model_patch, test_patch,
                            np.linalg.pinv(cv2.calcCovarMatrix(np.concatenate((model_patch, test_patch)))))
Пример #33
0
def pcaBuiltIn(landmarks):
    mean, vec = cv.PCACompute(landmarks, None)
    covar, _ = cv.calcCovarMatrix(
        landmarks,
        cv.cv.CV_COVAR_SCRAMBLED | cv.cv.CV_COVAR_SCALE | cv.cv.CV_COVAR_COLS)
    return covar