Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find corners on a Image using OpenCv

I´m trying to find the corners on a image, I don´t need the contours, only the 4 corners. I will change the perspective using 4 corners.

I´m using Opencv, but I need to know the steps to find the corners and what function I will use.

My images will be like this:(without red points, I will paint the points after) enter image description here

EDITED:

After suggested steps, I writed the code: (Note: I´m not using pure OpenCv, I´m using javaCV, but the logic it´s the same).

// Load two images and allocate other structures (I´m using other image)     IplImage colored = cvLoadImage(             "res/scanteste.jpg",             CV_LOAD_IMAGE_UNCHANGED); 

enter image description here

    IplImage gray = cvCreateImage(cvGetSize(colored), IPL_DEPTH_8U, 1);     IplImage smooth = cvCreateImage(cvGetSize(colored), IPL_DEPTH_8U, 1);      //Step 1 - Convert from RGB to grayscale (cvCvtColor)     cvCvtColor(colored, gray, CV_RGB2GRAY); 

enter image description here

    //2 Smooth (cvSmooth)     cvSmooth( gray, smooth, CV_BLUR, 9, 9, 2, 2);  

enter image description here

    //3 - cvThreshold  - What values?     cvThreshold(gray,gray, 155, 255, CV_THRESH_BINARY); 

enter image description here

    //4 - Detect edges (cvCanny) -What values?     int N = 7;     int aperature_size = N;     double lowThresh = 20;     double highThresh = 40;          cvCanny( gray, gray, lowThresh*N*N, highThresh*N*N, aperature_size );    

enter image description here

    //5 - Find contours (cvFindContours)     int total = 0;     CvSeq contour2 = new CvSeq(null);     CvMemStorage storage2 = cvCreateMemStorage(0);     CvMemStorage storageHull = cvCreateMemStorage(0);     total = cvFindContours(gray, storage2, contour2, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);     if(total > 1){           while (contour2 != null && !contour2.isNull()) {               if (contour2.elem_size() > 0) {                 //6 - Approximate contours with linear features (cvApproxPoly)                   CvSeq points = cvApproxPoly(contour2,Loader.sizeof(CvContour.class), storage2, CV_POLY_APPROX_DP,cvContourPerimeter(contour2)*0.005, 0);                   cvDrawContours(gray, points,CvScalar.BLUE, CvScalar.BLUE, -1, 1, CV_AA);                }               contour2 = contour2.h_next();           }      }  

enter image description here

So, I want to find the cornes, but I don´t know how to use corners function like cvCornerHarris and others.

like image 567
Ricardo Avatar asked Aug 31 '11 21:08

Ricardo


People also ask

How does harris corner Detector work?

Compared to the previous one, Harris' corner detector takes the differential of the corner score into account with reference to direction directly, instead of using shifting patches for every 45 degree angles, and has been proved to be more accurate in distinguishing between edges and corners.

What is corner detection in image processing?

Corner detection works on the principle that if you place a small window over an image, if that window is placed on a corner then if it is moved in any direction there will be a large change in intensity.


1 Answers

First, check out /samples/c/squares.c in your OpenCV distribution. This example provides a square detector, and it should be a pretty good start on how to detect corner-like features. Then, take a look at OpenCV's feature-oriented functions like cvCornerHarris() and cvGoodFeaturesToTrack().

The above methods can return many corner-like features - most will not be the "true corners" you are looking for. In my application, I had to detect squares that had been rotated or skewed (due to perspective). My detection pipeline consisted of:

  1. Convert from RGB to grayscale (cvCvtColor)
  2. Smooth (cvSmooth)
  3. Threshold (cvThreshold)
  4. Detect edges (cvCanny)
  5. Find contours (cvFindContours)
  6. Approximate contours with linear features (cvApproxPoly)
  7. Find "rectangles" which were structures that: had polygonalized contours possessing 4 points, were of sufficient area, had adjacent edges were ~90 degrees, had distance between "opposite" vertices was of sufficient size, etc.

Step 7 was necessary because a slightly noisy image can yield many structures that appear rectangular after polygonalization. In my application, I also had to deal with square-like structures that appeared within, or overlapped the desired square. I found the contour's area property and center of gravity to be helpful in discerning the proper rectangle.

like image 104
Throwback1986 Avatar answered Oct 12 '22 02:10

Throwback1986