AForge.net是一个用于处理图像方面的框架,这是地址:
http://www.codeproject.com/KB/GDI-plus/Image_Processing_Lab.aspx
这个不仅仅是示例代码,而且完全可以运行,可以当成一个Photoshop的替代品使用。
这个产品的另一些应用就是在motion detect上,很多运动识别方面的(主要是用到图片比较的)都会用这个类库来实现。比如http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx
我的小程使用的几个例子:
1:图片转角
2:设置阀值
3:抽取图片上的联通区域
其实还可以用其他方法来实现,比如取得图像差值,然后设置阀值。
应用:媳妇要处理一组实验数据,从图中把一个小气泡的图片取出来,然后计算其直径。
难点:去除噪音。把图片上不相干的其他元素都去掉,只获取要小气泡。并且计算出该区域面积。
当然,她完全可以用matlab来实现。
这里说一下使用AForge.net来实现的方式。
代码
结果如图
如果是用Matlab来实现,阀值可用通过winner2函数取得,这是一个比较好的去处噪音的方法。不过不知道c#里面有没有实现类似的函数。
1 private double ProcessFile(string file)
2 {
3 //Bitmap image = new Bitmap(@"D:\downloads\iplab_src\New Folder\150002.bmp");
4 if (file.ToLower().IndexOf(".bmp") < 0)
5 return -1;
6 Bitmap image = new Bitmap(file);
7 AForge.Imaging.Image.FormatImage(ref image); //设置开始调用
8
9 IFilter filter = new RotateBilinear(double.Parse("45")); //转45度角
10 Bitmap newImage = filter.Apply(image);
11
12 Threshold filter2 = new Threshold();
13 filter2.ThresholdValue = Convert.ToByte(this.txt_threshold.Text); //设置阀值,把图片黑白化,准备区分联通物体
14 newImage = filter2.Apply(newImage);
15 //newImage.Save(@"d:\temp\xx\1_" + Path.GetFileName(file));
16 this.imageTheshold.Image = newImage;
17
18 BlobCounter blobCounter = new BlobCounter(newImage); //把图片上的联通物体都分离开来
19 Blob[] blobs = blobCounter.GetObjects(newImage);
20
21 foreach (Blob blob in blobs)
22 {
23
24 if (blob.Image.Width > Convert.ToInt32(this.txt_bubbleLevel1.Text) && blob.Image.Width < Convert.ToInt32(this.txt_bubbleLevel2.Text))
25 {
26 this.imageBubble.Image = blob.Image; //找到符合条件的物体
27 //blob.Image.Save(@"d:\temp\xxx\1_" + Path.GetFileName(file));
28 Hashtable cls = new Hashtable();
29 for (int i = 0; i < blob.Image.Width; i++)
30 {
31 for (int j = 0; j < blob.Image.Height; j++)
32 {
33 Color c = blob.Image.GetPixel(i, j);
34 if (cls[c] != null)
35 {
36 cls[c] = Convert.ToInt32(cls[c]) + 1;
37 }
38 else
39 {
40 cls[c] = 0;
41 }
42 }
43 }
44 //
45
46 Color sc = Color.FromArgb(255, 255, 255, 255);
47 int size = Convert.ToInt32(cls[sc]);
48 double size2 = size * (150 / 60) * (150 / 60);//计算面积
49 double d0 = 2 * Math.Sqrt((size2 / Math.PI)) +Convert.ToInt32(this.txt_param.Text);//计算直径
50
51 return d0;
52 }
53
54 }
55 return 0;
56 }
RSS订阅






收 藏
推 荐