Arnold Vámos
Arnold Vámos

Reputation: 53

Matlab manipulate edges

I'm working on an image processing project. I have a grayscale image and detected edges with Canny edge detection. Now I would like to manipulate the result by filtering the unnecessary edges. I would like to keep the edges which are close to horizontal and delete edges which are close to vertical.

How can I delete the close to vertical edges?

Upvotes: 1

Views: 865

Answers (2)

shoelzer
shoelzer

Reputation: 10708

One option is to use half of a Sobel operator. The full algorithm finds horizontal and vertical edges, then combines them. You are only interested in horizontal edges, so just compute that part (which is Gy in the Wikipedia article).

You may also want to threshold the result to get a black and white image instead of shades of gray.

You could apply this technique to the original grayscale image or the result of the Canny edge detection.

Upvotes: 3

matheburg
matheburg

Reputation: 2180

It depends on how cost-intensive it is allowed to be. One easy way to do would be:

(1) Convolute your image with Sobel-Filters (gives Dx, Dy).

For each canny-edge-pixel:

(2) Normalize (Dx, Dy), s.t. in every pixel you have the direction of your edge.

(3) Compute the inner products with the direction you want to remove (in your case (0,1)).

(4) If the absolut value of the inner product is smaller than some threshold remove the pixel.

Example:

img = ...;
canny_img = ...;
removeDir = [0;1];
% convolute with sobel masks
sobelX = [1, 0, -1; 2, 0, -2; 1, 0, -1];
sobelY = sobelX';
DxImg = conv2(img,sobelX,'same');
DyImg = conv2(img,sobelY,'same');
% for each canny-edge-pixel:
for lin = 1:size(img,1) % <-> y
    for col = 1:size(img,2) % <-> x
        if canny_img(lin,col)
            % normalize direction
            normDir = [DxImg(lin,col); DyImg(lin,col)];
            normDir = normDir / norm(normDir,2);
            % inner product
            innerP = normDir' * removeDir;
            % remove edge?
            if abs(innerP) < cos(45/180*pi) % 45° threshold
                canny_img(lin,col) = 0;
            end
        end
    end
end

You can optimize it a lot due to your requirements.

Upvotes: 2

Related Questions