Interaction between opencv, emgucv, And. Net (the interaction of opencv, emgucv and. Net)

Source: Internet
Author: User
Tags unsupported

Author: Wang Xianrong

Preface

When using opencv and emgucv in. net, they must be converted between the supported image formats .. Net uses bitmap class to carry images, opencv uses iplimage pointer to carry images, and emgucv uses image <tcolor, tdepth> to carry images. This article describes how to convert between iplimage, image <tcolor, tdepth>, and bitmap.

Iplimage <=> miplimage

Miplimage is a hosting implementation in iplimage. It serves as a bridge between. NET and opencv. The conversion between iplimage pointer and miplimage mainly uses the ptrtostructure, structuretoptr, allochglobal, and freehglobal static methods in the marshal class.

/// <Summary> /// convert the miplimage structure to the iplimage pointer; // note: the pointer must be released using the marshal. freehglobal method after use. /// </Summary> /// <Param name = "mi"> miplimage object </param> /// <returns> returns the iplimage pointer </returns> Public static intptr miplimagetoiplimagepointer (miplimage mi) {intptr = marshal. allochglobal (MI. nsize); marshal. structuretoptr (MI, PTR, false); Return PTR ;} /// <summary> /// convert the iplimage pointer to the miplimage structure /// </Summary> /// <Param name = "PTR"> iplimage pointer </param> /// <returns> returns the miplimage structure </returns> Public static miplimage iplimagepointertomiplimage (intptr) {return (miplimage) Marshal. ptrtostructure (PTR, typeof (miplimage ));}

Note that you cannot use methods such as miplimage * PMI = (miplimage *) PTR. topointer (); and intptr = & Mi.

Iplimage <=> image <tcolor, tdepth>

With the help of miplimage, we can easily convert between iplimage pointer and image <tcolor, tdepth>.

/// <Summary> /// convert the iplimage pointer to an image object in emgucv. // note: here, you need to decide based on depth and nchannels in iplimage /// </Summary> // <typeparam name = "tcolor"> color type of this image (either gray, BGR, bgra, HSV, HLS, lab, luv, XYZ or ycc) </typeparam> // <typeparam name = "tdepth"> depth of this image (either byte, sbyte, single, double, uint16, int16 or int32) </typeparam> // <Param name = "PTR"> iplimage pointer </param> /// <r Eturns> returns the image object </returns> Public static image <tcolor, tdepth> iplimagepointertoemgucvimage <tcolor, tdepth> (intptr PTR) Where tcolor: struct, icolor where tdepth: New () {miplimage MI = iplimagepointertomiplimage (PTR); return new image <tcolor, tdepth> (MI. width, MI. height, MI. widthstep, MI. imagedata) ;}//< summary> /// convert the iplimage pointer to the iimage interface in emgucv; // Channel 1 corresponds to the grayscale image, and channel 3 corresponds to the BGR image, 4 channels correspond to bgra images. /// Note: 3 channels may not be BGR images, but HLS, HSV and other images // </Summary> /// <Param name = "PTR"> iplimage pointer </param> /// <returns> return iimage interface </returns> public static iimage iplimagepointtoemgucviimage (intptr) {miplimage MI = iplimagepointertomiplimage (PTR); Type tcolor; Type tdepth; string unsupporteddepth = "unsupported pixel depth ipl_depth "; string unsupportedchannels = "Number of unsupported channels (only 1, 2, and 4 channels are supported)"; Switch (MI. nchannels) {Case 1: tcolor = typeof (Gray); Switch (MI. depth) {Case ipl_depth.ipl_depth_8u: tdepth = typeof (byte); return new image <gray, byte> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_16u: tdepth = typeof (uint16); return new image <gray, uint16> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_16s: tdepth = typeof (int16); return new image <gray, int16> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_32s: tdepth = typeof (int32); return new image <gray, int32> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_32f: tdepth = typeof (single); return new image <gray, single> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_64f: tdepth = typeof (double); return new image <gray, double> (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 <BGR, byte> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_16u: tdepth = typeof (uint16); return new image <BGR, uint16> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_16s: tdepth = typeof (int16); return new image <BGR, int16> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_32s: tdepth = typeof (int32); return new image <BGR, int32> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_32f: tdepth = typeof (single); return new image <BGR, single> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_64f: tdepth = typeof (double); return new image <BGR, double> (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 <bgra, byte> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_16u: tdepth = typeof (uint16); return new image <bgra, uint16> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_16s: tdepth = typeof (int16); return new image <bgra, int16> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_32s: tdepth = typeof (int32); return new image <bgra, int32> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_32f: tdepth = typeof (single); return new image <bgra, single> (MI. width, MI. height, MI. widthstep, MI. imagedata); Case ipl_depth.ipl_depth_64f: tdepth = typeof (double); return new image <bgra, double> (MI. width, MI. height, MI. widthstep, MI. imagedata); default: Throw new notimplementedexception (unsupporteddepth);} default: Throw new notimplementedexception (unsupportedchannels );}} /// <summary> /// convert the image object in emgucv to an iplimage pointer; /// </Summary> /// <typeparam name = "tcolor"> color type of this image (either gray, BGR, bgra, HSV, HLS, lab, luv, XYZ or ycc) </typeparam> // <typeparam name = "tdepth"> depth of this image (either byte, sbyte, single, double, uint16, int16 or int32) </typeparam> /// <Param name = "image"> image object </param> /// <returns> returns the iplimage pointer </returns> Public static intptr emgucvimagetoiplimagepointer <tcolor, tdepth> (image <tcolor, tdepth> image) Where tcolor: struct, icolor where tdepth: New () {return image. PTR ;}

Image <tcolor, tdepth> <=> bitmap

Emgucv has implemented the conversion between the two, which are the following members of the image <tcolor, tdepth> class:

(1) Public Bitmap bitmap {Get; set ;}

This attribute can be used to obtain or set bitmaps. For image <gray, byte>, image <BGR, byte>, and image <bgra, byte>, the efficiency is high because image <tcolor, tdepth> shares data memory with bitmap.

(2) Public bitmap tobitmap (INT width, int height) and Public bitmap tobitmap () Methods

 
(3) Public Image (bitmap BMP)
 
(4) Public Image (INT width, int height, int stride, intptr scan0)
 
This constructor is almost omnipotent as long as you know the Memory Distribution of the image and the purpose you want.
 
 
 
Iplimage <=> bitmap
 
There are two ways to convert the iplimage pointer and bitmap. The first is to use image <tcolor, tdepth> as the media, and the second is to write the conversion method by yourself, for example, the followingCode:
/// <Summary> /// convert the iplimage pointer to a bitmap object. /// for unsupported pixel format, you can use the cvcvtcolor function to convert the image to a supported image pointer /// </Summary> /// <Param name = "PTR"> iplimage pointer </param> /// <returns> return bitmap object </returns> Public static bitmap iplimagepointertobitmap (intptr) {miplimage MI = iplimagepointertomiplimage (PTR); pixelformat; // pixel Format String unsupporteddepth = "unsupported pixel depth ipl_depth "; string unsupportedchannels = "Number of unsupported channels (only 1, 2, and 4 channels are supported)"; 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. width, MI. height, MI. widthstep, pixelformat, MI. imagedata); // for Grayscale Images, you must modify the color palette if (pixelformat = pixelformat. format8bppindexed) setcolorpaletteofgrayscalebitmap (Bitmap); Return bitmap ;} /// <summary> /// convert a bitmap to an iplimage pointer // </Summary> /// <Param name = "bitmap"> bitmap object </param>/ // <returns> returns the iplimage pointer </returns> Public static intptr bitmaptoiplimagepointer (Bitmap bitmap) {iimage = NULL; Switch (bitmap. pixelformat) {Case pixelformat. format8bppindexed: iimage = new image <gray, byte> (Bitmap); break; Case pixelformat. format16bppgrayscale: iimage = new image <gray, uint16> (Bitmap); break; Case pixelformat. format24bpprgb: iimage = new image <BGR, byte> (Bitmap); break; Case pixelformat. format32bppargb: iimage = new image <bgra, byte> (Bitmap); break; Case pixelformat. format48bpprgb: iimage = new image <BGR, uint16> (Bitmap); break; Case pixelformat. format64bppargb: iimage = new image <bgra, uint16> (Bitmap); break; default: Image <bgra, byte> tmp1 = new image <bgra, byte> (bitmap. size); byte [,] DATA = tmp1.data; For (INT I = 0; I <bitmap. width; I ++) {for (Int J = 0; j <bitmap. height; j ++) {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 ;} /// <summary> /// set the 256 level grayscale bitmap palette /// </Summary> /// <Param name = "bitmap"> </param> Public static void setcolorpaletteofgrayscalebitmap (Bitmap bitmap) {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); bitmap. palette = palette ;}}

at last, it's hard to type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.