# Autonomous Prostate Segmentation in 2D B-Mode Ultrasound Images
## Appendix A Ultrasound Image Preprocessing  
A point within the patient attached frame, \({}^{P}\vec{p}=[^{P}x,^{P}y,^{P}z,1]^{\top}\), can be transformed by a matrix, \(\frac{U}{P}T\), to a point in the the ultrasound frame, \({}^{U}\vec{p}\), given by \[ \begin{split}\begin{bmatrix}\mathit{U}px\\ \mathit{u}py\\ \mathit{u}z_{i}\\ \mathit{1}\end{bmatrix}=\begin{bmatrix}s_{px}&0&0&t_{px}\\ 0&s_{py}&0&t_{py}\\ 0&0&1&0\\ 0&0&0&1\end{bmatrix}\begin{bmatrix}\mathit{P}x\\ \mathit{P}y\\ \mathit{P}z_{i}\\ \mathit{1}\end{bmatrix},\end{split} \end{split} \tag{1}\]  
with the pixel-to-millimetre scaling of the US image (for the ultrasound images used in this work, the pixel-to-millimetre scaling values are \(s_{px}=5.5769\frac{px}{mn}\) and \(s_{py}=-5.5769\frac{px}{mn}\)). The origin of \(\{P\}\) was defined to be the origin of the ROI coordinate frame, \((\,{}^{U}px_{c},\,{}^{U}py_{c})\), giving a translation offset from the origin of \(\{U\}\) to \(\{P\}\) of \(t_{px}=33.4615\) and \(t_{py}=441.1758\) given by \(s_{px}\) and \(s_{px}\) and a translational offset between the origin of \(\{U\}\) with respect to the origin of \(\{P\}\), expressed as \(t_{px}\) and \(t_{py}\). As the frame \(\{U\}\) is used to describe the coordinates of a pixel within an image, the position vector \(\,{}^{U}\vec{p}\) of the pixel will instead be denoted by \((\,{}^{U}px,\,{}^{U}py)\). The \(\,{}^{U}\!z_{i}\) coordinate in this notation is omitted for brevity as the image warp, and subsequent superpixel image processing, work on a single 2D image slice at a time. Since the \(\,{}^{U}px\) and \(\,{}^{U}\!py\) coordinates describe image pixel locations, for an image \(\,{}^{U}\!\mathcal{I}_{i}\), the values of \(\,{}^{U}\!px\) and \(\,{}^{U}\!py\) will be restricted to being positive whole numbers and limited by the height, \(\,{}^{U}\!\mathcal{I}_{height}\), and width, \(\,{}^{U}\!\mathcal{I}_{width}\), of the image, such that \(\{\,{}^{U}\!px\in\mathbb{N}:1\leq\,{}^{U}\!px\leq\,{}^{U}\!\mathcal{I}_{width}\}\) and \(\{\,{}^{U}\!py\in\mathbb{N}:1\leq\,{}^{R}\!py\leq\,{}^{U}\!\mathcal{I}_{height}\}\). A function \((\,{}^{U}\!px,\,{}^{U}\!py)=\text{Trunc}(\,{}^{U}\!px,\,{}^{U}\!py)\), \(\,{}^{U}\!\mathcal{I}_{height}\), \(\,{}^{U}\!\mathcal{I}_{width})\) will first limit and then round any input position value such that it is returned as a valid \((\,{}^{U}\!px,\,{}^{U}\!py)\) pixel position. The grayscale intensity (brightness) of an individual pixel in the TRUS image at the location \((\,{}^{U}\!px,\,{}^{U}\!py)\) is denoted as \(\,{}^{U}\!\mathcal{I}_{i}(\,{}^{U}\!px,\,{}^{U}\!py)\). For computational efficiency, we will use an image warp as a preprocessing step on all of the ultrasound images. This preprocessing step is performed to limit the prostate segmentation algorithm to operate within a region of interest (ROI) in the TRUS image. The ROI includes only the actual US image (and excludes any background pixels outside of the ultrasound image field of view). The superpixel algorithm, described in Section 4, will then operate only on pixels within this ROI. Due to the small diameter of the TRUS probe, the ultrasound transducer elements have significant curvature, and the desired ROI consists of a large arc of pixels in the TRUS image; see Figure 10a. To isolate the ROI, the input B-mode TRUS image is warped through the use of a modified 2D Cartesian coordinate to polar coordinate transform. The resulting image, \(\,{}^{R}\!\mathcal{I}_{d}\), will be referred to as the ROI image. The pixels of the ROI image are referred to by \(\,{}^{R}\!\mathcal{I}_{d}(\,{}^{R}\!px,\,{}^{R}\!py)\), where a new coordinate frame \(\{R\}\) is defined for the pixels with the ROI image. The transformation between a pixel in the TRUS image \(\{U\}\) frame and the ROI image frame \(\{R\}\) is given by  
\[ \begin{split}\,{}^{R}\!px&=s_{rx}\Big{(}\text{atan2}( \,{}^{U}\!py-\,{}^{U}\!py_{c},\,{}^{U}\!px-\,{}^{U}\!px_{c})+t_{rx}\Big{)}\\ \,{}^{R}\!py&=s_{ry}\Big{(}\sqrt{(\,{}^{U}\!py-\,{}^{U} \!py_{c})^{2}+(\,{}^{U}\!px-\,{}^{U}\!px_{c})^{2}}+t_{ry}\Big{)},\end{split}