What is the "path around the image"? This just makes me think of a rectangle surrounding an image.
What do you mean by "loops X and Y and detects edges"?
By "edge", do you mean you have something like a black and white image where all black pixels are "edges"? You could start with one pixel and then check its neighbours, adding any black pixels to the list as you go... but which pixel do you start with? Does your image only contain one-pixel-thick lines? Can they cross or split? We need this information.
Can you provide images/diagrams/examples?