shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

how to split r,g,b channel image from Mat data pointer #1653

Open futureflsl opened 4 months ago

futureflsl commented 4 months ago

I known Cv2.Split can complete this funtion,but some time,we have to use pointer convert to r,g,b channel,like this

           Mat image = Cv2.ImRead("E:\\animal.jpg");
            int out_h = image.Height;
            int out_w = image.Width;
            int channel_step = out_h * out_w;
            Mat rmat =new Mat(out_h, out_w, MatType.CV_32FC1, image.CvPtr);
            Mat gmat = new Mat(out_h, out_w, MatType.CV_32FC1, image.CvPtr+channel_step);
            Mat bmat = new Mat(out_h, out_w, MatType.CV_32FC1, image.CvPtr+2 * channel_step);

            rmat *= 255f;
            gmat *= 255f;
            bmat *= 255f;
            Cv2.ImShow("R",rmat);
            Cv2.ImShow("G", gmat);
            Cv2.ImShow("B", bmat);
            Cv2.WaitKey(0);
            Cv2.DestroyAllWindows();

but the code can not run well,how modify it to work normally?

shimat commented 4 months ago

You should first consider using Cv2.Split(), but you may find the following page helpful for other methods: https://stackoverflow.com/questions/21619609/split-channels-from-rgb-array-in-c-without-opencv