Image Segmentation using thresholding is one of the most significant areas of image processing. However, the challenge lies in accurately and effectively segmenting medical images, which is a crucial step in many applications of medical image analysis. It necessitates the development of an effective and robust segmentation approach that can handle the complexity and diversity of medical images. To address this problem, we propose a novel image segmentation technique based on minimizing the cross-entropy function using a hybrid approach that combines the features of Opposition-Based Learning (OBL), Chameleon Swarm Algorithm (CSA), and Particle Swarm Optimization Algorithm (PSO). The opposition-based technique generates the initial population and improves convergence. Then, PSO and CSA are run in parallel on an unequal population set to improve the optimal results. The proposed approach, named the Opposition-based Chameleon Swarm Algorithm improved by Particle Swarm Algorithm (CSAPSO), is evaluated on twelve Chest X-Ray (CXR) images of patients for the detection of Pneumonia. It is further tested on a large data set related to COVID-19. We conducted extensive comparisons with other state-of-the-art methods and the Deep Learning Algorithms and used the performance indicators, namely Root Mean Square Error (RMSE), Peak Signal to Noise Ratio (PSNR) and Structure Similarity Index (SSIM), Classification Accuracy, Area Under Curve for evaluating the performance. The proposed approach is statically analyzed using the Friedman rank-sum test. Through the analysis, CSAPSO demonstrates better global optimal results compared to state-of-the-art techniques.