日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# emgucv 切图_自己积累的一些Emgu CV代码(主要有图片格式转换,图片裁剪,图片翻转,图片旋转和图片平移等功能)...

發布時間:2024/1/8 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# emgucv 切图_自己积累的一些Emgu CV代码(主要有图片格式转换,图片裁剪,图片翻转,图片旋转和图片平移等功能)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.Runtime.InteropServices;

using Emgu.CV;

using Emgu.CV.CvEnum;

using Emgu.CV.Structure;

namespace ZNLGIS

{

public class OpenCVEmguCVDotNet

{

///

/// 將MIplImage結構轉換到IplImage指針;

/// 注意:指針在使用完之后必須用Marshal.FreeHGlobal方法釋放。

///

/// MIplImage對象

/// 返回IplImage指針

public static IntPtr MIplImageToIplImagePointer(MIplImage mi)

{

IntPtr ptr = Marshal.AllocHGlobal(mi.nSize);

Marshal.StructureToPtr(mi, ptr, false);

return ptr;

}

///

/// 將IplImage指針轉換成MIplImage結構

///

/// IplImage指針

/// 返回MIplImage結構

public static MIplImage IplImagePointerToMIplImage(IntPtr ptr)

{

return (MIplImage)Marshal.PtrToStructure(ptr, typeof(MIplImage));

}

///

/// 將IplImage指針轉換成Emgucv中的Image對象;

/// 注意:這里需要您自己根據IplImage中的depth和nChannels來決定

///

/// Color type of this image (either Gray, Bgr, Bgra, Hsv, Hls, Lab, Luv, Xyz or Ycc)

/// Depth of this image (either Byte, SByte, Single, double, UInt16, Int16 or Int32)

/// IplImage指針

/// 返回Image對象

public static Image IplImagePointerToEmgucvImage(IntPtr ptr)

where TColor : struct, IColor

where TDepth : new()

{

MIplImage mi = IplImagePointerToMIplImage(ptr);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

}

///

/// 將IplImage指針轉換成Emgucv中的IImage接口;

/// 1通道對應灰度圖像,3通道對應BGR圖像,4通道對應BGRA圖像。

/// 注意:3通道可能并非BGR圖像,而是HLS,HSV等圖像

///

/// IplImage指針

/// 返回IImage接口

public static IImage IplImagePointToEmgucvIImage(IntPtr ptr)

{

MIplImage mi = IplImagePointerToMIplImage(ptr);

Type tColor;

Type tDepth;

string unsupportedDepth = "不支持的像素位深度IPL_DEPTH";

string unsupportedChannels = "不支持的通道數(僅支持1,2,4通道)";

switch (mi.nChannels)

{

case 1:

tColor = typeof(Gray);

switch (mi.depth)

{

case IPL_DEPTH.IPL_DEPTH_8U:

tDepth = typeof(Byte);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_16U:

tDepth = typeof(UInt16);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_16S:

tDepth = typeof(Int16);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_32S:

tDepth = typeof(Int32);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_32F:

tDepth = typeof(Single);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_64F:

tDepth = typeof(Double);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

default:

throw new NotImplementedException(unsupportedDepth);

}

case 3:

tColor = typeof(Bgr);

switch (mi.depth)

{

case IPL_DEPTH.IPL_DEPTH_8U:

tDepth = typeof(Byte);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_16U:

tDepth = typeof(UInt16);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_16S:

tDepth = typeof(Int16);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_32S:

tDepth = typeof(Int32);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_32F:

tDepth = typeof(Single);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_64F:

tDepth = typeof(Double);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

default:

throw new NotImplementedException(unsupportedDepth);

}

case 4:

tColor = typeof(Bgra);

switch (mi.depth)

{

case IPL_DEPTH.IPL_DEPTH_8U:

tDepth = typeof(Byte);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_16U:

tDepth = typeof(UInt16);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_16S:

tDepth = typeof(Int16);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_32S:

tDepth = typeof(Int32);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_32F:

tDepth = typeof(Single);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

case IPL_DEPTH.IPL_DEPTH_64F:

tDepth = typeof(Double);

return new Image(mi.width, mi.height, mi.widthStep, mi.imageData);

default:

throw new NotImplementedException(unsupportedDepth);

}

default:

throw new NotImplementedException(unsupportedChannels);

}

}

///

/// 將Emgucv中的Image對象轉換成IplImage指針;

///

/// Color type of this image (either Gray, Bgr, Bgra, Hsv, Hls, Lab, Luv, Xyz or Ycc)

/// Depth of this image (either Byte, SByte, Single, double, UInt16, Int16 or Int32)

/// Image對象

/// 返回IplImage指針

public static IntPtr EmgucvImageToIplImagePointer(Image image)

where TColor : struct, IColor

where TDepth : new()

{

return image.Ptr;

}

///

/// 將IplImage指針轉換成位圖對象;

/// 對于不支持的像素格式,可以先使用cvCvtColor函數轉換成支持的圖像指針

///

/// IplImage指針

/// 返回位圖對象

public static Bitmap IplImagePointerToBitmap(IntPtr ptr)

{

MIplImage mi = IplImagePointerToMIplImage(ptr);

PixelFormat pixelFormat; //像素格式

string unsupportedDepth = "不支持的像素位深度IPL_DEPTH";

string unsupportedChannels = "不支持的通道數(僅支持1,2,4通道)";

switch (mi.nChannels)

{

case 1:

switch (mi.depth)

{

case IPL_DEPTH.IPL_DEPTH_8U:

pixelFormat = PixelFormat.Format8bppIndexed;

break;

case IPL_DEPTH.IPL_DEPTH_16U:

pixelFormat = PixelFormat.Format16bppGrayScale;

break;

default:

throw new NotImplementedException(unsupportedDepth);

}

break;

case 3:

switch (mi.depth)

{

case IPL_DEPTH.IPL_DEPTH_8U:

pixelFormat = PixelFormat.Format24bppRgb;

break;

case IPL_DEPTH.IPL_DEPTH_16U:

pixelFormat = PixelFormat.Format48bppRgb;

break;

default:

throw new NotImplementedException(unsupportedDepth);

}

break;

case 4:

switch (mi.depth)

{

case IPL_DEPTH.IPL_DEPTH_8U:

pixelFormat = PixelFormat.Format32bppArgb;

break;

case IPL_DEPTH.IPL_DEPTH_16U:

pixelFormat = PixelFormat.Format64bppArgb;

break;

default:

throw new NotImplementedException(unsupportedDepth);

}

break;

default:

throw new NotImplementedException(unsupportedChannels);

}

Bitmap bitmap = new Bitmap(mi.width, mi.height, mi.widthStep, pixelFormat, mi.imageData);

//對于灰度圖像,還要修改調色板

if (pixelFormat == PixelFormat.Format8bppIndexed)

SetColorPaletteOfGrayscaleBitmap(bitmap);

return bitmap;

}

///

/// 將位圖轉換成IplImage指針

///

/// 位圖對象

/// 返回IplImage指針

public static IntPtr BitmapToIplImagePointer(Bitmap bitmap)

{

IImage iimage = null;

switch (bitmap.PixelFormat)

{

case PixelFormat.Format8bppIndexed:

iimage = new Image(bitmap);

break;

case PixelFormat.Format16bppGrayScale:

iimage = new Image(bitmap);

break;

case PixelFormat.Format24bppRgb:

iimage = new Image(bitmap);

break;

case PixelFormat.Format32bppArgb:

iimage = new Image(bitmap);

break;

case PixelFormat.Format48bppRgb:

iimage = new Image(bitmap);

break;

case PixelFormat.Format64bppArgb:

iimage = new Image(bitmap);

break;

default:

Image tmp1 = new Image(bitmap.Size);

Byte[, ,] data = tmp1.Data;

for (int i = 0; i < bitmap.Width; i++)

{

for (int j = 0; j < bitmap.Height; j++)

{

Color color = bitmap.GetPixel(i, j);

data[j, i, 0] = color.B;

data[j, i, 1] = color.G;

data[j, i, 2] = color.R;

data[j, i, 3] = color.A;

}

}

iimage = tmp1;

break;

}

return iimage.Ptr;

}

///

/// 設置256級灰度位圖的調色板

///

///

public static void SetColorPaletteOfGrayscaleBitmap(Bitmap bitmap)

{

PixelFormat pixelFormat = bitmap.PixelFormat;

if (pixelFormat == PixelFormat.Format8bppIndexed)

{

ColorPalette palette = bitmap.Palette;

for (int i = 0; i < palette.Entries.Length; i++)

palette.Entries[i] = Color.FromArgb(255, i, i, i);

bitmap.Palette = palette;

}

}

}

}

總結

以上是生活随笔為你收集整理的c# emgucv 切图_自己积累的一些Emgu CV代码(主要有图片格式转换,图片裁剪,图片翻转,图片旋转和图片平移等功能)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。