How to detect edges and rectangles
I once wrote an application for rectangle detection. It used Sobel edge detection and line Hough transform.

Instead of looking for single peaks in Hough image (lines), the program searched 4 peaks with distance of 90 degrees between them.

For each column in Hough image (corresponding to some angle), three other columns were searched for local maxima. When satifactory peak was found in each of the four columns, the rectangle have been detected.

The program constructed the rectangle and made additional checks for color consistency within and outside the rectangle to discriminate false positives. The program was for detecting paper placement in scanned sheets of papers.