图像分析最主要的一步就是二值化,先灰度化,后二值话,相关代码如下
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>((Bitmap)loadPictureBox.Image);
Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>();
if (loadPictureBox.Image != null)
{
loadPictureBox.Image.Dispose();
loadPictureBox.Image = null;
}
loadPictureBox.Image = imageGrayscale.ToBitmap();
Image<Gray, Byte> imageThreshold = imageGrayscale.ThresholdBinary(new Gray(128), new Gray(255));
if (loadPictureBox.Image != null)
{
loadPictureBox.Image.Dispose();
loadPictureBox.Image = null;
}
loadPictureBox.Image = imageThreshold.ToBitmap();
方法二
Image<Gray, Byte> dest = new Image<Gray, Byte>(imageGrayscale.Width, imageGrayscale.Height);
CvInvoke.cvThreshold(imageGrayscale, dest, 30, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
loadPictureBox.Image = dest.ToBitmap();