In this thesis we deal with the calculation and minimization of safety margins (CTV to PTV) that are required to account for setup errors and internal organ motion in radiotherapy. First of all, an algorithm was developed to automatically expand a three‐dimensional (3‐D) tumor volume, defined in a set of 2‐D CT images, with 3‐D margins of a given size. To do this manually and in three dimensions is impossible. The clinical advantages of the automatically calculated 3‐D margins over the much applied manual 2‐D expansion procedure were assessed. In addition, a model was devised to calculate the margins using measured geometrical deviations from the planned situation in representative patient populations. The calculated margins were such that the expected radiation dose in the tumor was adequate and homogeneous. It appeared that systematic deviations, which are the same each fraction of the treatment, are about three times more important than random deviations, which vary from fraction to fraction. The model was used to investigate whether smaller margins could be used when patients with prostate cancer are treated in the prone instead of the supine position, but there was no significant difference. Finally, the use of portal images for an on‐line correction of geometrical deviations was studied. The registration of bony structures in the images enabled on‐line corrections of the patient setup within a couple of minutes and a significant margin reduction. For prostate cancer treatments, simulations using multiple CT data of 15 patients indicated that gas pockets in the rectum, which are also visible in portal images, may be used for the on‐line determination of rectal wall and tumor motion.