Segmentation based in borders
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.
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);
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)
f is the image,
T is the limit (optional),
dir is the direction of filtering (
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)
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 < T2. Pixels with values bigger than
T2are said to be "strong" axis pixels, pixels in between
T2are 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)
f is the image,
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)
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)
f is the image from which we are going to extract the lines,
rho are the vectors that we obtain from the Hough transform, and
c are the vectors that
You can practice these concepts using