Emgu CV图像锐化和控制检测

时间:2018-09-28 11:48:23

标签: c# .net opencv emgucv

我正在一个项目中,我需要从表面上的IR激光器识别点。我用的是带红外滤镜的相机 一些输入图像:

enter image description here

enter image description here

也可以有几个点。因此,我尝试从网络摄像头中锐化此图像,然后使用Emgu CV的FindContours方法。 有我的代码:

public static Image<Gray, byte> Sharpen(Image<Gray, byte> image, int w, int h, double sigma1, double sigma2, int k)
{
    w = (w % 2 == 0) ? w - 1 : w;
    h = (h % 2 == 0) ? h - 1 : h;
    //apply gaussian smoothing using w, h and sigma 
    var gaussianSmooth = image.SmoothGaussian(w, h, sigma1, sigma2);
    //obtain the mask by subtracting the gaussian smoothed image from the original one 
    var mask = image - gaussianSmooth;
    //add a weighted value k to the obtained mask 
    mask *= k;
    //sum with the original image 
    image += mask;
    return image;
}

private void ProcessFrame(object sender, EventArgs arg)
{
    Mat frame = new Mat();
    if (_capture.Retrieve(frame, CameraDevice))
    {
        Image<Bgr, byte> original = frame.ToImage<Bgr, byte>();
        Image<Gray, byte> img = Sharpen(frame.ToImage<Gray, byte>(), 100, 100, 100, 100, 30);
        Image<Gray, byte> thresh = new Image<Gray, byte>(img.Size);
        CvInvoke.PyrDown(img, thresh);
        CvInvoke.PyrUp(thresh, thresh);
        Image<Gray, byte> mask = new Image<Gray, byte>(thresh.Size);
        Image<Gray, byte> cannyImg = thresh.Canny(10, 50);

        VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
        Mat hierarchy = new Mat();
        CvInvoke.FindContours(
            cannyImg,
            contours,
            hierarchy,
            RetrType.External,
            ChainApproxMethod.ChainApproxSimple
            );

        Image<Bgr, byte> resultImage = img.Copy().Convert<Bgr, byte>();

        int contCount = contours.Size;
        for (int i = 0; i < contCount; i++)
        {
            using (VectorOfPoint contour = contours[i])
            {
                resultImage.Draw(CvInvoke.BoundingRectangle(contour), new Bgr(255, 0, 0), 5);
            }
        }

        captureBox.Image = original.Bitmap;
        cvBox.Image = resultImage.Bitmap;
    }
}

结果图片示例:

enter image description here

因此,几乎所有时间都可以像我期望的那样工作,但是帧率非常低。我得到10-15 fps,分辨率为640x480。我需要能够以至少30 fps的速度对1920x1080执行相同的操作。这是我第一次使用OpenCV和Emgu.CV。我该怎么做才能使其表现更好?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,只是设置了阈值,因此图像仅变为黑白。通过调整阈值,即使在清晰度方面没有改善,我也能够获得相同的结果,但是由于没有进行繁重的处理,因此性能也得到了极大的改善

这是ARCore库的片段,而不是EmguCV上的

var bitmap = eventArgs.Frame;

            var filter = new Grayscale(0.2125, 0.7154, 0.0721);
            var grayImage = filter.Apply(bitmap);
            var thresholdFilter = new Threshold(CurrentThreshold);
            thresholdFilter.ApplyInPlace(grayImage);

            var blobCounter = new BlobCounter();
            blobCounter.ProcessImage(grayImage);
            var rectangles = blobCounter.GetObjectsRectangles();