Quantcast
Viewing latest article 2
Browse Latest Browse All 10

OpenCV Corner Detection with C#

Image may be NSFW.
Clik here to view.
OpenCV Logo
Last week, I wrote a few articles that showed you the basics for using OpenCV with C#. I taught you how to do video capture and basic edge detection. I taught you how to do eye tracking using OpenCV and C#. I even showed you how to record video with a standard webcam, OpenCV, and C#. Today, I’m going to show you yet another cool thing to do with OpenCV and C#. In this article, I am going to walk you thru loading images into OpenCV and detecting corners of objects in the image. Later on, I will build on top of this by teaching you how to create a lane detection system for autopilot vehicles and robots. But for now, we’re going to keep things simple and create a basic edge detector.

As I’ve shown you before, the first thing you need to do to use OpenCV is to add a reference to the libraries in your Solution Explorer. With your project expanded, right click on References and select “Add Reference…”. Then, click on the Browse tab and locate the OpenCvSharp libraries. If you don’t have them already, you can get them from my first OpenCV article here. Once you’ve included references to the OpenCvSharp runtimes, you’ll need to include them in your class with “using OpenCvSharp;“.

After you have your references setup, you’re gonna need to declare a few variables. The first variable you’re gonna need is an integer called “cornerCount”. This is the maximum number of corners you want the app to look for in your image. In the example below, I set this variable to 150 since I know that my example image only has 110 corners in it. However, if you’re using this application for detecting corners in a video feed, you might want to set this number a little lower. Otherwise, your application will run extremely slow.

The next 4 variables you’re gonna need to declare are for your image. The first of the 4 variables will be the color image that we’ll use for displaying later on and will include the corners detected. Since processing an image in full color can slow down the process, you’re gonna need to down-sample the image. This is where the second of the 4 images comes into play. All 4 images will be built using the IplImage object. But, the first 2 images will be instantiated with the fully qualified path to your test image as the first parameter and the LoadMode for the second parameter. In the example below, you can see that my first image was instantiated with LoadMode.AnyColor & LoadMode.AnyDepth modes and my second image was instantiated using the LoadMode.GrayScale mode. The third and fourth images are basically temporary images which will be used to store the features from your other 2 images.

Next, you’ll need to declare an array which we will use to store our corners. So, go ahead and make an array of CvPoint2D32f, but don’t worry about constructing it. Instead, we’re going to populate it using Cv.GoodFeaturesToTrack and passing our corners array in as an out object. You’ll also need to pass your cornerCount as a reference to the same feature along with your gray scale image and temporary images from above.

Now it’s time to have OpenCV detect the corners in your image. You’ll do this by using Cv.FindCornerSubPix. This method takes several different parameters which you’ll see in the example below. (Sorry. I’m being lazy tonight and skipping on the details. :-)). Once you have your corners detected, you’ll need to iterate over your array of corners and draw a marker to indicate where each corner is located. I used Cv.Circle to mark the corners in my example image, but you can use anything you want.

The Cv.Circle method is easy to work with. It only takes 5 parameters. I know that sounds like a lot, but it’s not bad. The first parameter that the Circle method takes is the full color image you created at the beginning of this tutorial. You can also use the gray scale image if you want, but then there would be no need for the full color image. The second parameter that the Circle method takes is a 2D point in your image. We can get those points from our corners array from earlier. The third parameter is the radius of the circle. The fourth parameter is the color of the circle. You’ll notice that I constructed a new RGB value in the example, but you can make it easier by using CvColor.Blue or whatever color you want. The last parameter that the Circle method needs is the thickness of your circles.

That’s everything. All you have left to do now is pass your image with the circles on it to a new CvWindow. To keep the window from automagically closing, you’ll also want to add the good ol’ Cv.WaitKey method which will keep your window open until you click on the window and press a key. If everything went well, your window should look something like this:

Image may be NSFW.
Clik here to view.
OpenCV Corner Detection

Here is the entire code I used for this example. Feel free to modify it any way you want and be sure to leave your questions and / or suggestions in the comments below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;

namespace EdgeDetect
{
    class Program
    {
 	 [STAThread]
        static void Main()
        {
            int cornerCount = 150;

            using (IplImage dstImg = new IplImage(@"02.JPG", LoadMode.AnyColor | LoadMode.AnyDepth))
            using (IplImage srcImgGray = new IplImage(@"02.JPG", LoadMode.GrayScale))
            using (IplImage eigImg = new IplImage(srcImgGray.GetSize(), BitDepth.F32, 1))
            using (IplImage tempImg = new IplImage(srcImgGray.GetSize(), BitDepth.F32, 1))
            {
                CvPoint2D32f[] corners;
                
                Cv.GoodFeaturesToTrack(srcImgGray, eigImg, tempImg, out corners, ref cornerCount, 0.1, 15);
                Cv.FindCornerSubPix(srcImgGray, corners, cornerCount, new CvSize(3, 3), new CvSize(-1, -1), new CvTermCriteria(20, 0.03));
                
                for (int i = 0; i < cornerCount; i++)
                    Cv.Circle(dstImg, corners[i], 3, new CvColor(0, 0, 255), 2);

                using (new CvWindow("Corners", WindowMode.AutoSize, dstImg))
                {
                    Cv.WaitKey(0);
                }
            }
        }
    }
}

Originally posted at http://www.prodigyproductionsllc.com/articles/programming/opencv-corner-detection-with-c/


Viewing latest article 2
Browse Latest Browse All 10

Trending Articles