public void Niblack(LocalBinarizationMethods method) { using var src = Image("mandrill.png", ImreadModes.Grayscale); using var dst = new Mat(); CvXImgProc.NiblackThreshold(src, dst, 255, ThresholdTypes.Binary, 5, 0.5, method); ShowImagesWhenDebugMode(dst); }
/// <summary> /// Applies Niblack thresholding to input image. /// </summary> /// <remarks><![CDATA[ /// The function transforms a grayscale image to a binary image according to the formulae: /// - **THRESH_BINARY** /// \f[dst(x, y) = \fork{\texttt{maxValue } /// }{if \(src(x, y) > T(x, y)\)}{0}{otherwise}\f] /// - ** THRESH_BINARY_INV** /// \f[dst(x, y) = \fork{0}{if \(src(x, y) > T(x, y)\)}{\texttt{maxValue}}{otherwise}\f] /// where \f$T(x, y)\f$ is a threshold calculated individually for each pixel. /// The threshold value \f$T(x, y)\f$ is the mean minus \f$ delta \f$ times standard deviation /// of \f$\texttt{blockSize} \times\texttt{blockSize}\f$ neighborhood of \f$(x, y)\f$. /// The function can't process the image in-place. /// ]]></remarks> /// <param name="src">Source 8-bit single-channel image.</param> /// <param name="dst">Destination image of the same size and the same type as src.</param> /// <param name="maxValue">Non-zero value assigned to the pixels for which the condition is satisfied, /// used with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.</param> /// <param name="type">Thresholding type, see cv::ThresholdTypes.</param> /// <param name="blockSize">Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.</param> /// <param name="k">The user-adjustable parameter used by Niblack and inspired techniques.For Niblack, /// this is normally a value between 0 and 1 that is multiplied with the standard deviation and subtracted from the mean.</param> /// <param name="binarizationMethod">Binarization method to use. By default, Niblack's technique is used. /// Other techniques can be specified, see cv::ximgproc::LocalBinarizationMethods.</param> public static void NiblackThreshold( InputArray src, OutputArray dst, double maxValue, ThresholdTypes type, int blockSize, double k, LocalBinarizationMethods binarizationMethod = LocalBinarizationMethods.Niblack) { if (src == null) { throw new ArgumentNullException(nameof(src)); } if (dst == null) { throw new ArgumentNullException(nameof(dst)); } src.ThrowIfDisposed(); dst.ThrowIfNotReady(); NativeMethods.ximgproc_niBlackThreshold(src.CvPtr, dst.CvPtr, maxValue, (int)type, blockSize, k, (int)binarizationMethod); GC.KeepAlive(src); GC.KeepAlive(dst); dst.Fix(); }
private static extern void cveNiBlackThreshold(IntPtr src, IntPtr dst, double maxValue, LocalBinarizationMethods type, int blockSize, double delta);
/// <summary> /// Niblack threshold /// </summary> /// <param name="src">The source image</param> /// <param name="dst">The output result</param> /// <param name="type">Value that defines which local binarization algorithm should be used.</param> /// <param name="blockSize">Block size</param> /// <param name="delta">delta</param> /// <param name="maxValue">Maximum value to use with CV_THRESH_BINARY and CV_THRESH_BINARY_INV thresholding types</param> public static void NiBlackThreshold(IInputArray src, IOutputArray dst, double maxValue, LocalBinarizationMethods type, int blockSize, double delta) { using (InputArray iaSrc = src.GetInputArray()) using (OutputArray oaDst = dst.GetOutputArray()) { cveNiBlackThreshold(iaSrc, oaDst, maxValue, type, blockSize, delta); } }