Segmentation of brain tumor images is a major research topic in medical imaging to have a refined detection and understanding of abnormal masses in the brain. This paper proposes a new segmentation method, consisting of three main steps, to detect brain lesions using magnetic resonance imaging (MRI). In the first step, the parts of the image delineating the skull bone are removed to exclude insignificant data. In the second step, which is the main contribution of this study, the particle swarm optimization (PSO) technique is applied to detect the block that contains the brain lesions. The fitness function, used to determine the best block among all candidate blocks, is based on a two-way fixed-effects analysis of variance (ANOVA). In the last step of the algorithm, the K-means segmentation method is used in the lesion block to classify it as tumor or not. A thorough evaluation of the proposed algorithm is performed using the MRI database provided by the Kouba imaging center in Algiers, Algeria. Estimates of the selected fitness function are first compared to those based on the sum-of-absolute-differences (SAD) dissimilarity criterion and demonstrate the efficiency and robustness of the ANOVA. The performance of the optimized brain tumor segmentation algorithm is then compared to the results of several state-of-the-art techniques, including fuzzy C-means, K-means, Otsu thresholding, local thresholding, and watershed segmentation. The results obtained using Dice coefficient, Jaccard distance, correlation coefficient, and root mean square error (RMSE) measurements demonstrate the superiority of the proposed optimized segmentation algorithm over equivalent techniques.