Beispiel #1
0
def single_image_features(image, feature_parameter):
    #1) Define an empty list to receive features
    img_features = []
    #2) Apply color conversion if other than 'RGB'
    feature_image = convert_color(image, feature_parameter.color_space)
    #3) Compute spatial features if flag is set
    if feature_parameter.spatial_feat == True:
        spatial_features = bin_spatial(feature_image,
                                       size=feature_parameter.spatial_size)
        #4) Append features to list
        img_features.append(spatial_features)
    #5) Compute histogram features if flag is set
    if feature_parameter.hist_feat == True:
        hist_features, rhist, ghist, bhist, bin_centers = color_hist(
            feature_image, nbins=feature_parameter.hist_bins)
        #6) Append features to list
        img_features.append(hist_features)
    #7) Compute HOG features if flag is set
    if feature_parameter.hog_feat == True:
        hog_features = get_hog_features(feature_image,
                                        feature_parameter.orient,
                                        feature_parameter.pix_per_cell,
                                        feature_parameter.cell_per_block,
                                        feature_parameter.hog_channel)
        #8) Append features to list
        img_features.append(hog_features)

    #9) Return concatenated array of features
    return np.concatenate(img_features)
Beispiel #2
0
def demo():
    # Read a color image
    img = cv2.imread("test_images/000275.png")

    # Select a small fraction of pixels to plot by subsampling it
    scale = max(img.shape[0], img.shape[1],
                64) / 64  # at most 64 rows and columns
    feature_image = convert_color(img)
    img_small = cv2.resize(
        feature_image,
        (np.int(img.shape[1] / scale), np.int(img.shape[0] / scale)),
        interpolation=cv2.INTER_NEAREST)

    # Convert subsampled image to desired color space(s)
    img_small_RGB = cv2.cvtColor(
        img_small, cv2.COLOR_BGR2RGB)  # OpenCV uses BGR, matplotlib likes RGB
    img_small_HSV = cv2.cvtColor(img_small, cv2.COLOR_BGR2HSV)
    img_small_rgb = img_small_RGB / 255.  # scaled to [0, 1], only for plotting

    # Plot and show
    plot3d(img_small_RGB, img_small_rgb)
    plt.show()

    plot3d(img_small_HSV, img_small_rgb, axis_labels=list("HSV"))
    plt.show()
Beispiel #3
0
def extract_features(imgs, cspace='RGB', spatial_size=(32, 32),
                     hist_bins=32, hist_range=(0, 256)):
    # Create a list to append feature vectors to
    features = []
    # Iterate through the list of images
    for file in imgs:
        # Read in each one by one
        image = mpimg.imread(file)
        feature_image = convert_color(image, cspace)
        # Apply bin_spatial() to get spatial color features
        spatial_features = bin_spatial(feature_image, size=spatial_size)
        # Apply color_hist() also with a color space option now
        hist_features = color_hist(feature_image, nbins=hist_bins, bins_range=hist_range)
        # Append the new feature vector to the features list
        features.append(np.concatenate((spatial_features, hist_features)))
    # Return list of feature vectors
    return features
def _selective_search_one_M(paras, color, k, mask):
    paras['k'] = k
    I = paras['im']
    I_color = color_space.convert_color(I, color)
    paras['im'] = I_color
    train = paras['train']
    is_rotate = paras['is_rotate']
    if is_rotate:
        (R, F, L, L_regions, eraseLabels, angle) = hierarchical_segmentation_M(paras, mask)
    else:
        (R, F, L, L_regions, eraseLabels) = hierarchical_segmentation_M(paras, mask)
    if train:
        return _generate_regions(R, L, L_regions, eraseLabels), F.values()[0][0]
    else:
        if is_rotate:
            return _generate_regions(R, L, L_regions, eraseLabels), F[0], angle
        else:
            return _generate_regions(R, L, L_regions, eraseLabels), F[0]
def extract_features(imgs,
                     cspace='RGB',
                     orient=9,
                     pix_per_cell=8,
                     cell_per_block=2,
                     hog_channel=0):
    # Create a list to append feature vectors to
    features = []
    # Iterate through the list of images
    for file in imgs:
        # Read in each one by one
        image = mpimg.imread(file)
        # apply color conversion if other than 'RGB'
        feature_image = convert_color(image, cspace)
        hog_features = get_hog_features(feature_image, orient, pix_per_cell,
                                        cell_per_block, hog_channel)
        # Append the new feature vector to the features list
        features.append(hog_features)
    # Return list of feature vectors
    return features
def demo():
    # Read in our vehicles and non-vehicles
    cars, notcars = load_smallset()

    # Generate a random index to look at a car image
    ind = np.random.randint(0, len(cars))
    # Read in the image
    image = mpimg.imread(cars[ind])
    image_YCrCb = convert_color(image, "YCrCb")
    channel1 = image_YCrCb[:, :, 0]
    # Define HOG parameters
    orient = 9
    pix_per_cell = 8
    cell_per_block = 2
    # Call our function with vis=True to see an image output
    features, hog_image = channel_hog_features(channel1, orient,
                                           pix_per_cell, cell_per_block,
                                           vis=True, feature_vec=False)

    side_by_side_plot(im1=image, im2=hog_image, im1_title="Example Car Image", im2_title="HOG Visualization", fontsize=16)

#demo()
def _selective_search_one(I, color, k, mask):
    I_color = color_space.convert_color(I, color)
    (R, F, L) = hierarchical_segmentation(I_color, k, mask)
    return _generate_regions(R, L)
Beispiel #8
0
def _selective_search_one(I, color, k, mask):
    I_color = color_space.convert_color(I, color)
    import pdb
    pdb.set_trace()  ## DEBUG ##
    (R, F, L) = hierarchical_segmentation(I_color, k, mask)
    return _generate_regions(R, L)
def _selective_search_one(I, color, k, mask, eraseMap):
    I_color = color_space.convert_color(I, color)
    (R, F, L, L_regions, eraseLabels) = hierarchical_segmentation(I_color, k, mask, eraseMap)
    return _generate_regions(R, L, L_regions, eraseLabels), F[0]
def _selective_search_one(I, color, k, mask):
    I_color = color_space.convert_color(I, color)
    (R, F, L) = hierarchical_segmentation(I_color, k, mask)
    return _generate_regions(R, L)
def subsample_search(img, y_start_stop, scale, clf, X_scaler,
                     feature_parameter):
    draw_img = np.copy(img)
    if y_start_stop[0] is None:
        y_start_stop[0] = 0
    if y_start_stop[1] is None:
        y_start_stop[1] = img.shape[0]
    y_start, y_stop = y_start_stop
    pix_per_cell, cell_per_block, orient, spatial_size, hist_bins = (
        feature_parameter.pix_per_cell, feature_parameter.cell_per_block,
        feature_parameter.orient, feature_parameter.spatial_size,
        feature_parameter.hist_bins)

    img_to_search = img[y_start:y_stop, :, :]
    img_to_search = convert_color(img_to_search,
                                  space=feature_parameter.color_space)
    if scale != 1:
        imshape = img_to_search.shape
        img_to_search = cv2.resize(
            img_to_search,
            (np.int(imshape[1] / scale), np.int(imshape[0] / scale)))

    ch1 = img_to_search[:, :, 0]
    ch2 = img_to_search[:, :, 1]
    ch3 = img_to_search[:, :, 2]

    # Define blocks and steps as above
    nxblocks = (ch1.shape[1] // pix_per_cell) - cell_per_block + 1
    nyblocks = (ch1.shape[0] // pix_per_cell) - cell_per_block + 1
    nfeat_per_block = orient * cell_per_block**2

    # 64 was the orginal sampling rate, with 8 cells and 8 pix per cell
    window = 64
    nblocks_per_window = (window // pix_per_cell) - cell_per_block + 1
    cells_per_step = 2  # Instead of overlap, define how many cells to step
    nxsteps = (nxblocks - nblocks_per_window) // cells_per_step
    nysteps = (nyblocks - nblocks_per_window) // cells_per_step

    # Compute individual channel HOG features for the entire image
    hog1 = channel_hog_features(ch1,
                                orient,
                                pix_per_cell,
                                cell_per_block,
                                feature_vec=False)
    hog2 = channel_hog_features(ch2,
                                orient,
                                pix_per_cell,
                                cell_per_block,
                                feature_vec=False)
    hog3 = channel_hog_features(ch3,
                                orient,
                                pix_per_cell,
                                cell_per_block,
                                feature_vec=False)

    on_windows = []
    for xb in range(nxsteps):
        for yb in range(nysteps):
            ypos = yb * cells_per_step
            xpos = xb * cells_per_step
            # Extract HOG for this patch
            hog_feat1 = hog1[ypos:ypos + nblocks_per_window,
                             xpos:xpos + nblocks_per_window].ravel()
            hog_feat2 = hog2[ypos:ypos + nblocks_per_window,
                             xpos:xpos + nblocks_per_window].ravel()
            hog_feat3 = hog3[ypos:ypos + nblocks_per_window,
                             xpos:xpos + nblocks_per_window].ravel()
            hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3))
            #hog_features = hog_feat1

            x_left = xpos * pix_per_cell
            y_top = ypos * pix_per_cell

            # Extract the image patch
            subimg = cv2.resize(
                img_to_search[y_top:y_top + window, x_left:x_left + window],
                (64, 64))

            # Get color features
            spatial_features = bin_spatial(subimg, size=spatial_size)
            hist_features, rh, gh, bh, bincen = color_hist(subimg,
                                                           nbins=hist_bins)

            # Scale features and make a prediction
            features = np.hstack(
                (spatial_features, hist_features, hog_features))
            test_features = X_scaler.transform(features.reshape(1, -1))
            #test_features = X_scaler.transform(np.hstack((shape_feat, hist_feat)).reshape(1, -1))
            test_prediction = clf.predict(test_features)

            if test_prediction == 1:
                x_left_scaled = np.int(x_left * scale)
                y_top_scaled = np.int(y_top * scale)
                window_scaled = np.int(window * scale)
                on_windows.append(((x_left_scaled, y_top_scaled + y_start),
                                   (x_left_scaled + window_scaled,
                                    y_top_scaled + window_scaled + y_start)))
                #cv2.rectangle(draw_img,(x_left_scaled, y_top_scaled+y_start),(x_left_scaled+window_scaled,y_top_scaled+window_scaled+y_start),(0,0,255),6)

    return on_windows