Ejemplo n.º 1
0
def train():
    training_set = []
    training_labels = []
    os.chdir("/Users/muyunyan/Desktop/EC500FINAL/logo/")
    counter = 0
    a = os.listdir(".")
    for i in a:
        os.chdir(i)
        print(i)
        for d in os.listdir("."):
            img = cv2.imread(d)
            res = cv2.resize(img, (250, 250))
            gray_image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
            xarr = np.squeeze(np.array(gray_image).astype(np.float32))
            m, v = cv2.PCACompute(xarr)
            arr = np.array(v)
            flat_arr = arr.ravel()
            training_set.append(flat_arr)
            training_labels.append(i)
        os.chdir("..")
        trainData = training_set
        responses = training_labels
        svm = svm.SVC()
        svm.fit(trainData, responses)
        return svm
Ejemplo n.º 2
0
 def __init__(self, image_feature, max_components):
     assert image_feature.ndim == 3
     feature = np.reshape(image_feature, (-1, image_feature.shape[2]))
     _mean = np.mean(feature, axis=0, keepdims=True)
     self.mean, self.eigen_vecs = cv2.PCACompute(
         feature, _mean, maxComponents=max_components)
     print('\tPCA computed!')
Ejemplo n.º 3
0
def is_line(image):
    if not have_solid_field(image):
        return False
    pixels = np.vstack(image.nonzero()).transpose().astype(np.float32)
    mean, eigenvectors = cv2.PCACompute(pixels, mean=None)
    projects = cv2.PCAProject(pixels, mean, eigenvectors)
    return np.std(projects, axis=0)[1] < 1
Ejemplo n.º 4
0
def pointing_line(gray, x, y, R, perimeter):
    epsilon = 0.4
    r = int(epsilon * R)

    img = gray[x - r:x + r, y - r:y + r]
    #thresh
    ret, thresh = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)
    #if thresh is not None:
    #conv
    #kernel = np.ones((9,9),np.uint8)
    #thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

    #countor
    _, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                              cv2.CHAIN_APPROX_NONE)

    if contours != []:
        perimeter = []
        #max perimeter
        for cnt in contours[1:]:
            perimeter.append(cv2.arcLength(cnt, True))
    if perimeter != []:
        maxindex = perimeter.index(max(perimeter))

        X = np.array(contours[maxindex + 1], dtype=np.float).reshape(
            (contours[maxindex + 1].shape[0], contours[maxindex + 1].shape[2]))
        mean, eigenvectors = cv2.PCACompute(X,
                                            mean=np.array([], dtype=np.float),
                                            maxComponents=1)
        vec = (-eigenvectors[0][0], -eigenvectors[0][1])
        drawAxis(gray, (x, y), vec, (0, 255, 0), R)
def pca_to_grey(image, mask, inverted=True):
    x,y,z = image.shape
    mat = image.reshape([x*y,z])
    filter_array = mask.reshape([x*y])
    pointlist = mat[filter_array > 0]
    mean, eigenvectors = cv2.PCACompute(pointlist, mean=None)
    axis = eigenvectors[0,:].reshape([3])

    newmat = np.dot(mat.astype(np.float32) - mean, axis)
    newpoints = newmat[filter_array > 0]
    Q1, Q3 = np.percentile(newpoints, 25), np.percentile(newpoints,75)
    iqr = Q3 - Q1
    cut_off_val = iqr * 1.5
    lower_bound= Q1 - cut_off_val
    non_outliers = [x for x in newpoints if x >= lower_bound]
    new_max = max(non_outliers)
    new_min = min(newpoints)
    np.clip(newpoints, new_min, new_max, out=newpoints)
    newpoints = np.asarray(newpoints, dtype=np.float64)
    rescale = np.interp(newmat, (np.min(newpoints), np.max(newpoints)), (0,255))
    rescale = np.around(rescale).astype(np.uint8)
    grey = rescale.reshape([x,y])
    if inverted:
        grey = cv2.bitwise_not(grey)
    pigment = cv2.bitwise_and(grey, mask)
    return pigment
Ejemplo n.º 6
0
 def linePtCloud(self, pts3D):
     if pts3D !=[]:
         mean, eigenVectors = cv2.PCACompute(np.transpose(pts3D), mean=None)
         line = np.concatenate((np.ravel(mean), eigenVectors[0, :]), axis=0)
     else:
         line = np.array([0, 0, 0, 0, 0, 0])
     return line
Ejemplo n.º 7
0
def compute_PCA(image: np.ndarray, display=False) -> tuple:
    mat = np.argwhere(image != 0)
    mat[:, [0, 1]] = mat[:, [1, 0]]
    mat = np.array(mat).astype(np.float32)  # have to convert type for PCA

    # mean (e. g. the geometrical center)
    # and eigenvectors (e. g. directions of principal components)
    m, e = cv2.PCACompute(mat, mean=np.array([]))

    # now to draw: let's scale our primary axis by 100,
    # and the secondary by 50
    centre = tuple(m[0])
    endpoint1 = tuple(m[0] + e[0] * 100)
    endpoint2 = tuple(m[0] + e[1] * 50)

    if display:
        img_show = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

        cv2.circle(img_show, centre, 5, (255, 255, 0))
        cv2.line(img_show, centre, endpoint1, (255, 255, 0))
        cv2.line(img_show, centre, endpoint2, (255, 255, 0))

        show(img_show)

    return centre, endpoint1, endpoint2
def template_match(image):
    f = open("standard deviation.txt", "a")
    global template_image
    res = cv2.matchTemplate(image, template_image, cv2.TM_CCOEFF)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    h, w, _ = template_image.shape
    bottom_right = (max_loc[0] + w, max_loc[1] + h)
    cv2.rectangle(image, max_loc, bottom_right, 255, 2)
    cv2.imshow("image", image)
    template_image_new = image[max_loc[1]:max_loc[1] + h,
                               max_loc[0]:max_loc[0] + w]
    manhattan_deviation = np.sum(template_image - template_image_new)
    print "Manhattan " + str(manhattan_deviation)
    euclidean_deviation = np.sqrt(
        np.sum(np.square(template_image - template_image_new)))
    print "Euclidean " + str(euclidean_deviation)
    standard_deviation = np.sqrt(
        np.sum(np.square(template_image - template_image_new)) /
        np.size(template_image))
    print "Standard " + str(standard_deviation)

    mean = np.mean(template_image, axis=0)
    mean, eigenVector = cv2.PCACompute(template_image, mean).reshape(1, -1)
    print eigenVector
    template_image = template_image_new
    cv2.imwrite("templates/template_picture" + str(time.time()) + ".jpg",
                template_image,
                params=[])
    f.write(str(standard_deviation) + "\n")
    cv2.imshow("image_crop", template_image)
    f.close()
    return 0
Ejemplo n.º 9
0
def process(src, dest):
    fd = open(os.path.join(src, 't10k-images-idx3-ubyte'), 'rb')
    arr = loadData(
        fd, 10000
    )[:
      7000, :]  #the test set has 10k samples but to avoid timeout we only take the first 7000
    print arr.shape

    #perform PCA
    mean, eigenvectors = cv2.PCACompute(arr, mean=None, retainedVariance=0.9)
    compressed = arr.dot(np.transpose(eigenvectors))
    print compressed.shape

    #sort images by k-means
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 1000, 0.99)
    flags = cv2.KMEANS_RANDOM_CENTERS
    compactness, labels, centers = cv2.kmeans(compressed, 10, None, criteria,
                                              10, flags)
    print "K-means done"

    for i in xrange(10):
        os.mkdir(os.path.join(dest, "[" + str(i) + "]"))

    for i in xrange(arr.shape[0]):
        img = arr[i].reshape((28, 28))
        cv2.imwrite(os.path.join(dest, str(labels[i]), str(i) + ".png"), img)
    print "Images saved"
Ejemplo n.º 10
0
def train_pca(train_file, dimension=100):
    """
    Performs Principle Component Analysis of the training data and save mean, eigenvectors
    """
    print 'PCA for grasp training data...\n'
    f_feat = open(train_file, "r")
    first_line = f_feat.readline()
    tags = first_line.split(',')
    idx_f = -1
    idx_s = -1
    for i in range(len(tags)):
        if tags[i].find("feature") == 0:
            idx_f = i
        if tags[i] == "side":
            idx_s = i
    assert (idx_f != -1 and idx_s != -1)
    features = f_feat.readlines()
    feature_listOfList = []
    for item in features:
        item_split = item.split(',')
        #only process right hand; modify this if double hands are to be process
        if "left" == item_split[idx_s]:
            continue
        feature_listOfList.append(item_split[idx_f:len(item_split) - 1])
    dataMatrix = np.array(feature_listOfList, dtype=np.float32)
    print dataMatrix.shape, dataMatrix.dtype
    mean, eigenvectors = cv2.PCACompute(data=dataMatrix,
                                        maxComponents=dimension)
    print mean.shape, eigenvectors.shape

    return [mean, eigenvectors]
Ejemplo n.º 11
0
def getPrincipalAxes(imgp):
    mean = np.empty((0))
    mean, eigen = cv2.PCACompute(imgp, mean)
    x = [eigen[0][0], eigen[1][0]]
    y = [eigen[0][1], eigen[1][1]]
    rotation = getAngle(horizontal, x, False)
    return x, y, np.ravel(mean), rotation
Ejemplo n.º 12
0
def visualize_span_points(name, small, span_points, corners):

    display = small.copy()

    for i, points in enumerate(span_points):

        points = norm2pix(small.shape, points, False)

        mean, small_evec = cv2.PCACompute(points.reshape((-1, 2)),
                                          None,
                                          maxComponents=1)

        dps = np.dot(points.reshape((-1, 2)), small_evec.reshape((2, 1)))
        dpm = np.dot(mean.flatten(), small_evec.flatten())

        point0 = mean + small_evec * (dps.min() - dpm)
        point1 = mean + small_evec * (dps.max() - dpm)

        for point in points:
            cv2.circle(display, fltp(point), 3, CCOLORS[i % len(CCOLORS)], -1,
                       cv2.LINE_AA)

        cv2.line(display, fltp(point0), fltp(point1), (255, 255, 255), 1,
                 cv2.LINE_AA)

    cv2.polylines(display, [norm2pix(small.shape, corners, True)], True,
                  (255, 255, 255))

    debug_show(name, 3, 'span points', display)
def extract_features(X, V=None, m=None, num_components=None):
    """Performs feature extraction

        This function is used to extract features from the dataset
        (currently only PCA is supported).
        It can also be used to preprocess a single data sample using some
        previously obtained PCA output. This makes it possible to obtain a
        set of basis vectors from an entire training set and apply this same
        set of basis vectors to a single test sample.

        :param X:   data (rows=images, cols=pixels)
        :param V:   PCA basis vectors
        :param m:   PCA mean vector
        :returns:       X (rows=samples, cols=features), V, m
    """
    if V is None or m is None:
        # need to perform PCA from scratch
        if num_components is None:
            num_components = 50

        # cols are pixels, rows are frames
        Xarr = np.squeeze(np.array(X).astype(np.float32))

        # perform PCA, returns mean and basis vectors
        m, V = cv2.PCACompute(Xarr)

        # use only the first num_components principal components
        V = V[:num_components]

    # backproject
    for i in range(len(X)):
        X[i] = np.dot(V, X[i] - m[0, i])

    return X, V, m
    def mark_hand_center(self, frame_in, cont):
        max_d = 0
        pt = (0, 0)
        x, y, w, h = cv2.boundingRect(cont)
        self.box = (x, y, w, h)
        for ind_y in xrange(int(y), int(y + h)):
            for ind_x in xrange(int(x), int(x + w)):
                dist = cv2.pointPolygonTest(cont, (ind_x, ind_y), True)
                if (dist > max_d):
                    max_d = dist
                    pt = (ind_x, ind_y)
        cv2.circle(frame_in, pt, int(max_d), (0, 0, 255), 2)
        sub_thresh = self.mask[y:y + h, x:x + w].copy()
        mat = np.argwhere(sub_thresh != 0)
        mat[:, [0, 1]] = mat[:, [1, 0]]
        mat = np.array(mat).astype(np.float32)  #have to convert type for PCA
        m, e = cv2.PCACompute(mat, mean=np.array([]))
        center = tuple(m[0])
        center = tuple([pt[0], pt[1]])
        rows, cols = frame_in.shape[:2]
        [vx, vy, x, y] = cv2.fitLine(cont, cv2.DIST_L2, 0, 0.01, 0.01)
        cv2.line(frame_in, (x - vx * 70, y - vy * 70), (x, y), (0, 255, 0), 2)
        endpoint1 = (x - vx * 70, y - vy * 70)
        # lefty = int((-x*vy/vx) + y)
        # righty = int(((cols-x)*vy/vx)+y)

        #cv2.line(frame_in,(cols-1,righty),(0,lefty),(0,255,0),2)
        # ec = cv2.fitEllipse(cont)
        # (x,y),(MA,ma),angle = ec
        # cv2.ellipse(frame_in,ec,(0, 0, 255),1)
        # print(angle)
        # if angle != 0:
        #     if angle > 90:
        #         k = math.tan(math.radians(90 + angle))
        #         x1 = center[0] - 1/k
        #         y1 = center[1] - 1
        #     elif angle>0 and angle < 90:
        #         #print("hg")
        #         k = math.tan(math.radians(90 -angle))
        #         x1 = center[0] - 1/k
        #         y1 = center[1] - 1
        # else:
        #     x1 = center[0]
        #     y1 = center[1] - 10
        #endpoint1 = tuple([x1, y1])
        #endpoint1 = tuple(m[0] + e[0]*10)
        #endpoint1 = tuple(m[0] + k*10)

        if endpoint1[0] < x:
            self.end = (endpoint1, (x, y))
        else:
            self.end = ((x, y), endpoint1)
        #self.end = ((int(endpoint1[0] + x), int(endpoint1[1] + y)), (int(center[0] + x), int(center[1] + y)))
        #cv2.circle(frame_in,self.end[0],5,(0,255,255),-1)
        #cv2.circle(frame_in,self.end[1],5,(0,255,255),-1)
        #print(len(cont))
        for [cnt_pts] in cont:
            self.cnt_pts.append(distant(cnt_pts, pt))
        #print(self.cnt_pts)
        return frame_in, pt, max_d
def primaryComponentAnalysis(imageFiltered, imageOriginal, agent):
    """Use primary component analysis to detect the agent's orientation."""
    #   cv2.CHAIN_APPROX_NONE       save all points
    #   cv2.CHAIN_APPROX_SIMPLE     only save key points
    img, contours, hierarchy = cv2.findContours(imageFiltered, cv2.RETR_LIST,
                                                cv2.CHAIN_APPROX_NONE)
    for i in range(0, len(contours)):
        area = cv2.contourArea(contours[i])  # calculate contour area
        # Remove small areas (noise) and big areas (the edges of the screen)
        if area < 1e2 or 1e5 < area:
            continue
        cv2.drawContours(imageOriginal, contours, i, (0, 255, 0), 2, 8,
                         hierarchy, 0)
        X = np.array(contours[i], dtype=np.float).reshape(
            (contours[i].shape[0],
             contours[i].shape[2]))  # save contour as float array
        # one-dimensioanl Primary Component Analysis
        mean, eigenvectors = cv2.PCACompute(X,
                                            mean=np.array([], dtype=np.float),
                                            maxComponents=1)
        pt = (mean[0][0], mean[0][1])
        vec = (eigenvectors[0][0], eigenvectors[0][1])  # eigen vectors
        drawAxis(imageOriginal, pt, vec, (0, 0, 255), 150)
        angle = math.atan2(-vec[1], vec[0])
    return imageOriginal
Ejemplo n.º 16
0
    def get_angle_pca(detectedObject: np.ndarray) -> float:
        # extract the PCA from segmentation object

        img_gray = cv2.cvtColor(detectedObject, cv2.IMREAD_GRAYSCALE)  # convert to grayscale
        # _, thresh = cv2.threshold(img_gray, 255, 1, cv2.THRESH_BINARY_INV)
        imag_arr = np.array(img_gray)
        mat = np.argwhere(imag_arr == 255)
        mat[:, [0, 1]] = mat[:, [1, 0]]
        mat = np.array(mat).astype(np.float32)  # have to convert type for PCA
        m, e = cv2.PCACompute(mat, mean=np.array([]))

        center = tuple(m[0])
        center = (int(center[0]), int(center[1]))

        # endpoint1 = tuple(m[0] + e[0] * 100)
        # endpoint1 = (int(endpoint1[0]), int(endpoint1[1]))

        endpoint2 = tuple(m[0] + e[1] * 50)
        endpoint2 = (int(endpoint2[0]), int(endpoint2[1]))

        x = center
        y = (center[0], center[1] + 50)
        z = endpoint2

        b = distance.euclidean(x, y)
        c = distance.euclidean(x, z)
        a = distance.euclidean(y, z)
        cos_alpha = (b * b + c * c - a * a) / (2 * b * c)
        pca_angle = float(np.degrees(np.arccos(cos_alpha)))

        return pca_angle
Ejemplo n.º 17
0
def extract_training_data():

    # cap = cv2.VideoCapture('stove pics/stove_off_vid.mp4')
    cap = cv2.VideoCapture('stove pics/training_off.mp4')

    hasFrame, frame = cap.read()

    draw_img(resize_img(frame, 50))

    # extracting features from the training videos
    X0, t0 = image_to_array('stove pics/training_off.mp4', 'off')
    # X0, t0 = image_to_array('stove pics/tobii stove off training.mp4', 'off')

    X1, t1 = image_to_array('stove pics/training_on.mp4', 'on')
    # X1, t1 = image_to_array('stove pics/tobii stove on training.mp4', 'on')

    X = np.append(X1, X0, axis=0)
    t = np.append(t1, t0, axis=0)

    # applying PCA for some reason the accuracy is really high witout PCA
    mean, eigenvectors = cv2.PCACompute(X.astype(np.float32),
                                        mean=None,
                                        maxComponents=X.shape[0])

    X = (X - mean) @ eigenvectors.T

    print(X.shape)

    np.savez('PCA parameters', mean=mean, eigenv=eigenvectors)
    np.savez("training data and labels", X=X, t=t)
Ejemplo n.º 18
0
def main():
    for v in face:
        in_matrix = None
        imgcnt = 0
        print('Read from: ' + v + ' Directory ')
        for f in os.listdir(os.path.join('training/', v)):
            imgcnt += 1
            print(f)
            # Read the image in as a gray level image.
            img = cv2.imread(os.path.join('training/', v, f),
                             cv2.IMREAD_GRAYSCALE)
            img_resized = cv2.resize(img, (w, h))

            # let's resize them to w * h
            vec = img_resized.reshape(w * h)

            # stack them up to form the matrix
            try:
                in_matrix = np.vstack((in_matrix, vec))
            except:
                in_matrix = vec

            # PCA
        if in_matrix is not None:
            mean, eigenvectors = cv2.PCACompute(
                in_matrix,
                np.mean(in_matrix, axis=0).reshape(1, -1))
        img = unFlatten(mean.transpose(), w,
                        h)  # Reconstruct mean to represent an image
        cv2.imwrite('trained/pca_face_' + v + '.png', img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
Ejemplo n.º 19
0
    def get_pca_all_acts(self, acts):
        '''Give three matrices (eigenvectors, eigenvalues, mean face for all PCA on actors) and six matrices (training set alphas, training labels, validation set and its labels, test set and its labels)'''
        act_filenames = [act_name for act_name in acts]
        train_set = zeros((TRAIN_SIZE * len(acts), IMG_DIM))
        vald_set = zeros((VALD_SIZE * len(acts), IMG_DIM))
        test_set = zeros((TEST_SIZE * len(acts), IMG_DIM))
        train_labels = zeros(TRAIN_SIZE * len(acts))
        vald_labels = zeros(VALD_SIZE * len(acts))
        test_labels = zeros(TEST_SIZE * len(acts))

        i = 0
        for act in act_filenames:
            train_set[TRAIN_SIZE*i:TRAIN_SIZE*(i+1), :], train_labels[TRAIN_SIZE*i:TRAIN_SIZE*(i+1)], \
            vald_set[VALD_SIZE*i:VALD_SIZE*(i+1), :], vald_labels[VALD_SIZE*i:VALD_SIZE*(i+1)], \
            test_set[TEST_SIZE*i:TEST_SIZE*(i+1), :], test_labels[TEST_SIZE*i:TEST_SIZE*(i+1)] = self.get_random_imgset(act, i, './'+CROPPED)
            i += 1

        #V, S, mean_X = pca(train_set)

        mean_X, V = cv2.PCACompute(train_set,
                                   np.mean(train_set, axis=0).reshape(1, -1))
        mean_X = mean_X.flatten()

        return V, mean_X, self.get_alpha_basis(
            V, mean_X, train_set
        ), train_set, train_labels, vald_set, vald_labels, test_set, test_labels
Ejemplo n.º 20
0
def load_precomputed_pca(train_images, k):

    try:

        d = np.load('mnist_pca.npz')
        mean = d['mean']
        eigenvectors = d['eigenvectors']
        print('loaded precomputed PCA from mnist_pca.npz')

    except:

        print('precomputing PCA one time only for train_images...')

        ndim = train_images.shape[1]
        mean, eigenvectors = cv2.PCACompute(
            train_images, mean=None, maxComponents=train_images.shape[1])

        print('done\n')

        np.savez_compressed('mnist_pca.npz',
                            mean=mean,
                            eigenvectors=eigenvectors)

    eigenvectors = eigenvectors[:k]

    return mean, eigenvectors
Ejemplo n.º 21
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)
Ejemplo n.º 22
0
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 = cv.PCACompute(data_pts, mean)

    # eigenvalues, eigenvectors = np.linalg.eig(eigenvectors)

    #eigenvalues = cv.eigen(eigenvectors)
    #np.linalg.eig

    # Store the center of the object
    cntr = (int(mean[0, 0]), int(mean[0, 1]))

    cv.circle(img, cntr, 3, (255, 0, 255), 2)
    p1 = (cntr[0] + 0.02 * eigenvectors[0, 0],
          cntr[1] + 0.02 * eigenvectors[0, 1])
    p2 = (cntr[0] - 0.02 * eigenvectors[1, 0],
          cntr[1] - 0.02 * eigenvectors[1, 1])
    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, cntr
Ejemplo n.º 23
0
def keypoints_from_samples(name, small, pagemask, page_outline,
                           span_points):

    all_evecs = np.array([[0.0, 0.0]])
    all_weights = 0

    for points in span_points:

        _, evec = cv2.PCACompute(points.reshape((-1, 2)),
                                 None, maxComponents=1)

        weight = np.linalg.norm(points[-1] - points[0])

        all_evecs += evec * weight
        all_weights += weight

    evec = all_evecs / all_weights

    x_dir = evec.flatten()

    if x_dir[0] < 0:
        x_dir = -x_dir

    y_dir = np.array([-x_dir[1], x_dir[0]])

    pagecoords = cv2.convexHull(page_outline)
    pagecoords = pix2norm(pagemask.shape, pagecoords.reshape((-1, 1, 2)))
    pagecoords = pagecoords.reshape((-1, 2))

    px_coords = np.dot(pagecoords, x_dir)
    py_coords = np.dot(pagecoords, y_dir)

    px0 = px_coords.min()
    px1 = px_coords.max()

    py0 = py_coords.min()
    py1 = py_coords.max()

    p00 = px0 * x_dir + py0 * y_dir
    p10 = px1 * x_dir + py0 * y_dir
    p11 = px1 * x_dir + py1 * y_dir
    p01 = px0 * x_dir + py1 * y_dir

    corners = np.vstack((p00, p10, p11, p01)).reshape((-1, 1, 2))

    ycoords = []
    xcoords = []

    for points in span_points:
        pts = points.reshape((-1, 2))
        px_coords = np.dot(pts, x_dir)
        py_coords = np.dot(pts, y_dir)
        ycoords.append(py_coords.mean() - py0)
        xcoords.append(px_coords - px0)

    if DEBUG_LEVEL >= 2:
        visualize_span_points(name, small, span_points, corners)

    return corners, np.array(ycoords), xcoords
Ejemplo n.º 24
0
def preprocess_item_sift(img, sift):

    keyPoints, descriptors = sift.detectAndCompute(img, None)

    print cv2.PCACompute(keyPoints, maxComponents=2)
    # V,S,m = pca.pca(keyPoints)

    print V
Ejemplo n.º 25
0
 def __computeEigenVectors(self):
     print("Computing PCA ", end="... ")
     mean, eigenvectors = cv2.PCACompute(self.__trainingDataMatrix,
                                         mean=None)
     print("DONE")
     self.__mean = mean
     self.__eigenVectors = eigenvectors
     self.__eigenVectors = np.transpose(self.__eigenVectors)
Ejemplo n.º 26
0
def pca(X, number_of_components):
    mean = np.mean(X, axis=0) / 255
    eigenvalues = np.array([])
    _, eigenvectors = cv2.PCACompute(X,
                                     mean=None,
                                     maxComponents=number_of_components)

    return [mean, eigenvalues, eigenvectors]
def all(input, output):

    # adaptive_threshold.py
    img = cv2.imread(input,0)
    img = cv2.medianBlur(img,5)
    img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)

    # erosion-dilation.py
    kernel = np.ones((3,3),np.uint8)
    img = cv2.dilate(img,kernel,iterations = 1)
    img = cv2.erode(img,kernel,iterations = 1)

    img = cv2.medianBlur(img,5)

    # main_axis2.py
    h, w = img.shape
    mat = np.argwhere(img != 255)
    mat[:, [0, 1]] = mat[:, [1, 0]]
    mat = np.array(mat).astype(np.float32)
    m, e = cv2.PCACompute(mat, mean = np.array([])) # อ่าน PCA
    center = tuple(m[0])
    endpoint1 = tuple(m[0] + e[0]*100)
    endpoint2 = tuple(m[0] + e[1]*50)
    delta0 = endpoint1[0]-center[0]
    delta1 = endpoint1[1]-center[1]
    angle = math.atan2(delta0, delta1)
    angle = angle*180/math.pi
    print(angle)
    inv = cv2.bitwise_not(img)
    rotated = ndimage.rotate(inv, -angle+90)
    inv = cv2.bitwise_not(rotated)
    img = inv

    # auto_crop2.py
    points = np.argwhere(img==0)
    points = np.fliplr(points)
    x, y, w, h = cv2.boundingRect(points)
    crop = img[y:y+h, x:x+w]
    img = crop

    # resize2.py
    height = 100
    width = int(img.shape[1] * height / img.shape[0])
    dim = (width, height)
    resize = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
    img = resize

    # add_white.py
    desired_size = 500
    old_size = img.shape[:2]
    old_size_int = functools.reduce(lambda sub, ele: sub * 10 + ele, old_size)
    top = bottom = left = 0
    right = desired_size - old_size[1]
    color = [255, 255, 255]
    white = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
    img = white

    cv2.imwrite(output,img)
Ejemplo n.º 28
0
 def get3dPCA(self, frames):
     #vcube = np.empty(shape=self.vCubeDimens,dtype=np.float32) x,y,t = self.vCubeDimens
     vcube = np.array(frames,dtype=np.float32)
     self.outCount = self.outCount + 1      
     sliceXY = np.swapaxes(vcube,1,2).reshape(t,y*x,order='F')#.swapaxes(0,1)    
     sliceXT = np.swapaxes(vcube,0,2).reshape(x,y*t)#.swapaxes(0,1)   
     sliceYT = np.swapaxes(vcube,0,1).reshape(y,x*t,order='F')#.swapaxes(0,1)   
     mean1, eigenXY = cv2.PCACompute(sliceXY, None)#,cv2.PCA_DATA_AS_COL) 
     # optional : write out eigenvectors#eign = copy.deepcopy(eigenXY)
     #cv2.normalize(eign, eign, 0, 255, cv2.NORM_MINMAX)
     #single = eign[0]
     #rolled = np.reshape(single,(-1,x))
     #frame = cv2.cvtColor(rolled, cv2.COLOR_GRAY2BGR)
     #cv2.imwrite('frames/' + repr(self.outCount) + 'frame.jpg', frames[0]);  
     #cv2.imwrite('frames/' + repr(self.outCount) + 'pca.jpg', frame);  
     mean2, eigenXT = cv2.PCACompute(sliceXT, None,cv2.PCA_DATA_AS_COL)   
     mean3, eigenYT = cv2.PCACompute(sliceYT, None,cv2.PCA_DATA_AS_COL)   
     return (eigenXY,eigenXT,eigenYT)    
Ejemplo n.º 29
0
def anotherPCA():
    global eigenVectors, avg
    # mean, eigenvectors2 = cv2.PCACompute(convertArrayToNPArray(setOfImages), np.mean(convertArrayToNPArray(setOfImages), axis=0).reshape(1, -1))
    avg, eigenVectors = cv2.PCACompute(
        convertArrayToNPArray(setOfImages),
        np.mean(convertArrayToNPArray(setOfImages), axis=0).reshape(1, -1))
    print("avg,", avg.shape)
    print("eigenvectors", eigenVectors.shape)
    print("eigenvectors[0]", eigenVectors[0].shape)
Ejemplo n.º 30
0
    def create_descriptors_pca(self, dim=90):
        '''
        计算描述子pca
        :param dim:
        :return:
        '''
        print("start create_descriptors_pca ...")
        query = DB.DescriptorModel.select(
            DB.DescriptorModel.id,
            DB.DescriptorModel.descriptor).tuples().iterator()
        features = numpy.array(map(lambda x: [x[0]] + list(x[1]), query))
        print("create_descriptors_pca,count=%d,dim=%d" % (len(features), dim))
        start = time()
        print("build eigenvectors start time %s" % start)

        mean, eigenvectors = cv2.PCACompute(features[:, 1:],
                                            None,
                                            maxComponents=dim)
        fitted = cv2.PCAProject(features[:, 1:], mean, eigenvectors)
        #pca = PCA(n_components=dim)
        #fitted = pca.fit_transform(features[:,1:])
        print("build eigenvectors cost time %s" % (time() - start))
        print("saving data ...")

        #scaler = preprocessing.MinMaxScaler()
        #pca = scaler.fit_transform(pca)
        DB.db.connect()
        with DB.db.transaction():
            DB.PcaModel.drop_table(fail_silently=True)
            DB.PcaModel.create_table()

            #res = DB.TrainingResult()
            #res.name = "daisy_pca"
            #res.data = pca
            #res.save()

            for i in range(0, len(fitted)):
                model = DB.PcaModel()
                model.pca = fitted[i]
                model.feature = features[i][0]
                model.save()

            DB.TrainingResult.delete().where(
                DB.TrainingResult.name == "pca_mean").execute()
            DB.TrainingResult.delete().where(
                DB.TrainingResult.name == "pca_eigenvectors").execute()
            tr = DB.TrainingResult()
            tr.name = "pca_mean"
            tr.data = mean
            tr.save()

            tr = DB.TrainingResult()
            tr.name = "pca_eigenvectors"
            tr.data = eigenvectors
            tr.save()

        print("create_descriptors_pca done")