[Digital Image Processing] A Method for Finding the image edge

Source: Internet
Author: User

An image with a black background. There is a white object with continuous edges. It is now required that the outer edge, that is, the part of the intersection with the black background, form the edge line and be composed of a single pixel.

Thoughts:

First, find the white point at the top of the image.

Then, the next Adjacent Vertex on the edge is searched from the white vertex (current vertex. And define the current start search direction as 1 (1 ). the search method is as follows: Start from the current direction, and check the adjacent points (eight connected domains) in each direction clockwise to see if they are white points. If yes, it is the next edge point. Before the 8-direction search is completed, the next edge point can be searched, because the image edge is continuous.

Next, define the current vertex as the adjacent next edge vertex just found. And modify the current start search direction to: the direction from the previous search to the next edge point is rotated 90 degrees counter-clockwise. The reason why we need to rotate 90 degrees is that the edge may be bent. However, even if it is bent, the next edge point must be within the range after 90 degrees of rotation. We can see from this drawing that we are looking for an outer edge. Then, search for the next adjacent edge point clockwise. Until the next adjacent edge point is the initial start point, and all the edge points are found.

This method can only find the outer single-pixel edge. There is nothing to do with empty space. Edge continuity and rules are also required.

Implementation Code (MatLab ):

% Construct an image % Height = 256; % width = 256; % I = zeros (height, width); % I (100: 160,80: 160) = 255; % I (70: 100,100: 130) = 255; % I (160: 180,120: 150) = 255; I = imread('1.jpg '); I = rgb2gray (I ); I = im2bw (I); I = double (I); [height, width] = size (I); for I = 1: height for j = 1: WIDTH If I (I, j) = 1 I (I, j) = 255; end endendi = 255-I; % the program applies to images with black backgrounds and white foreground figure, imshow (I); % find the top white point for I = 1: height flag = 0; % mark whether for j = 1 is found: Width if I (I, j) = 255 flag = 1; highest_ I = I; % coordinate highest_j = J; break of the vertex at the top of the record; end end if flag = 1 break; endend % search maxnum = 99999 from the top vertex; edgecount = 0; % edgeloc = zeros (maxnum, 2); % Add the top vertex to the edge vertex array edgecount = edgecount + 1; edgeloc (edgecount, 1) = highest_ I; edgeloc (edgecount, 2) = highest_j; current_ I = highest_ I; % records the coordinates of the current vertex current_j = highest_j; next_ I =-1; % records the coordinates of the next vertex adjacent to the current vertex next_j =-1; Dir ECT = zeros (); % record search direction: 8 Direct () = 0; direct () = 1; direct () = 1; direct) = 1; direct () = 1; direct () = 0; direct () = 1; direct () =-1; direct () = 0; direct (5, 2) =-1; direct (6, 1) =-1; direct (6, 2) =-1; direct (7, 1) =-1; direct (7, 2) = 0; direct (8, 1) =-1; direct (8, 2) = 1; while next_ I ~ = Highest_ I | next_j ~ = Highest_j if current_ I = highest_ I & current_j = highest_j direct_index = 1; % record search direction: Start Point, start from 1 in the search direction end while 1 = 1% in the order of next_ I = current_ I + direct (direct_index, 1); next_j = current_j + direct (direct_index, 2 ); if I (next_ I, next_j) = 255% search for the next edge point edgecount = edgecount + 1; edgeloc (edgecount, 1) = next_ I; edgeloc (edgecount, 2) = next_j; break; end direct_index = direct_index + 1; if direct_index> 8 direct_index = direct_index-8; end % update the variable value current_ I = next_ I; current_j = next_j; direct_index = direct_index-2; % Rotate 90 degrees counterclockwise if direct_index <1 direct_index = direct_index + 8; endendimageedge = zeros (height, width); for I = 1: edgecount imageedge (edgeloc (I, 1 ), edgeloc (I, 2) = 255; endfigure, imshow (imageedge)

Running result:

You can use the built-in drawing tool of Windows 7 to draw an image (white background, black foreground, Processed in the program, converted to a black background, white foreground ):

Detected edge:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.