Search code examples
c#emgucvhsv

Working with UMat in Emgu wrapper


I have made an application where I apply HSV filters to an image and then try to track a specific color. I want to use UMat because I need the computation of the image to be in the GPU and not in the CPU instead.

In the code below first I take an Image<Bgra, Byte> and then convert to UMat format with HSV color format. However, when I split the channels I get 3 channels for each channel (ex. the the hsvChannels[0] has three channels; I don't know why this is happening). Finally I can't apply the filters because when I use the inRangeImage method I get an image with one channel and error form CvInvoke.Copy "Unknown array format".

The application is not the best, but I want first to understand how UMat image format works and then try to improve it.

        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;

    }

Solution

  • It looks like a bug to me in Emgu.CV, as I see the same behavior.

    Try using CvInvoke.Split() instead like so:

    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>
    }
    

    This method seems to give CV_8UC1 UMats as expected.