Ask Your Question

How to detect page corners using openCV

asked 2013-04-24 05:50:59 -0500

iOSDev gravatar image

updated 2013-04-24 05:52:16 -0500

I tried below code:

    IplImage* eig_image = 0;
    IplImage* temp_image = 0;

    // Create grayscale IplImage from UIImage
    IplImage *img_color = [self CreateIplImageFromUIImage:imageView.image];
    IplImage *img1 = cvCreateImage(cvGetSize(img_color), IPL_DEPTH_8U, 1);
    cvCvtColor(img_color, img1, CV_BGR2GRAY);

    if(img1==0) {
        printf("oh no!");

    eig_image = cvCreateImage(cvGetSize(img1),IPL_DEPTH_32F, 1);
    temp_image = cvCreateImage(cvGetSize(img1),IPL_DEPTH_32F, 1);

    const int MAX_CORNERS = 100;
    CvPoint2D32f corners[MAX_CORNERS];
    int corner_count = MAX_CORNERS;
    double quality_level = 0.1;
    double min_distance = 1;
    int eig_block_size = 3;
    int use_harris = true;
    double k  = .4;

    cvGoodFeaturesToTrack(img1, eig_image, temp_image,corners,&corner_count,quality_level,min_distance,NULL,eig_block_size,use_harris,k);

    NSLog(@"corners %d",corner_count);

But got result : corners = 0

Can anyone help me with this issue.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-04-24 05:57:12 -0500

zerog80 gravatar image

updated 2013-04-24 05:58:24 -0500

I think a better and more robust way to find page corners is applying a Hough Line Transform, look for lines of sufficient length and compute intersections between each pair of them.

edit flag offensive delete link more


Hi zerog80, Thank you for your answer. Actually i am new to OpenCV. I don't have any idea how to call Hough Line Transform function. Will you give me some sample code which will help me to detect corners or lines of page.

iOSDev gravatar imageiOSDev ( 2013-04-24 06:33:02 -0500 )edit

answered 2013-04-24 07:31:52 -0500

zerog80 gravatar image

Hi, it is not so difficult, the basic steps are the following:

  1. Convert image to grayscale (cvtColor function).
  2. Apply an edge detector (Canny or Sobel, for example) to obtain a binary image (0 on the background, 1 on edges).
  3. Run the Probabilistic Hough Line Transform (better suited for finding few long lines) with the HoughLinesP function to obtain a vector of lines (you have to tweak the parameters to accomodate your scenario).
  4. Iterate through this vector to keep only lines with angles similar to 0° or 90° (supposing your page is almost perpendicular to axes).
  5. For each pair of lines with perpendicular angles, find the intersection point (here is better to solve the equivalent system of line equations for more robustness).
  6. You have a bunch of points for each of the four page corners, you must run a distance threshold filtering to merge them to obtain at most 4 of them.

Two excellent tutorials for the first three steps are part of the OpenCV documentation and you can find here and here.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-04-24 05:50:59 -0500

Seen: 2,017 times

Last updated: Apr 24 '13