by
84k 18
0
5
958
0
Top 5% !
Popular
Tagged
Specified
Refined
Popularity: 65780th place

Published on:

Architecturex86_64
InputName0image
InputName1rect
InputType0UnmanagedImage
InputType1Rectangle
LanguageC#
MethodNameCalculateThreshold
OutputTypeint
Windows VersionWindows7

CalculateThreshold: Calculate binarization threshold for the given image.

Calculate binarization threshold for the given image.

 
<param name="image">Image to calculate binarization threshold for.</param>
<param name="rect">Rectangle to calculate binarization threshold for.</param>
 
<returns>Returns binarization threshold.</returns>
 
<remarks><para>The method is used to calculate binarization threshold only. The threshold
later may be applied to the image using <see cref="Threshold"/> image processing filter.</para></remarks>
 
<exception cref="UnsupportedImageFormatException">Source pixel format is not supported by the routine. It should be
8 bpp grayscale (indexed) image.</exception>
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1383579/CalculateThreshold--Calculate-binarizati?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/// <summary> /// Calculate binarization threshold for the given image. /// </summary> /// /// <param name="image">Image to calculate binarization threshold for.</param> /// <param name="rect">Rectangle to calculate binarization threshold for.</param> /// /// <returns>Returns binarization threshold.</returns> /// /// <remarks><para>The method is used to calculate binarization threshold only. The threshold /// later may be applied to the image using <see cref="Threshold"/> image processing filter.</para></remarks> /// /// <exception cref="UnsupportedImageFormatException">Source pixel format is not supported by the routine. It should be /// 8 bpp grayscale (indexed) image.</exception> /// public static int CalculateThreshold(UnmanagedImage image, Rectangle rect) { if (image.PixelFormat != PixelFormat.Format8bppIndexed) throw new UnsupportedImageFormatException("Source pixel format is not supported by the routine."); int startX = rect.Left; int startY = rect.Top; int stopX = startX + rect.Width; int stopY = startY + rect.Height; int stopXM1 = stopX - 1; int stopYM1 = stopY - 1; int stride = image.Stride; int offset = stride - rect.Width; // differences and weights double ex, ey, weight, weightTotal = 0, total = 0; unsafe { // do the job byte* ptr = (byte*)image.ImageData.ToPointer(); // allign pointer to the first pixel to process ptr += (startY * image.Stride + startX); // skip the first line for the first pass ptr += stride; // for each line for (int y = startY + 1; y < stopYM1; y++) { ptr++; // for each pixels for (int x = startX + 1; x < stopXM1; x++, ptr++) { // the equations are: // ex = | I(x + 1, y) - I(x - 1, y) | // ey = | I(x, y + 1) - I(x, y - 1) | // weight = max(ex, ey) // weightTotal += weight // total += weight * I(x, y) ex = Math.Abs(ptr[1] - ptr[-1]); ey = Math.Abs(ptr[stride] - ptr[-stride]); weight = (ex > ey) ? ex : ey; weightTotal += weight; total += weight * (*ptr); } ptr += offset + 1; } } // calculate threshold return (weightTotal == 0) ? (byte)0 : (byte)(total / weightTotal); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus