-2

Can the mat or umat have null values in the middle? I need it to behave like .png format. If not, how can I achieve this purpose? Mat and Umat act like a rectangle image to me.

I have tried rectangle copy method, grabcut method, but none of the method works right.

I started the code as below:

Bitmap bmpUImage = new Bitmap(plotBox.Image);
Image<Bgr, Byte> uimage = new Image<Bgr, byte>(bmpUImage);
UMat input = uimage.ToUMat();

A mat or umat act like a .png, having some cutout/null values in the middle part of the image.

  • So, you want transparency for certrain image parts!? All you need is to add an alpha channel to your image and modify the desired part accordingly. Please, have a look at [this SO question, and the answers](https://stackoverflow.com/questions/40527769). – HansHirse Apr 03 '19 at 04:31
  • Thanks HansHirse to provide me an idea on the alpha channel! Since I'm using emgu, I found a better link for my propose here that might benefits others: https://stackoverflow.com/questions/45660427/emgu-c-sharp-opencv-make-color-black-transparent – Jemt tinhwa Apr 03 '19 at 10:02

1 Answers1

0
///////////////// add black as transparency /////////////////
        Bitmap bmpUImage = new Bitmap(plotBox_Method.Image);

        Mat output = new Mat();
        Mat Shading_Image = new Image<Bgr, byte>(bmpUImage).Mat;

        // puting a black rectangle for transparent area later
        Shading_Image.CopyTo(output);
        CvInvoke.Rectangle(output, mask_rect, new MCvScalar(0,0,0), -1);

        Mat imageMat = output;
        Mat finalMat = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 4);
        Mat tmp = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
        Mat alpha = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);

        CvInvoke.CvtColor(imageMat, tmp, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
        CvInvoke.Threshold(tmp, alpha, 0, 255, Emgu.CV.CvEnum.ThresholdType.Binary);

        CvInvoke.Imshow("alpha", alpha);

        VectorOfMat rgb = new VectorOfMat(3);

        CvInvoke.Split(imageMat, rgb);

        Mat[] rgba = { rgb[0], rgb[1], rgb[2], alpha };

        VectorOfMat vector = new VectorOfMat(rgba);

        CvInvoke.Merge(vector, finalMat);

        imgPlot.Image = finalMat.ToImage<Bgra, Byte>().ToBitmap(); 
        ///////////////// end of add black as transparency /////////////////