stereo disparity using semi-凯发k8网页登录
this example shows how to compute disparity between left and right stereo camera images using the semi-global block matching algorithm. this algorithm is suitable for implementation on an fpga.
distance estimation is an important measurement for applications in automated driving and robotics. a cost-effective way of performing distance estimation is by using stereo camera vision. with a stereo camera, depth can be inferred from point correspondences using triangulation. depth at any given point can be computed if the disparity at that point is known. disparity measures the displacement of a point between two images. the higher the disparity, the closer the object.
this example computes disparity using the semi-global block matching (sgbm) method, similar to the (computer vision toolbox) function. the sgbm method is an intensity-based approach and generates a dense and smooth disparity map for good 3d reconstruction. however, it is highly compute-intensive and requires hardware acceleration using fpgas or gpus to obtain real-time performance.
the example model presented here is fpga-hardware compatible, and can therefore provide real-time performance.
introduction
disparity estimation algorithms fall into two broad categories: local methods and global methods. local methods evaluate one pixel at a time, considering only neighboring pixels. global methods consider information that is available in the whole image. local methods are poor at detecting sudden depth variation and occlusions, and hence global methods are preferred. semi-global matching uses information from neighboring pixels in multiple directions to calculate the disparity of a pixel. analysis in multiple directions results in a lot of computation. instead of using the whole image, the disparity of a pixel can be calculated by considering a smaller block of pixels for ease of computation. thus, the semi-global block matching (sgbm) algorithm uses block-based cost matching that is smoothed by path-wise information from multiple directions.
using the block-based approach, this algorithm estimates approximate disparity of a pixel in the left image from the same pixel in the right image. more information about stereo vision is available . before going into the algorithm and implementation details, two important parameters need to be understood: disparity levels and number of directions.
disparity levels: disparity levels is a parameter used to define the search space for matching. as shown in figure below, the algorithm searches for each pixel in the left image from among d pixels in the right image. the d values generated are d disparity levels for a pixel in left image. the first d columns of left image are unused because the corresponding pixels in right image are not available for comparison. in the figure, w represents the width of the image and h is the height of the image. for a given image resolution, increasing the disparity level reduces the minimum distance to detect depth. increasing the disparity level also increases the computation load of the algorithm. at a given disparity level, increasing the image resolution increases the minimum distance to detect depth. increasing the image resolution also increases the accuracy of depth estimation. the number of disparity levels are proportional to the input image resolution for detection of objects at the same depth. this example supports disparity levels from 8 to 128 (both values inclusive). the explanation of the algorithm refers to 64 disparity levels. the models provided in this example can accept input images of any resolution.
number of directions: in the sgbm algorithm, to optimize the cost function, the input image is considered from multiple directions. in general, accuracy of disparity result improves with increase in number of directions. this example analyzes five directions: left-to-right (a1), top-left-to-bottom-right (a2), top-to-bottom (a3), top-right-to-bottom-left (a4), and right-to-left (a5).
sgbm algorithm
the sgbm algorithm takes a pair of rectified left and right images as input. the pixel data from the raw images may not have identical vertical coordinates because of slight variations in camera positions. images need to be rectified before performing stereo matching to make all epi-polar lines parallel to the horizontal axis and match vertical coordinates of each corresponding pixel. for more details on rectification, please see (computer vision toolbox) function. the figure shows a block diagram of the sgbm algorithm, using five directions.
the sgbm algorithm implementation has three major modules: matching cost calculation, directional cost calculation and post-processing.
many methods have been explored in the literature for computing matching cost. this example implementation uses the census transform as explained in [2]. this module can be divided into two steps: center-symmetric census transform (csct) of left and right images and hamming distance computation. first, the model computes the csct on each of the left and right images using a sliding window. for a given pixel, a 9-by-7 pixel window is considered around it. csct for the center pixel in that window is estimated by comparing the value of each pixel with its corresponding center-symmetric counterpart in the window. if the pixel value is larger than its corresponding center-symmetric pixel, the result is 1, otherwise the result is 0. the figure shows an example 9-by-7 window. the center pixel number is 31. the 0th pixel is compared to the 62nd pixel (blue), the 1st pixel is compared to the 61st pixel (red), and so on, to generate 31 results. each result a single bit output and the result of the whole window is arranged as a 31-bit number. this 31-bit number is the csct output for each pixel in both images.
in the hamming distance module, the csct outputs of the left and right images are pixel-wise xor'd and set bits are counted to generate the matching cost for each disparity level. to generate d disparity levels, d pixel-wise hamming distance computation blocks are used. the matching cost for d disparity levels at a given pixel position, p, in the left image is computed by computing the hamming distance with (p to d p) pixel positions in the right image. the matching cost, c(p,d), is computed at each pixel position, p, for each disparity level, d. the matching cost is not computed for pixel positions corresponding to the first d columns of the left image.
the second module of sgbm algorithm is directional cost estimation. in general, due to noise, the matching cost result is ambiguous and some wrong matches could have lower cost than correct ones. therefore additional constraints are required to increase smoothness by penalizing changes of neighboring disparities. this constraint is realized by aggregating 1-d minimum cost paths from multiple directions. it is represented by aggregated cost from r directions at each pixel position, s(p,d), as given by
the 1-d minimum cost path for a given direction, l_r(p,d), is computed as shown in the equation.
as mentioned earlier, this example uses five directions for disparity computation. propagation in each direction is independent. the resulting disparities at each level from each direction are aggregated for each pixel. total cost is the sum of the cost calculated for each direction.
the third module of sgbm algorithm is post-processing. this module has three steps: minimum cost index calculation, interpolation, and a uniqueness function. minimum cost index calculation finds the index corresponding to the minimum cost for a given pixel. sub-pixel quadratic interpolation is applied on the index to resolve disparities at the sub-pixel level. the uniqueness function ensures reliability of the computed minimum disparity. a higher value of the uniqueness threshold marks more disparities unreliable. as a last step, the negative disparity values are invalidated and replaced with -1.
hdl implementation
the figure below shows the overview of the example model. the blocks leftimage and rightimage import a stereo image pair as input to the algorithm. in the input subsystem, the frame to pixels block converts input images from the leftimage and rightimage blocks to a pixel stream and accompanying control signals in a pixelcontrol
bus. the pixel stream is passed as input to the sgbmhdlalgorithm subsystem which contains three computation modules described above: matching cost calculation, directional cost calculation, and post-processing. the output of the sgbmhdlalgorithm subsystem is a disparity value pixel stream. in the output subsystem, the pixels to frame block converts the output to a matrix disparity map. the disparity map is displayed using the video viewer block.
modelname = 'sgbmdisparityexample'; open_system(modelname); set_param(modelname,'sampletimecolors','off'); set_param(modelname,'open','on'); set_param(modelname,'simulationcommand','update'); set(allchild(0),'visible','off');
matching cost calculation
the matching cost calculation is again separated into two parts: csct computation and hamming distance calculation. csct is calculated on each 9-by-7 pixel window by aligning each group of pixels for comparison using (simulink) blocks, (simulink) blocks and buffers. the input pixels are padded with zeros to allow csct computation for the corner pixels. the resulting stream of pixels is passed to ctlogic subsystem. figure below shows ctlogic subsystem which uses the tapped delay block to generate a group of pixels. the pixels are buffered for imgcolsize
cycles, where imgcolsize is the number of pixels in an image line. a group of pixels that is aligned for comparison is generated from each row. the for each block and logical operator block replicate the comparison logic for each pixel of the input vector size. to implement a 9-by-7 window, the model uses four such for each blocks. the result generated by each for each block is a vector which is further concatenated to form a vector of size 31-bits. after (hdl coder) is used, the output data type is uint5
. csct and zero-padding operations are performed separately on the left and right input images and the results are passed to the hamming distance subsystem.
open_system('sgbmdisparityexample/sgbmhdlalgorithm/matchingcost/censustransform/ctlogic','force');
in the hamming distance subsystem, the 65th result of the left csct is xor'd with the 65th to 2nd results of the right csct. the set bits are counted to obtain hamming distance. this distance must be calculated for each disparity level. the right csct result is passed to the enabledtappeddelay subsystem to generate a group of pixels which is then xor'd with the left csct result using for each block. the for each block also counts the set bits in the result. the for each block replicates the hamming distance calculation for each disparity level. the result is a vector, with 64 disparity levels corresponding to each pixel. this vector is the matching cost, and it is passed to the directional cost subsystem.
open_system('sgbmdisparityexample/sgbmhdlalgorithm/matchingcost/hammdista','force');
directional cost calculation
the directional cost subsystem computes disparity at each pixel in multiple directions. the five directions used in the example are left-to-right (a1), top-left-to-bottom-right (a2), top-to-bottom (a3), top-right-to-bottom-left (a4), and right-to-left (a5). as the cost aggregation at each pixel in each direction is independent of each other, all five directions are implemented concurrently.
each directional analysis is investigating the previous cost value with respect to the current cost value. the value of previous cost required to compute the current cost for each pixel depends on the direction under consideration. the figure below shows the position of the previous cost with respect to the current cost under computation, for all five directions.
in the figure above, the blue box indicates the position of the current pixel for which current cost values are computed. the red box indicates the position of the previous cost values to be used for current cost computation. for a1, the current cost becomes the previous cost value for the next computation when traversing from left to right. thus, the current cost value should be immediately fed back to compute the next current cost, as described in [3]. for a2, when traversing from left to right, current cost value should be used as the previous cost value after imgcolsize 1 cycles. current cost values are hence buffered for cycles equal to imgcolsize 1 and then fed back to compute the next current cost.
similarly, for a3 and a4, the current cost values are buffered for cycles equal to imgcolsize and imgcolsize-1, respectively. however, for a5, when traversing from left to right, the previous cost value is not available. thus, the direction of traversal to compute a5 is reversed. this adjustment is done by reversing the input pixels of each row. the current cost value then becomes the previous cost value for the next current cost computation, similar to a1.
the 1-d minimum cost path computes the current cost at d disparity position, using the matching cost value, the previous cost values at disparities d-1, d, and d 1, and the minimum of the previous cost values. the figure below shows the minimum cost path subsystem, which computes the current cost at a disparity position for a pixel.
open_system('sgbmdisparityexample/sgbmhdlalgorithm/directionalcost/lefttoright/lrsubsystem/mincostpath','force');
the for each block is used to replicate the minimum cost path calculation for each disparity level, for each direction. the figure below shows the implementation of a1 for 64 disparity levels. as shown in the figure, 64 minimum cost path calculations are generated as represented by mincostpath subsystem. the matching cost is an input from the hamming distance subsystem. the current cost computed by the mincostpath subsystem is immediately fed back to itself as the previous cost values, for the next current cost computation. thus, values for prevcost_d are now available. values for prevcost_d-1 are obtained by shifting the 1st to 63rd fed-back values to the 2nd to 64th positions. the d-1 subsystem contains a (simulink) block that shifts the position of the values, and fills in zero at the 1st position.
similarly, values for prevcost_d 1 position are obtained by shifting the 2nd to 64th feedback values to the 1st to 63rd position and inserting a zero at the 64th position. the current cost computed is also passed to the min block to compute the minimum value from the current cost values. this value is fed back to the minprevcost input of the mincostpath subsystem. the next current cost is then computed by using the current cost values, acting as previous cost values, in the next cycle for a1. since the minimum cost of disparity levels from the previous set is immediately needed for the current set, this feedback path is the critical path in the design.
open_system('sgbmdisparityexample/sgbmhdlalgorithm/directionalcost/lefttoright/lrsubsystem','force');
the current cost computations for a2, a3, and a4 are implemented in the same manner. since the current cost value is not immediately required for these directions, there is a buffer in both feedback paths. this buffer prevents this feedback path from being the critical path in the design. the figure below shows the a3 implementation with a buffer in the feedback paths.
open_system('sgbmdisparityexample/sgbmhdlalgorithm/directionalcost/toptobottom/tbsubsystem','force');
the current cost calculation for a5 has additional logic to reverse the rows at input and again reverse the rows at output to match the pixel positions for the total cost calculation. a single buffer of imgcolsize cycles achieves this reversal. since all directions are calculated concurrently, the time required to reverse the rows must be compensated for on the other paths. delay equivalent to 2*imgcolsize cycles is introduced in the other four directions. to optimize resources, instead of buffering 64 values of matching cost for each pixel, the 31-bit result of csct is buffered. a separate hamming distance module is then required to compute matching cost for a5. this design reduces on-chip memory usage. the rows are reversed after the csct computation and matching cost is calculated using a separate hamming distance module that provides the matching cost input to a5. also, dataaligner subsystem is used to remove data discontinuity for each row before passing it to hamming distance subsystems. this helps easy synchronization of data at time of aggregation. the current cost obtained from all five directions at each pixel are aggregated to obtain the total cost at each pixel. the total cost is passed to the post-processing subsystem.
post-processing
in the post-processing subsystem, the index of the minimum cost is calculated at each pixel position from 64 disparity levels by using min blocks in a tree architecture. the index value obtained is the disparity of each pixel. along with minimum cost index computation, the minimum cost value at the computed index, and the cost values at index-1 and index 1 are also computed. the minimum_cost_index subsystem implements tree architecture to compute a minimum value from 128 values. 64 disparity values are padded with 64 more values to make a vector of 128 values. minimum value is computed from this vector with 128 values. in case, a vector with 128 values is available no value is padded to a vector or in other words, vector is passed directly for minimum value calculation. (simulink) is used to select between logic using variant subsystem variables. sub-pixel quadratic interpolation is then applied to the index to resolve disparity at sub-pixel level. also, a uniqueness function is applied to the index calculated by min blocks, to ensure reliable disparity results. as a last step, invalid disparities are identified and replaced with -1.
model parameters
the model presented here takes disparity levels and uniqueness threshold as input parameters as shown in figure. disparity levels is an integer value from 8 to 128 with the default value of 64. higher value of disparity level reduces the minimum distance detected. also, for larger input image size larger disparity level helps better detection of depth of object. the uniqueness threshold must be a positive integer value, between 0 and 100 with a typical range from 5 to 15. lower value of uniqueness threshold marks more disparities reliable. the default value of uniqueness threshold is 5.
simulation and results
the example model can be simulated by specifying a path for the input images in the leftimage and rightimage blocks. the example uses sample images of size 640-by-480 pixels. the figure shows a sample input image and the calculated disparity map. the model exports these calculated disparities and a corresponding valid signal to the matlab workspace, using variable names dispmap
and dispmapvalid
respectively. the output disparity map is 576-by-480 pixels, since the first 64 columns are unused in the disparity computation. the unused pixels are padded with 0 in output subsystem to generate output image of size 640-by-480 as shown in video viewer. a disparity map with colorbar is generated using the commands shown below. higher disparity values in the result indicate that the object is nearer to the camera and lower disparity values indicate farther objects.
dispmapvalid = find(dispmapvalid == 1); disparitymap = (reshape(dispmap(dispmapvalid(1:imgrowsize*imgcolsize),:),imgcolsize,imgrowsize))'; figure(); imagesc(disparitymap); title('disparity map'); colormap jet; colorbar;
the example model is compatible to generate hdl code. you must have an hdl coder™ license to generate hdl code. the design was synthesized for the intel® arria® 10 gx (115s2f45i1sg) fpga. the table below shows resource utilization for three disparity level at different image resolutions. considering one pair of stereo input images as a frame, the algorithm throughput is estimated by finding the number of clock cycles required for processing the current frame before the arrival of next frame. the core algorithm throughput, without overhead of buffering input and output data, is the maximum operating frequency divided by the minimum cycles required between input frames. for example, for 128 disparity levels and 1280-by-720 image resolution, the minimum cycles to process the input frame is 938,857 clock cycles/frame. the maximum operating frequency obtained for algorithm with 128 disparity levels is 61.69 mhz, the core algorithm throughput is computed as 65 frames per second.
% ================================================================================== % |disparity levels || 64 || 96 || 128 | % ================================================================================== % |input image resolution || 640 x 480 || 960 x 540 || 1280 x 720 | % |alm utilization || 45,613 (11%) || 64,225 (15%) || 85,194 (20%) | % |total registers || 49,232 || 64,361 || 85,564 | % |total block memory bits || 3,137,312 (6%) || 4,599,744 (9%) || 11,527,360 (21%) | % |total ram blocks || 264 (10%) || 409 (16%) || 741 (28%) | % |total dsp blocks || 65 (4%) || 97 (6%) || 129 (8%) | % ==================================================================================
references
[1] hirschmuller h., accurate and efficient stereo processing by semi-global matching and mutual information, international conference on computer vision and pattern recognition, 2005.
[2] spangenberg r., langner t., and rojas r., weighted semi-global matching and center-symmetric census transform for robust driver assistance, computer analysis of images and patterns, 2013.
[3] gehrig s., eberli f., and meyer t., a real-time low-power stereo vision engine using semi-global matching, international conference on computer vision system, 2009.