We extend the pairwise HAMMER registration algorithm to work in a groupwise manner for improving structural alignment of different individual brain images of a group. To achieve this, a tentative group mean is first generated from the previous aligned brain images (initially with affine registration), and all brain images are then registered onto the tentative group mean by HAMMER to obtain a refined group mean. Eventually, by repeating these two steps, a refined group mean image can be constructed. To obtain a better estimate of the group mean, we propose to average the aligned image according to anatomical shape, instead of intensity. Also, to alleviate possible large anatomical misalignment in the initial stages of the registration, a minimum risk estimator is employed for refining the correspondences before averaging, to prevent averaging across irrelevant anatomical structures, which, if not avoided, will render the group mean fuzzy. The performance of our groupwise registration method is evaluated by using real data (NIREP) in a ROI overlap analysis, and simulated data in an atrophy detection experiment. The results show that our groupwise registration algorithm yields better performance in both registration consistency and accuracy than the original pairwise HAMMER algorithm.