在Emgu包装中使用UMat

本文关键字:UMat Emgu 包装 | 更新日期: 2023-09-27 18:00:41

我制作了一个应用程序,将HSV过滤器应用于图像,然后尝试跟踪特定的颜色。我想使用UMat,因为我需要在GPU中计算图像,而不是在CPU中。

在下面的代码中,我先取一个Image<Bgra, Byte>,然后用HSV颜色格式转换为UMat格式。然而,当我分割通道时,每个通道有3个通道(例如,hsvChannels[0]有三个通道;我不知道为什么会发生这种情况)。最后,我不能应用过滤器,因为当我使用inRangeImage方法时,我会得到一个带有一个通道的图像,并从CvInvoke.Copy"未知数组格式"中得到错误。

这个应用程序不是最好的,但我想首先了解UMat图像格式是如何工作的,然后尝试改进它

        private UMat CvAndHsvImage(Image<Bgra, Byte> imgFrame, byte lowerHue, byte upperHue, byte lowerSat, byte upperSat, byte lowerBright, byte upperBright,
        byte erosion = 0, byte dilate = 0, bool hue = false, bool sat = false, bool bright = false, bool invert = false)
    {
        // First convert the input image to hsv so we can change the channels
        //Image<Hsv, Byte> hsvImage = imgFrame.Convert<Hsv, Byte>();
        UMat hsvImage = new UMat();
        UMat bgrImage = new UMat();
        CvInvoke.CvtColor(imgFrame, bgrImage, ColorConversion.Bgra2Bgr);
        CvInvoke.CvtColor(bgrImage, hsvImage, ColorConversion.Bgr2Hsv);
        // Final image that will be returned.
        UMat ResultImage = new UMat();
        UMat ResultImageH = new UMat();
        UMat ResultImageS = new UMat();
        UMat ResultImageV = new UMat();
        UMat[] hsvChannels = new UMat[3];
        hsvChannels = hsvImage.Split();
        UMat img1 = inRangeImage(hsvChannels[0], lowerHue, upperHue);
        UMat img2 = inRangeImage(hsvChannels[1], lowerSat, upperSat);
        UMat img3 = inRangeImage(hsvChannels[2], lowerBright, upperBright);

        #region checkBox Color Mode
        // Evaluation of the check box for each filter. Return the right image
        if (hue)
        {
            CvInvoke.cvCopy(img1, ResultImageH, IntPtr.Zero);
        }
        if (sat)
            //ResultImageS = img2;
            CvInvoke.cvCopy(img2, ResultImageS, IntPtr.Zero);
        if (bright)
            //ResultImageV = img3;
            CvInvoke.cvCopy(img3, ResultImageS, IntPtr.Zero);
        if (hue && !sat && !bright)
            ResultImage = ResultImageH;
        if (!hue && sat && !bright)
            ResultImage = ResultImageS;
        if (!hue && !sat && bright)
            ResultImage = ResultImageV;
        if (hue && sat && !bright)
        {
            CvInvoke.BitwiseAnd(ResultImageH, ResultImageS, ResultImageH);
            ResultImage = ResultImageH;
        }
        if (hue && !sat && bright)
        {
            CvInvoke.BitwiseAnd(ResultImageH, ResultImageV, ResultImageH);
            ResultImage = ResultImageH;
        }
        if (!hue && sat && bright)
        {
            CvInvoke.BitwiseAnd(ResultImageS, ResultImageV, ResultImageS);
            ResultImage = ResultImageS;
        }
        if (hue && sat && bright)
        {
            CvInvoke.BitwiseAnd(ResultImageH, ResultImageS, ResultImageH);
            CvInvoke.BitwiseAnd(ResultImageH, ResultImageV, ResultImageH);
            ResultImage = ResultImageH;
        }
        #endregion
        UMat grayImage = new UMat();
        CvInvoke.CvtColor(ResultImage, bgrImage, ColorConversion.Hsv2Bgr);
        CvInvoke.CvtColor(bgrImage, grayImage, ColorConversion.Bgr2Gray);
        hsvImage.Dispose();
        bgrImage.Dispose();
        return grayImage;
    }
    /// <summary>
    /// Method to define the upper and lower values of the channels of HSV image
    /// </summary>
    /// <param name="hsvImage">Image in HSV and Byte format</param>
    /// <param name="lower">Lower value to be applied</param>
    /// <param name="upper">Upper value to be applied</param>
    /// <returns>Grayscale image with the applied range</returns>
    private UMat inRangeImage(UMat hsvImage, int lower, int upper)
    {
        UMat resultImage = new UMat();
        UMat lowerBorder = new UMat(hsvImage.Rows, hsvImage.Cols, DepthType.Cv8U, 3);
        UMat upperBorder = new UMat(hsvImage.Rows, hsvImage.Cols, DepthType.Cv8U, 3);
        lowerBorder.SetTo(new Gray(lower).MCvScalar);
        upperBorder.SetTo(new Gray(upper).MCvScalar);
        CvInvoke.InRange(hsvImage, lowerBorder, upperBorder, resultImage);
        // Dispose the image due to causing memory leaking.
        lowerBorder.Dispose();
        upperBorder.Dispose();
        return resultImage;
    }

在Emgu包装中使用UMat

对我来说,这就像是Emgu中的一个bug。CV,就像我看到的一样。

尝试使用CvInvoke。Split()改为:

private UMat CvAndHsvImage(Image<Bgra, Byte> imgFrame, byte lowerHue, byte upperHue, byte lowerSat, byte upperSat, byte lowerBright, byte upperBright,
    byte erosion = 0, byte dilate = 0, bool hue = false, bool sat = false, bool bright = false, bool invert = false)
{
    // <snip>
    // Final image that will be returned.
    UMat ResultImage = new UMat();
    UMat ResultImageH = new UMat();
    UMat ResultImageS = new UMat();
    UMat ResultImageV = new UMat();
    // << Replaced this >>
    // UMat[] hsvChannels = new UMat[3];
    // hsvChannels = hsvImage.Split();
    // << with this >>
    VectorOfUMat hsvChannels = new VectorOfUMat();
    CvInvoke.Split(hsvImage, hsvChannels);
    // </snip>
}

该方法似乎如预期的那样给出CV_8UC1 UMats。