The code below takes a HOG descriptor and computes the gradient magnitude per cell by looping through all blocks and adding the gradient strengths for each of the cells per block to each individual cell and finally averages the gradient strenghts.

The gradient strengths are then visualized by line lengths in the direction of the corresponding gradient bin.