简体   繁体   中英

How to merge two images using OpenCvSharp4 v4.2

I need to create a method in C# that will receive an image (or path to it), blur it and then try to merge the blurred image with the original using OpenCvSharp4. So far, I can create the blur image just fine, but the merging part is giving me a hard time. I've had tried a few pieces of sample code found online to no avail. Any idea how to get the merging part ?

-thanks,

  var mat = Cv2.ImRead(OriginalFileName, ImreadModes.Unchanged);
        Cv2.Resize(mat, mat, new Size(933, 934), 0d, 0d, InterpolationFlags.Linear);
        Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2RGB);
        Mat newImage = new Mat();
        Cv2.GaussianBlur(mat, newImage, new Size(67, 67), 0d, 0d, BorderTypes.Default);
        Cv2.CvtColor(newImage, newImage, ColorConversionCodes.BGR2RGB);
        Mat merged = Mat.Ones || Mat.Zeros // HELP NEEDED HERE
        Cv2.Merge(new Mat[] { mat, newImage }, merged);

Found a good hint and sample code here: How to merge two images in opencv?

Here's my final solution:

 var mat = Cv2.ImRead(OriginalFileName, ImreadModes.Unchanged);
        Cv2.Resize(mat, mat, new Size(933, 934), 0d, 0d, InterpolationFlags.Linear);
        Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2RGB);
        Mat newImage = new Mat();
        Cv2.GaussianBlur(mat, newImage, new Size(67, 67), 0d, 0d, BorderTypes.Default);
        Cv2.CvtColor(newImage, newImage, ColorConversionCodes.BGR2RGB);

        //merging
        double alpha = 0; double beta; 
        Mat src1, src2, merge;
        merge = new Mat();
        src1 = mat;
        src2 = newImage;
        beta = (1.0 - alpha);
        Cv2.AddWeighted(src1, alpha, src2, beta, 0.0, merge);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM