Search code examples
c#image-processingemgucvpattern-recognition

Taking the HOG descriptor of an image using HOGDescriptor from EMGU CV C#


How can I compute the hog descriptor vector of an image using EMGU CV and C#.

If i make something like this:

float[] f;
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(fullPath);

f = hog.Compute(img1, Size.Empty, Size.Empty,null );

it doesn't work, it gives a

Object reference not set to an instance of an object.

exception. I want to compute the hog descriptor with default parameters.

Does someone know how to do this ?

Emgu cv is very poorly documented.

I have modified the code and now I am getting the following error: "External component has thrown an exception" The code is listed below

public float[] GetVector(Image<Bgr, Byte> im)
    {
        HOGDescriptor hog = new HOGDescriptor();    // with defaults values
       // Image<Bgr, Byte> pImage = new Image<Bgr, Byte>(;
       //pImage.ROI = new Rectangle(new Point(0, 0), new Size(64, 128));
        Point[] p = new Point[im.Width * im.Height];
        int k = 0;
        for (int i = 0; i < im.Width; i++)
        {
            for (int j = 0; j < im.Height; j++)
            {
                Point p1 = new Point(i, j);
                p[k++] = p1;
            }
        }
        return hog.Compute(im, new Size(8, 8), new Size(0, 0), p);
    }

Solution

  • Just for the record here is he answer:

    public Image<Bgr, Byte> Resize(Image<Bgr, Byte> im)
            {
                return im.Resize(64, 128, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
            }
            public float[] GetVector(Image<Bgr, Byte> im)
            {
                HOGDescriptor hog = new HOGDescriptor();    // with defaults values
                Image<Bgr, Byte> imageOfInterest = Resize(im);
                Point[] p = new Point[imageOfInterest.Width * imageOfInterest.Height];
                int k = 0;
                for (int i = 0; i < imageOfInterest.Width; i++)
                {
                    for (int j = 0; j < imageOfInterest.Height; j++)
                    {
                        Point p1 = new Point(i, j);
                        p[k++] = p1;
                    }
                }
    
                return hog.Compute(imageOfInterest, new Size(8, 8), new Size(0, 0), p);
            }
    

    If someone else will ever need it :)