# Efficient Edge Detection Method for Focused Images
## 4 Edge Detection by the Local k-Means  
In this section, we propose an efficient method for edge detection in focused images, which is based on the proposed use of the k-Means algorithm locally. Though the idea of local use of the k-Means algorithm through the sliding window was already proposed [37], it was performed quite differently and was dedicated to specific tasks like text document analysis. As we could see in the previous section, the application of the k-Means algorithm to the whole image does not lead to satisfactory results of edge detection. Therefore, we propose to apply k-Means locally via a filtering window, which goes through the image, like a typical filter, and compute k-Means in this local window. However, unlike in the classical filtering process, we compute here a subimage within the filter instead of a single pixel. It means that, unlike in the state-of-the-art methods, we obtain as a result a subimage instead of a pixel. In more detail, to compute the convolution of an image with the typical filter of size \(5\times 5\) pixels, see Figure 4a, we apply the filtered pixel by pixel to the image (in the horizontal and then the vertical direction) and compute the new pixel's value each time taking into account the values within this \(5\times 5\) pixels window. However, in our case, we apply the filter to the given square area, see Figure 4b. In other words, we divide the given image into subimages of size e.g., \(4\times 4\) pixels and for each subimage we apply the area mask (of size, e.g., \(8\times 8\) pixels) and compute the k-Means algorithm, within this \(8\times 8\) pixels window. Then we draw the result within just the \(4\times 4\) pixels area, which is the considered subimage. Similarly, as in classical filtering, in the proposed method, we deal with edge pixels that go beyond the image during filtering. We solve this problem by reducing the mask's size to the area size in border squares. The proposed method is summarized in Algorithm 1. We fix the initial values of the segments and means on lines 1-2. In lines 4-12, the classical k-Means algorithm is presented. We iterate the segmentation according to the means (lines 6-7). In each step, the means are updated (lines 8-9). The algorithm stops when the assumed error measured as the Mean Square Error (MSE) between the original image and the segmented one (line 11) is small enough. Next, the local application of this algorithm is defined in lines 13-17. The image is divided into adjacent subsquares of size _areaSize_ (lines 13-14). Then, for each such subsquare, the accompanying mask is defined of size _maskSize_ (line 15). Next, the k-Means algorithm is computed for such an image within the mask (line 16). Finally, just the small subimage of size _areaSize_ is drawn as segmented (line 17). Note that, depending on an image, we can adjust the sizes of the filtering window and the considered area. Note that when we fix these sizes as the same, we deal with a simple application of the k-Means algorithm in squared subimages of the considered image. Figure 3: Edge detection by the k-Means algorithm. The upper row: k-Means segmentation for (**a**) \(k=3\), (**b**) \(k=8\) segments. The lower row: the edges of k-Means segmentation for (**c**) \(k=3\), (**d**) \(k=8\) segments. ```
Input:\(img\) - image; \(k\) - number of segments; \(areaSize\) - size of the considered area; \(maskSize\) - size of the filtering mask. Output: An image with detected edges.