-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImagePrep.cs
52 lines (47 loc) · 2.12 KB
/
ImagePrep.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace SSD2
{
static class ImagePrep
{
public static Image<Bgr, Byte> ResizeImage(Image<Bgr, byte> sourceImage, int xRes, int yRes)
{
return sourceImage.Resize(xRes, yRes, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
}
public static Image<Bgr, Byte> GreyscaleImage(Image<Bgr, Byte> sourceImage)
{
//float[,] weights = new float[3, 3]
//{
// { 0.3333f, 0.3333f, 0.3333f }, // "flat" weighting - may as well use the emgu version
// { 0.3000f, 0.5900f, 0.1100f }, // standard NTSC RGB luminence weights
// { 0.3086f, 0.6094f, 0.0820f } // modified weights according to http://www.graficaobscura.com/matrix/index.html
//};
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0.3000f, 0.3000f, 0.3000f, 0, 0},
new float[] {0.5900f, 0.5900f, 0.5900f, 0, 0},
new float[] {0.1100f, 0.1100f, 0.1100f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
// apply the colourmatrix to a set of image attributes
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
// Convert the Iimage to a bmp,
Bitmap sourceBMP = sourceImage.ToBitmap();
// use it to make a blank bmp to itiate a graphics object
Bitmap tempBMP = new Bitmap(sourceBMP.Width, sourceBMP.Height);
Graphics graphic = Graphics.FromImage(tempBMP);
// apply the source image and the color matrix (via the attributes) to the graphics obj
graphic.DrawImage(sourceBMP, new Rectangle(0, 0, sourceBMP.Width, sourceBMP.Height), 0, 0, sourceBMP.Width, sourceBMP.Height, GraphicsUnit.Pixel, attributes);
return new Image<Bgr, byte>(tempBMP);
}
}
}