In this paper, we design linear time algorithms to recognize and determine topological invariants such as genus and homology groups in 3D. These invariants can be used to identify patterns in 3D image recognition and medical image analysis. Our method is based on cubical images with direct adjacency, also called (6,26)connectivity images in discrete geometry. According to the fact that there are only six types of local surface points in 3D and a discrete version of the wellknown Gauss-Bonnett Theorem in differential geometry, we first determine the genus of a closed 2D-connected component (a closed digital surface). Then, we use the Alexander duality to obtain the homology groups of a 3D object in 3D space. This idea can be extended to general simplicial decomposed manifolds or cell complexes in 3D.