这里记下一些学习过程中的心得和技巧。我用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 图像操作笔记的更多相关文章

  1. 学习笔记TF015:加载图像、图像格式、图像操作、颜色

    TensorFlow支持JPG.PNG图像格式,RGB.RGBA颜色空间.图像用与图像尺寸相同(height*width*chnanel)张量表示.通道表示为包含每个通道颜色数量标量秩1张量.图像所有 ...

  2. HT for Web基于HTML5的图像操作(三)

    上篇采用了HTML5的Canvas的globalCompositeOperation属性达到了染色效果,其实CSS也提供了一些常规图像变化的设置参数,关于CSS的过滤器Filter设置可参考 http ...

  3. 2014 年10个最佳的PHP图像操作库

    2014 年10个最佳的PHP图像操作库   Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Pytho ...

  4. 2014 年10个最佳的PHP图像操作库--留着有用

    Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...

  5. 10个最佳的PHP图像操作库

    Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...

  6. C#中Bitmap类 对图像の操作 可检测图片完整性

    try { Bitmap bm = new Bitmap(pics[ip]); BitmapToBytes(bm).Reverse().Take(2); } catch (Exception ex) ...

  7. Python用Pillow(PIL)进行简单的图像操作

    Python用Pillow(PIL)进行简单的图像操作 颜色与RGBA值 计算机通常将图像表示为RGB值,或者再加上alpha值(通透度,透明度),称为RGBA值.在Pillow中,RGBA的值表示为 ...

  8. Centos7系统下修改主机名操作笔记

    习惯了在Centos6系统下修改主机名的操作,但是Centos7下修改主机名的操作却大不相同!操作笔记如下: 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient) ...

  9. Tensorflow图像操作

    图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...

随机推荐

  1. sublime 编辑器汉化

    一.下载Sublime编辑器 官网下载地址:http://www.sublimetext.com/3 二.下载汉化包 汉化包下载地址:http://files.cnblogs.com/akwwl/su ...

  2. 利用js 获取ip和地址

    1.引用第三方js<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> 2.     I ...

  3. Effective C++ Item 38 通过复合塑模出 has-a 或 is-implemented-in-terms-of

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:在应用域,复合意味着 has-a. 在实现域.复合意味着 is-implemented ...

  4. jquery20--animate() : 运动的方法

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. Android webview 运行时不调用系统自带浏览器

    WebView mobView = new WebView(this); mobView.loadUrl("http://www.csdn.net"); WebSettings w ...

  6. Kinect 开发 —— 语音识别(上)

    Kinect的麦克风阵列在Kinect设备的下方.这一阵列由4个独立的水平分布在Kinect下方的麦克风组成.虽然每一个麦克风都捕获相同的音频信号,但是组成阵列可以探测到声音的来源方向.使得能够用来识 ...

  7. 搭建 Nginx 静态网站

    示例代码:/etc/nginx/nginx.conf user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid / ...

  8. 压状态bfs

    一般地图很小,状态不多,可以装压或者hash,构造压缩或hash的函数,构造还原地图的函数,然后就无脑bfs(感觉就是SPFA) 题目: 1.玩具游戏:二进制压缩状态 #include<cstd ...

  9. shell项目-分发系统-构建文件分发系统

    shell项目-分发系统-构建文件分发系统 需求背景对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台.所以,自动同步文件是至关重要的. 实 ...

  10. 【例题 8-11 UVA-10954】Add All

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 就是合并果子.. 每次都合并最小的就可以啦. 别忘了初始化 [代码] /* 1.Shoud it use long long ? 2 ...