using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Orc.SmartImage;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Orc.SmartImage.Gpu;
using Orc.SmartImage.UnmanagedObjects;
namespace Orc.SmartImage.PerformanceTest
{
public class PerformanceTestCase0
{
public static String Test(IntPtr hwnd, Bitmap src, int count)
{
Shader sd = new Shader(hwnd);
// ArgbImage8 img8 = ArgbImage8.CreateFromBitmap(src);
Argb32Image img32 = Argb32Image.CreateFromBitmap(src);
StringBuilder sb = new StringBuilder();
Stopwatch sw = new Stopwatch();
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
ProcessImageWithEmgucv(src);
sw.Stop();
sb.AppendLine("EmguCV:" + sw.ElapsedMilliseconds.ToString());
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
ProcessImageWithOpencv(src);
sw.Stop();
sb.AppendLine("OpenCV:" + sw.ElapsedMilliseconds.ToString());
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
Grayscale(src);
sw.Stop();
sb.AppendLine("BitmapData:" + sw.ElapsedMilliseconds.ToString());
//DoSomething();
//sw.Reset();
//sw.Start();
//for (int i = 0; i < count; i++)
// img8.ToGrayscaleImage();
//sw.Stop();
//sb.AppendLine("ArgbImage8:" + sw.ElapsedMilliseconds.ToString());
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
img32.ToGrayscaleImage();
sw.Stop();
sb.AppendLine("ArgbImage32:" + sw.ElapsedMilliseconds.ToString());
//sw.Reset();
//sw.Start();
//for (int i = 0; i < count; i++)
// img8.ToGrayscaleImage();
//sw.Stop();
//sb.AppendLine("ArgbImage8:" + sw.ElapsedMilliseconds.ToString());
return sb.ToString();
}
private static int[] DoSomething()
{
int[] data = new Int32[20000000];
for (int i = 0; i < data.Length; i++)
{
data[i] = i;
}
return data;
}
private static GrayscaleImage TestMyConvert(ArgbImage img)
{
return img.ToGrayscaleImage();
}
/// <summary>
/// 使用EmguCv处理图像
/// </summary>
private static void ProcessImageWithEmgucv(Bitmap bitmapSource)
{
//灰度
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>(bitmapSource);
Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>();
}
/// <summary>
/// 使用Open Cv P/Invoke处理图像
/// </summary>
unsafe private static void ProcessImageWithOpencv(Bitmap bitmapSource)
{
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>(bitmapSource);
IntPtr ptrSource = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MIplImage)));
Marshal.StructureToPtr(imageSource.MIplImage, ptrSource, true);
IntPtr ptrGrayscale = CvInvoke.cvCreateImage(imageSource.Size, IPL_DEPTH.IPL_DEPTH_8U, 1);
CvInvoke.cvCvtColor(ptrSource, ptrGrayscale, COLOR_CONVERSION.CV_BGR2GRAY);
}
/// <summary>
/// 将指定图像转换成灰度图
/// </summary>
/// <param name="bitmapSource">源图像支持3通道或者4通道图像,支持Format24bppRgb、Format32bppRgb和Format32bppArgb这3种像素格式</param>
/// <returns>返回灰度图,如果转化失败,返回null。</returns>
private static Bitmap Grayscale(Bitmap bitmapSource)
{
Bitmap bitmapGrayscale = null;
if (bitmapSource != null && (bitmapSource.PixelFormat == PixelFormat.Format24bppRgb || bitmapSource.PixelFormat == PixelFormat.Format32bppArgb || bitmapSource.PixelFormat == PixelFormat.Format32bppRgb))
{
int width = bitmapSource.Width;
int height = bitmapSource.Height;
Rectangle rect = new Rectangle(0, 0, width, height);
bitmapGrayscale = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
//设置调色板
ColorPalette palette = bitmapGrayscale.Palette;
for (int i = 0; i < palette.Entries.Length; i++)
palette.Entries[i] = Color.FromArgb(255, i, i, i);
bitmapGrayscale.Palette = palette;
BitmapData dataSource = bitmapSource.LockBits(rect, ImageLockMode.ReadOnly, bitmapSource.PixelFormat);
BitmapData dataGrayscale = bitmapGrayscale.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
byte b, g, r;
int strideSource = dataSource.Stride;
int strideGrayscale = dataGrayscale.Stride;
unsafe
{
byte* ptrSource = (byte*)dataSource.Scan0.ToPointer();
byte* ptr1;
byte* ptrGrayscale = (byte*)dataGrayscale.Scan0.ToPointer();
byte* ptr2;
if (bitmapSource.PixelFormat == PixelFormat.Format24bppRgb)
{
for (int row = 0; row < height; row++)
{
ptr1 = ptrSource + strideSource * row;
ptr2 = ptrGrayscale + strideGrayscale * row;
for (int col = 0; col < width; col++)
{
b = *ptr1;
ptr1++;
g = *ptr1;
ptr1++;
r = *ptr1;
ptr1++;
*ptr2 = (byte)(0.114 * b + 0.587 * g + 0.299 * r);
ptr2++;
}
}
}
else //bitmapSource.PixelFormat == PixelFormat.Format32bppArgb || bitmapSource.PixelFormat == PixelFormat.Format32bppRgb
{
for (int row = 0; row < height; row++)
{
ptr1 = ptrSource + strideGrayscale * row;
ptr2 = ptrGrayscale + strideGrayscale * row;
for (int col = 0; col < width; col++)
{
b = *ptr1;
ptr1++;
g = *ptr1;
ptr1++;
r = *ptr1;
ptr1 += 2;
*ptr2 = (byte)(0.114 * b + 0.587 * g + 0.299 * r);
ptr2++;
}
}
}
}
bitmapGrayscale.UnlockBits(dataGrayscale);
bitmapSource.UnlockBits(dataSource);
}
return bitmapGrayscale;
}
}
}
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Orc.SmartImage;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Orc.SmartImage.Gpu;
using Orc.SmartImage.UnmanagedObjects;
namespace Orc.SmartImage.PerformanceTest
{
public class PerformanceTestCase0
{
public static String Test(IntPtr hwnd, Bitmap src, int count)
{
Shader sd = new Shader(hwnd);
// ArgbImage8 img8 = ArgbImage8.CreateFromBitmap(src);
Argb32Image img32 = Argb32Image.CreateFromBitmap(src);
StringBuilder sb = new StringBuilder();
Stopwatch sw = new Stopwatch();
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
ProcessImageWithEmgucv(src);
sw.Stop();
sb.AppendLine("EmguCV:" + sw.ElapsedMilliseconds.ToString());
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
ProcessImageWithOpencv(src);
sw.Stop();
sb.AppendLine("OpenCV:" + sw.ElapsedMilliseconds.ToString());
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
Grayscale(src);
sw.Stop();
sb.AppendLine("BitmapData:" + sw.ElapsedMilliseconds.ToString());
//DoSomething();
//sw.Reset();
//sw.Start();
//for (int i = 0; i < count; i++)
// img8.ToGrayscaleImage();
//sw.Stop();
//sb.AppendLine("ArgbImage8:" + sw.ElapsedMilliseconds.ToString());
DoSomething();
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
img32.ToGrayscaleImage();
sw.Stop();
sb.AppendLine("ArgbImage32:" + sw.ElapsedMilliseconds.ToString());
//sw.Reset();
//sw.Start();
//for (int i = 0; i < count; i++)
// img8.ToGrayscaleImage();
//sw.Stop();
//sb.AppendLine("ArgbImage8:" + sw.ElapsedMilliseconds.ToString());
return sb.ToString();
}
private static int[] DoSomething()
{
int[] data = new Int32[20000000];
for (int i = 0; i < data.Length; i++)
{
data[i] = i;
}
return data;
}
private static GrayscaleImage TestMyConvert(ArgbImage img)
{
return img.ToGrayscaleImage();
}
/// <summary>
/// 使用EmguCv处理图像
/// </summary>
private static void ProcessImageWithEmgucv(Bitmap bitmapSource)
{
//灰度
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>(bitmapSource);
Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>();
}
/// <summary>
/// 使用Open Cv P/Invoke处理图像
/// </summary>
unsafe private static void ProcessImageWithOpencv(Bitmap bitmapSource)
{
Image<Bgr, Byte> imageSource = new Image<Bgr, byte>(bitmapSource);
IntPtr ptrSource = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MIplImage)));
Marshal.StructureToPtr(imageSource.MIplImage, ptrSource, true);
IntPtr ptrGrayscale = CvInvoke.cvCreateImage(imageSource.Size, IPL_DEPTH.IPL_DEPTH_8U, 1);
CvInvoke.cvCvtColor(ptrSource, ptrGrayscale, COLOR_CONVERSION.CV_BGR2GRAY);
}
/// <summary>
/// 将指定图像转换成灰度图
/// </summary>
/// <param name="bitmapSource">源图像支持3通道或者4通道图像,支持Format24bppRgb、Format32bppRgb和Format32bppArgb这3种像素格式</param>
/// <returns>返回灰度图,如果转化失败,返回null。</returns>
private static Bitmap Grayscale(Bitmap bitmapSource)
{
Bitmap bitmapGrayscale = null;
if (bitmapSource != null && (bitmapSource.PixelFormat == PixelFormat.Format24bppRgb || bitmapSource.PixelFormat == PixelFormat.Format32bppArgb || bitmapSource.PixelFormat == PixelFormat.Format32bppRgb))
{
int width = bitmapSource.Width;
int height = bitmapSource.Height;
Rectangle rect = new Rectangle(0, 0, width, height);
bitmapGrayscale = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
//设置调色板
ColorPalette palette = bitmapGrayscale.Palette;
for (int i = 0; i < palette.Entries.Length; i++)
palette.Entries[i] = Color.FromArgb(255, i, i, i);
bitmapGrayscale.Palette = palette;
BitmapData dataSource = bitmapSource.LockBits(rect, ImageLockMode.ReadOnly, bitmapSource.PixelFormat);
BitmapData dataGrayscale = bitmapGrayscale.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
byte b, g, r;
int strideSource = dataSource.Stride;
int strideGrayscale = dataGrayscale.Stride;
unsafe
{
byte* ptrSource = (byte*)dataSource.Scan0.ToPointer();
byte* ptr1;
byte* ptrGrayscale = (byte*)dataGrayscale.Scan0.ToPointer();
byte* ptr2;
if (bitmapSource.PixelFormat == PixelFormat.Format24bppRgb)
{
for (int row = 0; row < height; row++)
{
ptr1 = ptrSource + strideSource * row;
ptr2 = ptrGrayscale + strideGrayscale * row;
for (int col = 0; col < width; col++)
{
b = *ptr1;
ptr1++;
g = *ptr1;
ptr1++;
r = *ptr1;
ptr1++;
*ptr2 = (byte)(0.114 * b + 0.587 * g + 0.299 * r);
ptr2++;
}
}
}
else //bitmapSource.PixelFormat == PixelFormat.Format32bppArgb || bitmapSource.PixelFormat == PixelFormat.Format32bppRgb
{
for (int row = 0; row < height; row++)
{
ptr1 = ptrSource + strideGrayscale * row;
ptr2 = ptrGrayscale + strideGrayscale * row;
for (int col = 0; col < width; col++)
{
b = *ptr1;
ptr1++;
g = *ptr1;
ptr1++;
r = *ptr1;
ptr1 += 2;
*ptr2 = (byte)(0.114 * b + 0.587 * g + 0.299 * r);
ptr2++;
}
}
}
}
bitmapGrayscale.UnlockBits(dataGrayscale);
bitmapSource.UnlockBits(dataSource);
}
return bitmapGrayscale;
}
}
}