The balanced iterative reducing and clustering using hierarchies (BIRCH) method was adopted to optimize the results of the resting-state functional magnetic resonance imaging (RS-fMRI) to analyze the changes in the brain function of patients with chronic pain accompanied by poor emotion or abnormal sleep quality in this study, so as to provide data support for the prevention and treatment of clinical chronic pain with poor emotion or sleep quality. 159 patients with chronic pain who visited the hospital were selected as the research objects, and they were grouped according to the presence or absence of abnormalities in emotion and sleep. The patients without poor emotion and sleep quality were set as the control group (60 cases), and the patients with the above symptoms were defined in the observation group (90 cases). The brain function was detected by RS-fMRI technology based on the BIRCH algorithm. The results showed that the rand index (RI), adjustment of RI (ARI), and Fowlkes–Mallows index (FMI) results in the k-means, flow cytometry (FCM), and BIRCH algorithms were 0.82, 0.71, and 0.88, respectively. The scores of Hamilton Depression Scale (HAHD), Hamilton Anxiety Scale (HAMA), and Pittsburgh Sleep Quality Index (PSQI) were 7.26 ± 3.95, 7.94 ± 3.15, and 8.03 ± 4.67 in the observation group and 4.03 ± 1.95, 5.13 ± 2.35, and 4.43 ± 2.07 in the control group; the higher proportion of RS-fMRI was with abnormal brain signal connections. A score of 7 or more meant that the number of brain abnormalities was more than 90% and that of less than 7 was less than 40%, showing a statistically obvious difference in contrast
P
<
0.05
. Therefore, the BIRCH clustering algorithm showed reliable value in the optimization of RS-fMRI images, and RS-fMRI showed high application value in evaluating the emotion and sleep quality of patients with chronic pain.