Accurate classification of COVID-19 from chest X-rays is critical but remains limited by overlapping features with other lung diseases and the suboptimal performance of current methods. This study addresses the diagnostic gap by introducing a novel hybrid framework for precise segmentation and classification of lung conditions. Methods: The approach combines multi-level thresholding with the advanced metaheuristic optimization algorithms animal migration optimization (AMO), electromagnetism-like optimization (EMO), and the harmony search algorithm (HSA) to enhance image segmentation. A convolutional neural network (CNN) is then employed to classify segmented images into COVID-19, viral pneumonia, or normal categories. Results: The proposed method achieved high diagnostic performance, with 99% accuracy, 99% sensitivity, and 99.5% specificity, confirming its robustness and effectiveness in clinical image classification tasks. Conclusions: This study offers a novel and technically integrated solution for the automated diagnosis of COVID-19 and related lung conditions. The method’s high accuracy and computational efficiency demonstrate its potential for realworld deployment in medical diagnostics.