# 代码代写｜CSCI 5561: Assignment #1 Histogram of Oriented Gradients

这是一篇来自美国的关于定向梯度直方图的**代码代写**

**1 Submission **

**– **extract_hog

**– **get_differential_filter

**– **filter_image

**– **get_gradient

**– **build_histogram

**– **get_block_descriptor

**– **face_detection

HOG ver1.py is attached with the Canvas assignment for you to download.

**– **numpy & matplotlib: https://scipy.org/install.html

**– **opencv: https://pypi.org/project/opencv-python/

def visualize_hog(im, hog, cell_size, block_size)

visualize_face_detection(I_target, bounding_boxes, bb_size)

**2 HOG**

Figure 1: Histogram of oriented gradients. HOG feature is extracted and visualized for (a) the entire image and (b) zoom-in image. The orientation and magnitude of the red lines represents the gradient components in a local cell.

In this assignment, you will implement a variant of HOG (Histogram of Oriented Gradients) in Python proposed by Dalal and Trigg [1] (2015 Longuet-Higgins Prize Winner).

It had been long standing top representation (until deep learning) for the object detection task with a deformable part model by combining with a SVM classififier [2]. Given an input image, your algorithm will compute the HOG feature and visualize as shown in Figure 1 (the line directions are perpendicular to the gradient to show edge alignment).

The orientation and magnitude of the red lines represents the gradient components in a local cell.

def extract_hog(im):

return hog

**Input: **input gray-scale image with uint8 format.

**Output: **HOG descriptor.

**Description: **You will compute the HOG descriptor of input image im. The pseudocode can be found below:

**Algorithm 1 **HOG

1: Convert the gray-scale image to float format and normalize to range [0, 1].

2: Get difffferential images using get_differential_filter and filter_image

3: Compute the gradients using get_gradient

4: Build the histogram of oriented gradients for all cells using build_histogram

5: Build the descriptor of all blocks with normalization using get_block_descriptor

6: Return a long vector (hog) by concatenating all block descriptors.

**2.1 Image fifiltering**

Figure 2: (a) Input image dimension. (b-c) Difffferential image along *x *and *y *directions.

def get_differential_filter():

return filter_x, filter_y

**Input: **none.

**Output: **filter_x and filter_y are 3*×*3 fifilters that difffferentiate along *x *and *y *directions, respectively.

**Description: **You will compute the gradient by difffferentiating the image along *x *and *y *directions. This code will output the difffferential fifilters.

def filter_image(im, filter):

return im_filtered

**Input: **im is the gray scale *m **× **n *image (Figure 2(a)) converted to flfloat format and filter is a fifilter (*k **× **k *matrix)

**Output: **im_filtered is *m **× **n *fifiltered image. You may need to pad zeros on the boundary on the input image to get the same size fifiltered image.

**Description: **Given an image and fifilter, you will compute the fifiltered image. Given the two functions above, you can generate difffferential images by visualizing the magnitude of the fifilter response as shown in Figure 2(b) and 2(c).

**2.2 Gradient Computation**

Figure 3: Visualization of (a) magnitude and (b) orientation of image gradients. (c-e) Visualization of gradients at every 3rd pixel (the magnitudes are re-scaled for illustrative purpose.).

def get_gradient(im_dx, im_dy):

return grad_mag, grad_angle

**Input: **im_dx and im_dy are the *x *and *y *difffferential images (size: *m **× **n*).

**Output: **grad_mag and grad_angle are the magnitude and orientation of the gradient images (size: *m **× **n*). Note that the range of the angle should be [0*, π*), i.e., unsigned angle (*θ *== *θ *+ *π*).

**Description: **Given the difffferential images, you will compute the magnitude and angle of the gradient. Using the gradients, you can visualize and have some sense with the image, i.e., the magnitude of the gradient is proportional to the contrast (edge) of the local patch and the orientation is perpendicular to the edge direction as shown in Figure 3.

**2.3 Orientation Binning**

Figure 4: (a) Histogram of oriented gradients can be built by (b) binning the gradients to corresponding bin.

def build_histogram(grad_mag, grad_angle, cell_size):

return ori_histo

**Input: **grad_mag and grad_angle are the magnitude and orientation of the gradient images (size: *m **× **n*); cell_size is the size of each cell, which is a positive integer.

**Output: **ori_histo is a 3D tensor with size *M **× **N **× *6 where *M *and *N *are the number of cells along *y *and *x *axes, respectively, i.e., *M *= *b **m/*cell_size*c *and *N *= *b **n/*cell_size*c *where *b·c *is the round-offff operation as shown in Figure 4(a).

**Description: **Given the magnitude and orientation of the gradients per pixel, you can build the histogram of oriented gradients for each cell.

ori_histo(*i, j, k*) = X (*u,v*)*∈C**i,j *grad_mag(*u, v*) if grad_angle(*u, v*) *∈ **θ**k *(1)

where *C**i,j *is a set of *x *and *y *coordinates within the (*i, j*) cell, and *θ**k *is the angle range of each bin, e.g., *θ*1 = [165*◦**, *180*◦*) *∪ *[0*◦**, *15*◦ *), *θ*2 = [15*◦**, *45*◦ *), *θ*3 = [45*◦**, *75*◦ *),*θ*4 = [75*◦**, *105*◦ *), *θ*5 = [105*◦**, *135*◦ *), and *θ*6 = [135*◦**, *165*◦ *). Therefore, ori_histo(i,j,:)returns the histogram of the oriented gradients at (*i, j*) cell as shown in Figure 4(b).

Using the ori_histo, you can visualize HOG per cell where the magnitude of the line proportional to the histogram as shown in Figure 1. Typical cell_size is 8.