# Segmentation based in borders

## Point detection

To detect points in an image we can use a mask like this one:

```
-1 -1 -1
-1 8 -1
-1 -1 -1
```

Using this in MATLAB would be very easy:

```
w = [-1 -1 -1; -1 8 -1; -1 -1 -1]
g = abs(imfilter(double(f), w));
T = max(g(:)); % <-- T is a limit value.
g = g >= T; % <-- g is a logic matrix.
imshow(g);
```

The result would be, black if it didn't detect any point, and white for the pixels where it detected it.

## Line detection

We can do it through masks:

```
-1 -1 -1
2 2 2
-1 -1 -1
% Horizontal
```

```
-1 -1 2
-1 2 -1
2 -1 -1
% 45°
```

```
-1 2 -1
-1 2 -1
-1 2 -1
% Vertical
```

```
2 -1 -1
-1 2 -1
-1 -1 2
% -45°
```

For example, for -45°:

```
w = [2 -1 -1; -1 2 -1; -1 -1 2];
g = imfilter(double(f), w);
figure, imshow(g, []);
g = abs(g);
T = max(g(:));
g = g >= T;
figure, imshow(g);
```

## Axis detection

$g={\left({{G}_{x}}^{2}+{{G}_{y}}^{2}\right)}^{1/2}$

The basic idea behind axis detection is to find places in the image where the intensity changes fast, using one of these two criteria:

- Finding places where the first derivative of the intensity is bigger than a given value.
- Find places in the image where the second derivative crosses zero.

To detect axis:

`[g, t] = edge(f, 'method', parameters)`

### Axis detection with Sobel filters

`[g, t] = edge(f, 'sobel', T, dir)`

where `f`

is the image, `T`

is the limit (optional), `dir`

is the direction of filtering (`'horizontal'`

, `'vertical'`

, or `'both'`

(default)), `g`

is the image of the axis and `t`

is the limit.

### Axis detection with Prewitt filters

`[g, t] = edge(f, 'prewitt', T, dir)`

### Axis detection with Roberts filters

`[g, t] = edge(f, 'roberts', T, dir)`

### Axis detection with LoG filters

`[g, t] = edge(f, 'roberts', sigma)`

where `sigma`

is the standard deviation.

### Axis detection, the Canny detector

`[g, t] = edge(f, 'canny', sigma)`

How it works:

- We perform a smoothing of the image with a Gaussian filter.
- The gradients and their direction are calculated for each pixel. An axis point is defined as a point that has a maximum in the direction of the gradient.
- The algorithm filters the previous image with limits
`T1`

and`T2`

, where`T1 < T2`

. Pixels with values bigger than`T2`

are said to be "strong" axis pixels, pixels in between`T1`

and`T2`

are said to be "weak" axis pixels. - Finally, the algorithm performs an "edge linking" and incorporates the weak axis pixels that have a connexion of 8 with the group of the strong axis pixels.

### Axis detection, the Hough transform

`[h, theta, rho] = hough(f, dtheta, drho)`

where `f`

is the image, `dtheta`

and `drho`

specify the spacing in each axis, `H`

is the Hough transform, `theta`

is a vector that contains the angle for each sample and `rho`

is a vector that contains the ρ value for each sample.

`[r, c] = houghpeaks(h, numpeaks)`

Here, `h`

is the Hough transform, `numpeaks`

is the number of peaks we want to detect, `r`

is a vector with the position in rows of the peaks and `c`

is a vector with the position in columns of the peaks.

With the previous function we detect peaks in the transform that we want to transform into lines. We do that through the function below:

`lines = houghlines(f, theta, rho, r, c)`

where `f`

is the image from which we are going to extract the lines, `theta`

and `rho`

are the vectors that we obtain from the Hough transform, and `r`

and `c`

are the vectors that `houghpeaks`

returns.

You can practice these concepts using `snippet05`

.