# Lung Field Segmentation in Chest X-rays:
## 2 Materials and Methods
### Minimizing the Irrelevant Lung Area
The way we isolate these points is by evaluating the maximum distance of the farthest point to the start and end points of the convexity defect, as well as the minimum distance of the farthest point to the line that connects the start and end points:  
\[\mathbf{\Omega}=\left\{\boldsymbol{x}\,\middle|\,\boldsymbol{x}\in\boldsymbol{ X},\,\boldsymbol{s}\in\boldsymbol{S},\boldsymbol{e}\in\boldsymbol{E}\wedge \boldsymbol{max}(\boldsymbol{d}(\boldsymbol{x},\,\boldsymbol{s}),\, \boldsymbol{d}(\boldsymbol{x},\,\boldsymbol{e}))\,>\boldsymbol{T_{1}}\,\wedge \boldsymbol{d}_{l}(\boldsymbol{s},\boldsymbol{e},\boldsymbol{x})>\boldsymbol{T_ {2}}\,\right\} \tag{2}\]  
where \(\boldsymbol{X}\) denotes the set of farthest points of all convexity defects, \(\boldsymbol{S}\) and \(\boldsymbol{E}\) correspondingly the sets of starting and ending points of the convexity defects, \(\boldsymbol{d}\) the Euclidean distance among two points, \(\boldsymbol{d}_{l}(\boldsymbol{s},\boldsymbol{e},\boldsymbol{x})\) the minimum distance of the point \(\boldsymbol{x}\) to the line defined by \(\boldsymbol{s}\) and \(\boldsymbol{e}\), and \(\boldsymbol{T_{1}}\,\) and \(\boldsymbol{T_{2}}\,\) distance thresholds, selected to be sufficiently large (in order to avoid actual lung area separation) relative to the smallest dimension of the bounding box that encloses the identified region up to now. It must be noted that not all of these points are valid separation points. Following a very conservative approach, we only keep the points in narrow defects. This is done by calculating the angle formed by the lines created from the farthest point and two points on the contour sufficiently close to that point, more specifically one point preceding and one following on the contour. Equation (3) shows this filtering. \[\mathbf{\Omega}^{\prime}=\left\{\boldsymbol{x}|\boldsymbol{x}\in\mathbf{ \Omega}\wedge\,\boldsymbol{v_{a}}\,\leq\,\boldsymbol{T_{a}}\,\right\} \tag{3}\]  
where \(\boldsymbol{v_{a}}=\boldsymbol{\measuredangle}\boldsymbol{C_{i+10}}\boldsymbol{C_{i }}\boldsymbol{C_{i-10}}\), considering C the initial contour points' vector, and \(i\) is the index of the examined farthest point on the contour. \(\boldsymbol{T_{a}}\) is the preselected maximum allowed angle. By now, we are aware of initial contour defects that most probably signal the existence of two unrelated contours. The next step is to achieve this separation. Ideally, the separation should be done by a multitude of curves that represent the complex geometry of the defect, or better said the geometry of the border between the two contours. For simplicity, the separation was made by a line: the one that is connected with the separation point and crosses the defect's bounding box so that the number of background points is maximized, as can be seen in Equation (4) below. Figure 3c shows the separation line selection process. \[\boldsymbol{L}=\underset{\boldsymbol{x}\in\boldsymbol{S}}{\text{arg max}}\left(\left\|\boldsymbol{p}\boldsymbol{\in} \boldsymbol{x}\boldsymbol{I}(\boldsymbol{p})=\boldsymbol{0}\right\|\right) \tag{4}\]  
where \(\boldsymbol{S}\,\) is the set of all lines between the defect's farthest point and the points on the line between the defect's start and end, and \(\boldsymbol{p}\in\boldsymbol{x}\) denotes the points on the line \(\boldsymbol{x}\) represented as pixel coordinates in the image \(\boldsymbol{I}\). It must be noted that background pixels in a binary image have a value of 0. After the separation, it is vital to correctly select the lung field. This was done by utilizing some heuristics, related to the dimensions of the contours and the density of bone points. Figure 3d shows the final selected contour, which confirms that the usage of a straight separation line leads to a suboptimal result. The discovery of the curve that best resembles the border between the two contours would be a very advantageous improvement in the current algorithm.