Skip to content

FastNlMeansDenoisingMulti does not work #1402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
warren-byoun opened this issue Mar 21, 2022 · 3 comments · Fixed by #1404 · May be fixed by #1756
Closed

FastNlMeansDenoisingMulti does not work #1402

warren-byoun opened this issue Mar 21, 2022 · 3 comments · Fixed by #1404 · May be fixed by #1756

Comments

@warren-byoun
Copy link

Summary of your issue

Environment

Visual Studio 2017,
OpenCvSharp4 v4.5.5

What did you do when you faced the problem?

I uses
Cv2.FastNlMeanDenoising and Cv2.FastNIMeanDenoisingMulti

Cv2.FastNIMeanDenoising works fine.
But Cv2.FastNIMeanDenoisingMulti does not.

exception occurs :
"Unsupported depth! Only CV_8U is supported for NORM_L2"

MatTypes of input sources are MatType.CV_8UC1 all.

Example code:

public override int Run(ClsImageCollection<Mat> inputImages)
        {
            try
            {
                Mat outMat = new Mat(this.OutputImageHeight, this.OutputImageWidth, MatType.CV_8UC1);
                if (inputImages.Count == 1)
                    Cv2.FastNlMeansDenoising(inputImages[0], outMat, this.h, this.templateWindowSize, this.searchWindowSize);
                else
                {
                    List<Mat> inputs = inputImages.Values.ToList();
                    inputs.ForEach(mat => Trace.WriteLine($"{mat.Depth()}"));

                    Cv2.FastNlMeansDenoisingMulti(inputs, outMat, this.InputCount / 2, this.InputCount, this.h, this.templateWindowSize, this.searchWindowSize);
                }
                _outputImages.Add(0, outMat);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);
                return ClsFilter.NG;
            }
            return ClsFilter.OK;
            //return base.Run(inputImages);
        }


Output:

예외 발생: 'OpenCvSharp.OpenCVException'(OpenCvSharp.dll)
Unsupported depth! Only CV_8U is supported for NORM_L2
   위치: OpenCvSharp.Internal.NativeMethods.<>c.<.cctor>b__1674_0(ErrorCode status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userData)
   위치: OpenCvSharp.Internal.NativeMethods.photo_fastNlMeansDenoisingMulti(IntPtr[] srcImgs, Int32 srcImgsLength, IntPtr dst, Int32 imgToDenoiseIndex, Int32 temporalWindowSize, Single h, Int32 templateWindowSize, Int32 searchWindowSize)
   위치: OpenCvSharp.Cv2.FastNlMeansDenoisingMulti(IEnumerable`1 srcImgs, OutputArray dst, Int32 imgToDenoiseIndex, Int32 temporalWindowSize, Single h, Int32 templateWindowSize, Int32 searchWindowSize)
   위치: OpenCvSharp.Cv2.FastNlMeansDenoisingMulti(IEnumerable`1 srcImgs, OutputArray dst, Int32 imgToDenoiseIndex, Int32 temporalWindowSize, Single h, Int32 templateWindowSize, Int32 searchWindowSize)
   위치: ACE.Imaging.Filter.ClsFilterNLM.Run(ClsImageCollection`1 inputImages) 파일 D:\Work-PlatformLAB\Project\02.ACE\03.Source\ACEProject\ACE\ACE.Imaging\Filter\ClsFilterNLM.cs:줄 85

where 예외 발생 = exception, 위치 = location, 줄 = line

What did you intend to be?

@warren-byoun
Copy link
Author

warren-byoun commented Mar 23, 2022

I found it by myself.

Cv2_photo.cs:

    public static void FastNlMeansDenoisingMulti(

        IEnumerable<Mat> srcImgs, OutputArray dst,
        int imgToDenoiseIndex, int temporalWindowSize,
        float h = 3, int templateWindowSize = 7, int searchWindowSize = 21)
    {
        if (srcImgs == null)
            throw new ArgumentNullException(nameof(srcImgs));
        if (dst == null)
            throw new ArgumentNullException(nameof(dst));
        
      // changed
        dst.ThrowIfNotReady();
        var srcImgPtrs = srcImgs.Select(x => x.CvPtr).ToArray();

        NativeMethods.HandleException(
            NativeMethods.photo_fastNlMeansDenoisingMulti(
                srcImgPtrs, srcImgPtrs.Length, dst.CvPtr,
                imgToDenoiseIndex,
                temporalWindowSize, h, templateWindowSize, searchWindowSize));

        dst.Fix();
        GC.KeepAlive(srcImgs);
        /*
      //previous source
        var srcImgsAsArrays = srcImgs.Select(m => new InputArray(m)).ToArray();
        try
        {
            FastNlMeansDenoisingMulti(srcImgsAsArrays, dst, imgToDenoiseIndex, temporalWindowSize,
                h, templateWindowSize, searchWindowSize);
        }
        finally
        {
            foreach (var img in srcImgsAsArrays)
            {
                img.Dispose();
            }
        }
        */
    }

photo.h in OpenCvSharpExtern

 CVAPI(ExceptionStatus) photo_fastNlMeansDenoisingMulti(cv::Mat ** srcImgs, int srcImgsLength, 
    cv::_OutputArray *dst, int imgToDenoiseIndex, int temporalWindowSize,
   float h, int templateWindowSize, int searchWindowSize)
{
    BEGIN_WRAP

    std::vector<cv::Mat> srcImgsVec;
    for (int i = 0; i < srcImgsLength; i++)
        srcImgsVec.push_back(*srcImgs[i]);

    cv::fastNlMeansDenoisingMulti(srcImgsVec, *dst, imgToDenoiseIndex, temporalWindowSize, h, templateWindowSize, 
          searchWindowSize);

    END_WRAP
}

shimat added a commit that referenced this issue Mar 27, 2022
@shimat
Copy link
Owner

shimat commented Mar 27, 2022

Thank you for your information!

@smx-smx
Copy link

smx-smx commented Apr 15, 2025

Are you sure this is fixed?
I get an AccessViolationException using the same exact Test case code (which is commented out, so you wouldn't be able to spot this failure)

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants