Purpose: Image thresholding and gradient analysis have remained popular image preprocessing tools for several decades due to the simplicity and straight-forwardness of their definitions. Also, optimum selection of threshold and gradient strength values are hidden steps in many advanced medical imaging algorithms. A reliable method for threshold optimization may be a crucial step toward automation of several medical image based applications. Most automatic thresholding and gradient selection methods reported in literature primarily focus on image histograms ignoring a significant amount of information embedded in the spatial distribution of intensity values forming visible features in an image. Here, we present a new method that simultaneously optimizes both threshold and gradient values for different object interfaces in an image that is based on unification of information from both the histogram and spatial image features; also, the method works for unknown number of object regions. Methods: A new energy function is formulated by combining the object class uncertainty measure, a histogram-based feature, of each pixel with its image gradient measure, a spatial contextual feature in an image. The energy function is designed to measure the overall compliance of the theoretical premise that, in a probabilistic sense, image intensities with high class uncertainty are associated with high image gradients. Finally, it is expressed as a function of threshold and gradient parameters and optimum combinations of these parameters are sought by locating pits and valleys on the energy surface. A major strength of the algorithm lies in the fact that it does not require the number of object regions in an image to be predefined. Results: The method has been applied on several medical image datasets and it has successfully determined both threshold and gradient parameters for different object interfaces even when some of the thresholds are almost impossible to locate in the histogram. Both accuracy and reproducibility of the method have been examined on several medical image datasets including repeat scan 3D multidetector computed tomography (CT) images of cadaveric ankles specimens. Also, the new method has been qualitatively and quantitatively compared with Otsu's method along with three other algorithms based on minimum error thresholding, maximum segmented image information and minimization of homogeneity-and uncertainty-based energy and the results have demonstrated superiority of the new method. Conclusions: We have developed a new automatic threshold and gradient strength selection algorithm by combining class uncertainty and spatial image gradient features. The performance of the method has been examined in terms of accuracy and reproducibility and the results found are better as compared to several popular automatic threshold selection methods.