阈值的选择当然希望智能、简单一些。应该能应付一般的图片。
What is Binarization?
Binarization is the process of transforming data features of any entity into vectors of binary numbers to make classifier algorithms more efficient. In a simple example, transforming an image’s gray-scale from the 0-255 spectrum to a 0-1 spectrum is binarization.
How is Binarization used?
In machine learning, even the most complex concepts can be transformed into binary form. For example, to binarize the sentence “The dog ate the cat,” every word is assigned an ID (for example dog-1, ate-2, the-3, cat-4). Then replace each word with the tag to provide a binary vector. In this case the vector: <3,1,2,3,4> can be refined by providing each word with four possible slots, then setting the slot to correspond with a specific word: <0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,1>. This is commonly referred to as the bag-of-words-method.
Since the ultimate goal is to make this data easier for the classifier to read while minimizing memory usage, it’s not always necessary to encode the whole sentence or all the details of a complex concept. In this case, only the current state of how the data is parsed is needed for the classifier. For example, when the top word on the stack is used as the first word in the input queue. Since order is quite important, a simpler binary vector is preferable.
二值算法综述请阅读:
C#,图像二值化(01)——二值化算法综述与二十三种算法目录https://blog.csdn.net/beijinghorn/article/details/128425225?spm=1001.2014.3001.5502
支持函数请阅读:
C#,图像二值化(02)——用于图像二值化处理的一些基本图像处理函数之C#源代码https://blog.csdn.net/beijinghorn/article/details/128425984?spm=1001.2014.3001.5502
联高简化版自适应算法是个不错的选择。
using System;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace Legalsoft.Truffer.ImageTools
{
public static partial class BinarizationHelper
{
#region 灰度图像二值化 全局算法 联高 算法
///
/// 计算图片的自适应灰度阈值
///
/// 灰度化的图片数据
///
public static byte Adaptive_Threshold(byte[,] data, double ratio)
{
int height = data.GetLength(0);
int width = data.GetLength(1);
int[] histogram = Gray_Histogram(data);
double sum = (width * height) / ratio;
int cnt = 0;
byte left = 0;
while (left < 255 && cnt < sum)
{
cnt += histogram[left++];
}
cnt = 0;
byte right = 255;
while (right > left && cnt < sum)
{
cnt += histogram[right--];
}
return (byte)((left + right) / 2);
}
///
/// 图像二值化的联高自适应算法
///
/// 灰度化的图片数据
///
public static void Automatic_Adaptive_Algorithm(byte[,] data, double ratio = 8.0)
{
byte threshold = Adaptive_Threshold(data, ratio);
Threshold_Algorithm(data, threshold);
}
#endregion
}
}
Binarization is important in digital image processing, mainly in computer vision applications. Thresholding is an efficient technique in binarization. The choice of thresholding technique is crucial in binarization. There are various thresholding algorithms have been proposed to define the optimal threshold value.
Binarization can be used in recognising text and symbols, e.g. document processing. Identifying objects with distinctive silhouettes, e.g. components on a conveyor in a manufacturing plant, and determining the orientation of objects are some other examples of binarization applications. Binarization generally involves two steps including the determination of a gray threshold according to some objective criteria and assigning each pixel to one class of background or foreground. If the intensity of the pixel is greater than the determined threshold then it belongs to the foreground class and otherwise to the background. The main problem in binarization is the choice of thresholding technique.
可见 ratio 的取值影响不大,该算法比较实用。
Image Thresholding
Image thresholding is a simple form of image segmentation. It is a way to create a binary image from a grayscale or full-color image. This is typically done in order to separate "object" or foreground pixels from background pixels to aid in image processing.
Thresholding is the simplest method of image segmentation, that replace each pixel in an image with a black pixel, if the image intensity is less than than some fixed constant, or a white pixel if image intensity is greater than the constant.
As the name implies, image thresholding allows us to apply a certain “threshold” to determine whether each pixel is of interest to us or not. Consequently, there are many practical and useful applications of image thresholding. For example we can use this to create a mask in order to isolate certain parts of an image. Alternatively, it can also be a good means to reduce noise within an image. With this in mind, let’s see how to use this in OpenCV.