# Image Analysis of Dynamic Brain Activity Based on Gray Distance Compensation
## 2 Automated Detection of Cellular Activity
### Detection of Active Regions  
As astrocytes respond to the release of neuromodulators such as norepinephrine through a rise of calcium [3; 6; 7], regions where the cells express GCaMP3 exhibit distinct time-varying changes in fluorescence, defined as \"active regions\". As GCaMP3 was expressed by only a subset of cells [3], it is possible to delineate the boundaries of active regions that provide valid signals. With a bench-top two-photon microscope, the boundaries of these cells could be identified and served as a standard for analysis development. With this fiber-optic imaging system, the excitation as well as emission of fluorescence from the GCaMP3-expressing astrocytes are significantly absorbed and scattered by brain tissue, resulting in images with the most pixel intensities falling within the middle level of the gray scale. Because our ability to discriminate fluorescence changes decreases when the image intensity is in the middle level [8], active regions were often not obvious and did not have distinct boundaries. To facilitate the identification of active regions, the gray level in individual frames were transformed by Equation (1), where \(f\) is the pixel gray value in the image, and _R(f)_ is the transformed resolving level. Figure 1: Block diagram for segmenting and tracing active regions. \[R(f)=\left\{ \begin{array}{ll}-\frac{f}{8}+6&0\leq f<32\\ -\frac{f}{32}+3&32\leq f<64\\ \frac{f}{128}+6&64\leq f<192\\ \frac{f}{64}-1&192\leq f<256\end{array} \right. \tag{1}\]  
After gray transformation, the higher and lower gray levels are stretched and the middle gray is compressed, so most active areas with higher gray levels, the putative active regions, are enhanced and share the same resolving level. Then, the post-transformation images were binarized. Since intensity level varies among frames, Equation (2) gives rise to a binarization threshold \(f_{H}\) that is specific to each frame, by taking into account the image maximum gray \(f_{max}\) and its transformed resolving level \(R(f_{max})\), as follows:  
\[f_{H}=f_{max}-r\times R(f_{max}) \tag{2}\]  
\(r\) is a coefficient, set to 5-10, with its value inverse to the mean intensity of each frame. In short, by plugging in the term of the \(r\times R(f_{max})\), Equation (2) factors in the brightness of individual images when determining the binarization threshold \(f_{H}\). White regions in the binarized image (Figure 2b) should denote active astrocytes; however, when compared to the two-photon standard, some regions were inappropriately categorized as active, presumably because of light scattering from nearby active cells. To eliminate this influence, a gray distance compensation is proposed [9; 10]. This concept is based on the fact that the farther the tissue is to the activity center, the weaker the influence of the scattering light is to it, as described by Equation (3):  
\[f_{c}=\left\{ \begin{array}{ll}f-c\times Dist(g)&f-Dist(g)\geq 0\\ 0&f-Dist(g)<0\end{array} \right. \tag{3}\]  
where \(f_{c}\) is the gray value after the distance compensation, \(f\) is the pixel gray value of the raw brain image, and \(Dist(g)\) is the minimum Euclidean distance to the activity center. \(c\) is a compensation coefficient correlated with the extent of light scattered from an active cell; \(c\) is at 1 when the scattering is minimal. Once active regions were determined, activity centers were defined as the highest intensity points in these regions. To perform the distance compensation, for every pixel binarized as 1, Euclidean distances to each individual activity center are first calculated. The smallest of these was then designated as \(Dist(g)\) and processed through Equation (3). As a result, the gray value of each pixel was selectively compensated with respect to its nearest activity center. Figure 2: Gray distance compensation. (**a**) The raw fluorescence image collected by the fiber optic imaging device. (**b**) Binarization of the post gray transformation image in which white regions imply the active regions.