Esempio n. 1
0
def saveSamplesMosaic(kitti_base, pos_labels, mosaicShape, tileShape, fname):
    import random

    imgShape = (mosaicShape[0] * tileShape[0], mosaicShape[1] * tileShape[1])
    mosaic_img = np.zeros((imgShape[0],imgShape[1],3), np.float32)
    # avg_img = np.zeros((imgShape[0],imgShape[1]), np.float32)

    numTiles = mosaicShape[0]*mosaicShape[1]
    labels = pos_labels
    if len(pos_labels) >= numTiles:
        labels = random.sample(pos_labels, numTiles)
    index = 0
    for r in range(mosaicShape[0]):
        for c in range(mosaicShape[1]):
            if index >= len(labels):
                break

            label = labels[index]
            index += 1
            sample = getCroppedSampleFromLabel(kitti_base, label)

            resized = resizeSample(sample, tileShape, label)

            trs = tileShape[0]
            tcs = tileShape[1]
            tr = r * trs
            tc = c * tcs
            mosaic_img[tr:tr+trs, tc:tc+tcs] = resized

    cv2.imwrite(fname, mosaic_img)
Esempio n. 2
0
def saveAverageImage(kitti_base, pos_labels, shape, fname, avg_num=None):
    num_images = float(len(pos_labels))
    avg_num = min(avg_num, num_images)
    if avg_num is None:
        avg_num = num_images

    # avg_img = np.zeros((shape[0],shape[1],3), np.float32)
    avg_img = np.zeros(shape, np.float32)
    progressbar = ProgressBar('Averaging ' + fname, max=len(pos_labels))
    num = 0
    for label in pos_labels:
        if num >= avg_num:
            break
        num += 1
        progressbar.next()
        sample = getCroppedSampleFromLabel(kitti_base, label)
        # sample = np.float32(sample)

        resized = resizeSample(sample, shape, label)

        resized = auto_canny(resized)
        resized = np.float32(resized)

        avg_img = cv2.add(avg_img, resized / float(avg_num))
    progressbar.finish()

    cv2.imwrite(fname, avg_img)
Esempio n. 3
0
def saveSamplesMosaic(kitti_base, pos_labels, mosaicShape, tileShape, fname):
    import random

    imgShape = (mosaicShape[0] * tileShape[0], mosaicShape[1] * tileShape[1])
    mosaic_img = np.zeros((imgShape[0], imgShape[1], 3), np.float32)
    # avg_img = np.zeros((imgShape[0],imgShape[1]), np.float32)

    numTiles = mosaicShape[0] * mosaicShape[1]
    labels = pos_labels
    if len(pos_labels) >= numTiles:
        labels = random.sample(pos_labels, numTiles)
    index = 0
    for r in range(mosaicShape[0]):
        for c in range(mosaicShape[1]):
            if index >= len(labels):
                break

            label = labels[index]
            index += 1
            sample = getCroppedSampleFromLabel(kitti_base, label)

            resized = resizeSample(sample, tileShape, label)

            trs = tileShape[0]
            tcs = tileShape[1]
            tr = r * trs
            tc = c * tcs
            mosaic_img[tr:tr + trs, tc:tc + tcs] = resized

    cv2.imwrite(fname, mosaic_img)
Esempio n. 4
0
def saveAverageImage(kitti_base, pos_labels, shape, fname, avg_num=None):
    num_images = float(len(pos_labels))
    avg_num = min(avg_num, num_images)
    if avg_num is None:
        avg_num = num_images

    # avg_img = np.zeros((shape[0],shape[1],3), np.float32)
    avg_img = np.zeros(shape, np.float32)
    progressbar = ProgressBar('Averaging ' + fname, max=len(pos_labels))
    num = 0
    for label in pos_labels:
        if num >= avg_num:
            break
        num += 1
        progressbar.next()
        sample = getCroppedSampleFromLabel(kitti_base, label)
        # sample = np.float32(sample)

        resized = resizeSample(sample, shape, label)

        resized = auto_canny(resized)
        resized = np.float32(resized)

        avg_img = cv2.add(avg_img, resized / float(avg_num))
    progressbar.finish()

    cv2.imwrite(fname, avg_img)
Esempio n. 5
0
def find_label_clusters(kitti_base,
                        kittiLabels,
                        shape,
                        num_clusters,
                        descriptors=None):
    if descriptors is None:
        progressbar = ProgressBar('Computing descriptors',
                                  max=len(kittiLabels))
        descriptors = []
        for label in kittiLabels:
            progressbar.next()
            img = getCroppedSampleFromLabel(kitti_base, label)
            # img = cv2.resize(img, (shape[1], shape[0]), interpolation=cv2.INTER_AREA)
            img = resizeSample(img, shape, label)
            hist = get_hog(img)
            descriptors.append(hist)
        progressbar.finish()
    else:
        print 'find_label_clusters,', 'Using supplied descriptors.'
        print len(kittiLabels), len(descriptors)
        assert (len(kittiLabels) == len(descriptors))

    # X = np.random.randint(25,50,(25,2))
    # Y = np.random.randint(60,85,(25,2))
    # Z = np.vstack((X,Y))

    # convert to np.float32
    Z = np.float32(descriptors)

    # define criteria and apply kmeans()
    K = num_clusters
    print 'find_label_clusters,', 'kmeans:', K
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    attempts = 10
    ret, label, center = cv2.kmeans(Z, K, None, criteria, attempts,
                                    cv2.KMEANS_RANDOM_CENTERS)
    # ret,label,center=cv2.kmeans(Z,2,criteria,attempts,cv2.KMEANS_PP_CENTERS)

    print 'ret:', ret
    # print 'label:', label
    # print 'center:', center

    # # Now separate the data, Note the flatten()
    # A = Z[label.ravel()==0]
    # B = Z[label.ravel()==1]

    clusters = partition(kittiLabels, label)
    return clusters
Esempio n. 6
0
def find_label_clusters(kitti_base, kittiLabels, shape, num_clusters, descriptors=None):
    if descriptors is None:
        progressbar = ProgressBar('Computing descriptors', max=len(kittiLabels))
        descriptors = []
        for label in kittiLabels:
            progressbar.next()
            img = getCroppedSampleFromLabel(kitti_base, label)
            # img = cv2.resize(img, (shape[1], shape[0]), interpolation=cv2.INTER_AREA)
            img = resizeSample(img, shape, label)
            hist = get_hog(img)
            descriptors.append(hist)
        progressbar.finish()
    else:
        print 'find_label_clusters,', 'Using supplied descriptors.'
        print len(kittiLabels), len(descriptors)
        assert(len(kittiLabels) == len(descriptors))

    # X = np.random.randint(25,50,(25,2))
    # Y = np.random.randint(60,85,(25,2))
    # Z = np.vstack((X,Y))

    # convert to np.float32
    Z = np.float32(descriptors)

    # define criteria and apply kmeans()
    K = num_clusters
    print 'find_label_clusters,', 'kmeans:', K
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    attempts = 10
    ret,label,center=cv2.kmeans(Z,K,None,criteria,attempts,cv2.KMEANS_RANDOM_CENTERS)
    # ret,label,center=cv2.kmeans(Z,2,criteria,attempts,cv2.KMEANS_PP_CENTERS)

    print 'ret:', ret
    # print 'label:', label
    # print 'center:', center

    # # Now separate the data, Note the flatten()
    # A = Z[label.ravel()==0]
    # B = Z[label.ravel()==1]

    clusters = partition(kittiLabels, label)
    return clusters