Computation of ventricular volume and the diagnostic quantities like ejection-fraction ratio, heart output, mass, etc. requires detection of myocardial boundaries. The problem of segmenting an image into separate regions is one of the most significant problems in vision. Terzopoulos et al., have proposed an approach to detect the contour regions of complex shapes, assuming a user selected an initial contour not very far from the desired solution. We propose an optimal dynamic programming (DP) based method to detect contours. It is exact and not iterative. We first consider a list of uncertainty for each point selected by the user, wherein the point is allowed to move. Then, a search window is created from two consecutive lists. We then apply a dynamic programming (DP) algorithm to obtain the optimal contour passing through these lists of uncertainty, optimally utilizing the given information. For tracking, the final contour obtained at one frame is sampled and used as initial points for the next frame. Then, the same DP process is applied. We have demonstrated the algorithms on natural objects in a large spectrum of applications, including interactive segmentation of the regions of interest in medical images.