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里面的东西搞出来就行啦~~~啦啦啦,太方便了太方便了~~~ 就是这些吧,好困。。。明天又是美好的一天嗯

 

posted on 2016-03-04 13:55  song2013  阅读(6135)  评论(0编辑  收藏  举报

导航