Emgucv 图像操作笔记
这里记下一些学习过程中的心得和技巧。我用VS2008,C#的平台进行编写。
1、将图片载入PictureBox的方法:
Image<Bgr, byte> img = new Image<Bgr, byte>("lena.jpg");
//读入一张BGR图像,要将lena.jpg放入DEBUG目录下。
pictureBox1.Image = img.ToBitmap();
//ToBitmap()将IImage格式转换为Bitmap格式,便能为PictureBox所用了。或者
下面这样:
pictureBox1.Image=img.Bitmap;
发现EmguCV的IImage格式确实很强大呀。。。
2、图片的数据处理
Bgr color=img[y,x];
Img[y,x]=color;
//对Image<Bgr,byte>的第y行第x列进行读取和写入操作。
Bgr格式的数据可以通过
Bgr.Blue,Bgr.Green,Bgr.Red访问
Gray格式的数据可以通过Gray.intensity访问
所有数据都是可以读写的。
3、IntPtr到Image格式的转换
这个当然在现在的新版本已经用不着这么麻烦了,不过还是把代码贴过来,感觉
写的很不错的说,虽然是unsafe的……
private Image ShowIplImageInWindow(IntPtr src)
{
Emgu.CV.Structure.MIplImage img =
(Emgu.CV.Structure.MIplImage)Marshal.PtrToStructure(src,
typeof(Emgu.CV.Structure.MIplImage));
Bitmap disp = new Bitmap(img.width, img.height,
PixelFormat.Format24bppRgb);
BitmapData bmp = disp.LockBits(new Rectangle(0, 0, img.width,
img.height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
long linebytes = (img.width * 24 + 31) / 32 * 4;
unsafe
{
byte* pixel = (byte*)bmp.Scan0.ToPointer();
if (img.nChannels == 3)
{
for (int i = 0; i < img.height; i++)
{
for (int j = 0, n = 0; j < img.width; j++, n++)
{ byte b = ((byte*)img.imageData + img.widthStep *
i)[3 * j];
byte g = ((byte*)img.imageData + img.widthStep *
i)[3 * j + 1];
byte r = ((byte*)img.imageData + img.widthStep *
i)[3 * j + 2];
*(pixel + linebytes * (i) + n) = b;
n++;
*(pixel + linebytes * (i) + n) = g;
n++;
*(pixel + linebytes * (i) + n) = r;
}
}
}
else if (img.nChannels == 1)
{
for (int i = 0; i < img.height; i++)
{
for (int j = 0, n = 0; j < img.width; j++, n++)
{
byte g = ((byte*)img.imageData + img.widthStep *
i)[j];
*(pixel + linebytes * (i) + n) = g;
n++;
*(pixel + linebytes * (i) + n) = g;
n++;
*(pixel + linebytes * (i) + n) = g;
}
}
}
else
{
return null;
}
}
disp.UnlockBits(bmp);
return (Image)disp;
}
当然,Emgu还提供了一个巨强大无比的ImageBox,可以在工具栏里直接使用。详
细介绍在emgu的官网上都有,大家有兴趣的自己去看吧
终于把之前用OpenCV写的两个算法用EmguCV实现了,而且似乎效率还说得过
去,当然了,差不多得比之前慢几倍哈,这就是效率和便捷的取舍了。 最近主要的两个程序,一个是两张图比较差异度,生成一个灰度图,然后计算不
同的像素个数;另一个是统计一张图片中有多少人,用框框圈住就行,然后给个
统计总数。
第一个实现比较简单,算法比较重要;第二个实现比较麻烦,分类器比较重要,
算法其次了。
首先上第一个程序的核心代码
Image<Gray, byte> temp=new
Image<Gray,byte>(back.Bitmap.Width,back.Bitmap.Height);
for (int i = 0; i < back.Bitmap.Height; i++)
{
for (int j = 0; j < back.Bitmap.Width; j++)
{
Bgr backColor = back[i, j];
Bgr frontColor = front[i, j];
temp[i,j]=new Gray(
(Math.Sqrt(backColor.Blue-frontColor.Blue)
+Math.Sqrt(backColor.Green-frontColor.Green)
+Math.Sqrt(backColor.Red-frontColor.Red)>0.2)?0:255);
}
}
上次讲到了,灰度图的点是Gray格式,BGR当然就是Bgr格式了……
Gray和Bgr都可以直接定义,并且可以按照二维数组从图中读取和更改。
每个Image都有一个成员:Bitmap,是将这个图转换为Bitmap的数据,这个非
常强大,因为Bigmap就是C#里面的数据类型而与EmguCV无关。
上次提过一句ImageBox,这个是一个非常强大的东西,可以直接将EmguCV支持
的图像输出在ImageBox里面,更主要的是,可以有非常强大的右键功能,在图
片框里单击右键可以有几乎所有图像处理的基本功能,图像读取、图像smooth,
图像变换,图像放缩,图像存储等等。不试不知道一试吓一跳啊~~~
然后就是人数统计的那个程序了:
Image<Bgr, byte> img = new Image<Bgr, byte>("lena.jpg");
HaarCascade mm = new HaarCascade("data.xml");
Bgr[] colors =
{
new Bgr(0, 0, 255),
new Bgr(0, 128, 255),
new Bgr(0, 255, 255),
new Bgr(0, 255, 0),
new Bgr(255, 128, 0),
new Bgr(255, 255, 0),
new Bgr(255, 0, 0),
new Bgr(255, 0, 255) }; 上面这堆东西全部是初始化。Image不用讲了,HaarCascade就是载入一个分类
器,使用方法非常简单,一句话的事。可以输入路径读取。至于colors数组纯
粹是为了创建几个框框的颜色防止过于单调……
var faces = img.DetectHaarCascade(
mm, 1.1, 2,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(30, 30)
)[0];
for (int i = 0; i < faces.Length; i++)
{
img.Draw(faces[i].rect, colors[i % 8], 3);
}
imageBox1.Image = img;
label1.Text = "人数是:"+faces.Length.ToString();
这里我们发现,原来每个Image还有一个DetectHaarCascade方法啊~~~很神奇
吧,都把模式识别的东西封装到如此地步了,我们还用写什么呢。。。里面的参
数我就不细讲了,给出一个表吧。
HaarCascade
Haar classifier cascade in internal representation
Double
The factor by which the search window is scaled between the
subsequent scans, for example, 1.1 means increasing window by 10%
Int32
Minimum number (minus 1) of neighbor rectangles that makes up an
object. All the groups of a smaller number of rectangles than
min_neighbors-1 are rejected. If min_neighbors is 0, the function
does not any grouping at all and returns all the detected candidate
rectangles, which may be useful if the user wants to apply a
customized grouping procedure
HAAR_DETECTION_TYPE
Mode of operation. Currently the only flag that may be specified
is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny
edge detector to reject some image regions that contain too few or
too much edges and thus can not contain the searched object. The
particular threshold values are tuned for face detection and in this
case the pruning speeds up the processing.
Size
Minimum window size. By default, it is set to the size of samples
the classifier has been trained on (~20x20 for face detection)
The objects detected, one array per channel 然后嘛,就把faces里面的东西搞出来就行啦~~~啦啦啦,太方便了太方便了~~~ 就是这些吧,好困。。。明天又是美好的一天嗯
Emgucv 图像操作笔记的更多相关文章
- 学习笔记TF015:加载图像、图像格式、图像操作、颜色
TensorFlow支持JPG.PNG图像格式,RGB.RGBA颜色空间.图像用与图像尺寸相同(height*width*chnanel)张量表示.通道表示为包含每个通道颜色数量标量秩1张量.图像所有 ...
- HT for Web基于HTML5的图像操作(三)
上篇采用了HTML5的Canvas的globalCompositeOperation属性达到了染色效果,其实CSS也提供了一些常规图像变化的设置参数,关于CSS的过滤器Filter设置可参考 http ...
- 2014 年10个最佳的PHP图像操作库
2014 年10个最佳的PHP图像操作库 Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Pytho ...
- 2014 年10个最佳的PHP图像操作库--留着有用
Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...
- 10个最佳的PHP图像操作库
Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...
- C#中Bitmap类 对图像の操作 可检测图片完整性
try { Bitmap bm = new Bitmap(pics[ip]); BitmapToBytes(bm).Reverse().Take(2); } catch (Exception ex) ...
- Python用Pillow(PIL)进行简单的图像操作
Python用Pillow(PIL)进行简单的图像操作 颜色与RGBA值 计算机通常将图像表示为RGB值,或者再加上alpha值(通透度,透明度),称为RGBA值.在Pillow中,RGBA的值表示为 ...
- Centos7系统下修改主机名操作笔记
习惯了在Centos6系统下修改主机名的操作,但是Centos7下修改主机名的操作却大不相同!操作笔记如下: 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient) ...
- Tensorflow图像操作
图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...
随机推荐
- 温习 socket http tcp
Socket是一个接口,可以实现TCP或者UDP的传输HTTP是协议 资料: 1.TCP/IP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接. ...
- Java Web学习总结(9)——servlet和Jsp生命周期解读
一.servlet的工作工程 Servlet是运行在Servlet容器(有时候也叫Servlet引擎,是web服务器和应用程序服务器的一部分,用于在发送的请求和响应之上提供网络服务,解码基于MIME的 ...
- java.util.ConcurrentModificationException 异常解决的方法及原理
近期在修程序的bug,发现后台抛出下面异常: Exception in thread "main" java.util.ConcurrentModificationExceptio ...
- Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/ma ven/cli/Maven
安装maven 中出现例如以下异常: Exception in thread "main" java.lang.UnsupportedClassVersionError: org/ ...
- HDOJ 5414 CRB and String 模拟
CRB and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- CSS Text
http://www.runoob.com/css/css-text.html 文本颜色 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RG ...
- es6 --- Generator 函数
第一部分,ES6 中的 Generator 在 ES6 出现之前,基本都是各式各样类似Promise的解决方案来处理异步操作的代码逻辑,但是 ES6 的Generator却给异步操作又提供了新的思路, ...
- JavaScript笔记(5)
1.return 跳出当前函数 返回一个结果 <script> //return可以跳出当前函数 所以return写在函数的最后一行 function out() { return fun ...
- nexus 搭建maven私服
1. nexus 下载地址 https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.4-03-bundle.tar.g ...
- 【Codeforces Round #428 (Div. 2) A】Arya and Bran
[Link]: [Description] [Solution] 傻逼题 [NumberOf WA] [Reviw] [Code] #include <bits/stdc++.h> usi ...