public static class HalconEx
{
#region Public Methods
/// <summary>
/// Determines whether the specified halcon object, image, xld or region is not null and is initialized.
/// </summary>
/// <param name="from"> Halcon Object</param>
/// <returns></returns>
public static bool IsValid(this HObject source)
{
var result = false;
if (null != @source)
{
if (@source.IsInitialized())
{
result = @source.CountObj() > 0;
}
}
return result;
}
/// <summary>
/// convert an HObject containing an image to an HImage
/// </summary>
/// <param name="form"></param>
/// <returns></returns>
public static HImage ToHImage(this HObject source)
{
var imageTemp = new HImage();
if (source.IsValid())
{
if (source.GetObjClass().S == "image")
{
imageTemp.Dispose();
imageTemp = new HImage(source);
}
}
return imageTemp;
}
/// <summary>
/// Converts a bitmap to a Halcon image.
/// </summary>
/// <param name="from"></param>
/// <returns></returns>
public static HImage ToHImage(this Bitmap source)
{
Contract.Requires(source != null);
Rectangle rectangle = new Rectangle(0, 0, source.Width, source.Height);
var interleavedHalconImage = new HImage();
// Convert 24 bit bitmap to 32 bit bitmap in order to ensure
// that the bit width of the image (the Stride) is divisible by four.
// Otherwise, one might obtain skewed conversions.
var image32 = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppRgb);
image32.SetResolution(source.HorizontalResolution, source.VerticalResolution);
using (Graphics g = Graphics.FromImage(image32))
{
g.DrawImage(source, new Point(0, 0));
}
// Obtain the image pointer.
var bitmapData = image32.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
IntPtr pointerToBitmap = bitmapData.Scan0;
IntPtr pointerToPixels = pointerToBitmap;
// Create HALCON image from the pointer.
interleavedHalconImage.GenImageInterleaved(pointerToPixels, "bgrx", source.Width, source.Height,
-1, "byte", source.Width, source.Height, 0, 0, -1, 0);
// Don't forget to unlock the bits again. ;-)
image32.UnlockBits(bitmapData);
var outputHalconImage = interleavedHalconImage.CopyImage();
// Release memory by dereferencing and garbage collection
interleavedHalconImage.Dispose();
image32.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
return outputHalconImage;
}
/// <summary>
/// Converts an HObject containing an region to an HRegion.
/// </summary>
/// <param name="from"></param>
/// <returns></returns>
public static HRegion ToHRegion(this HObject source)
{
var newRegion = new HRegion();
if (source.IsValid())
{
if (source.GetObjClass().S == "region")
{
newRegion.Dispose();
newRegion = new HRegion(source);
}
}
return newRegion;
}
/// <summary>
/// Converts an HObject containing an xld_cont to an HXLDCont.
/// </summary>
/// <param name="from"></param>
/// <returns></returns>
public static HXLDCont ToHXLDCont(this HObject source)
{
var newXldCont = new HXLDCont();
if (source.IsValid())
{
if (source.GetObjClass().S == "xld_cont")
{
newXldCont.Dispose();
newXldCont = new HXLDCont(source);
}
}
return newXldCont;
}
/// <summary>
/// Converts an HObject containing an xld_parallel to an HXLDPara.
/// </summary>
/// <param name="from"></param>
/// <returns></returns>
public static HXLDPara ToHXLDPara(this HObject source)
{
var newXldPara = new HXLDPara();
if (source.IsValid())
{
if (source.GetObjClass().S == "xld_parallel")
{
newXldPara.Dispose();
newXldPara = new HXLDPara(source);
}
}
return newXldPara;
}
/// <summary>
/// Converts an HObject containing an xld_poly to an HXLDPoly.
/// </summary>
/// <param name="from"></param>
/// <returns></returns>
public static HXLDPoly ToHXLDPoly(this HObject source)
{
var newXldPoly = new HXLDPoly();
if (source.IsValid())
{
if (source.GetObjClass().S == "xld_poly")
{
newXldPoly.Dispose();
newXldPoly = new HXLDPoly(source);
}
}
return newXldPoly;
}
/// <summary>
/// Get an HTuple TypeName
/// </summary>
/// <param name="from"></param>
/// <returns></returns>
public static string ToTypedString(this HTuple source)
{
switch (source.Type)
{
case HTupleType.INTEGER:
return source.I.ToString();
case HTupleType.LONG:
return source.L.ToString();
case HTupleType.DOUBLE:
return source.D.ToString("G");
case HTupleType.STRING:
return source;
default:
return string.Empty;
}
}
/// <summary>
/// Get an HTupleElements TypeName
/// </summary>
/// <param name="from"></param>
/// <returns></returns>
public static string ToTypedString(this HTupleElements source)
{
switch (source.Type)
{
case HTupleType.INTEGER:
return source.I.ToString();
case HTupleType.LONG:
return source.L.ToString();
case HTupleType.DOUBLE:
return source.D.ToString("G");
case HTupleType.STRING:
return source;
default:
return string.Empty;
}
}
/// <summary>
/// Returns the convex hull from a collection of XLD contours.
/// </summary>
/// <param name="from">The collection of XLD contours to be processed.</param>
/// <returns>An XLD contour that is the convex hull.</returns>
/// <example> HXLDCont convexHullXld = MyXldContours.ConvexHull (); .</example>
public static HXLDCont ConvexHull(this HXLDCont source)
{
HXLDCont tempContour = new HXLDCont(), convexHull = new HXLDCont();
HTuple cumulativeRows = new HTuple(), cumulativeColumns = new HTuple();
try
{
if (@source != null && @source.IsValid())
{
int count = @source.CountObj();
if (count > 1)
{
for (int i = 1; i <= count; i++)
{
@source[i].GetContourXld(out HTuple rows, out HTuple columns);
cumulativeRows = cumulativeRows.TupleConcat(rows);
cumulativeColumns = cumulativeColumns.TupleConcat(columns);
}
tempContour.GenContourPolygonXld(
cumulativeRows,
cumulativeColumns);
convexHull = tempContour.ShapeTransXld("convex");
}
else
{
convexHull = @source.ShapeTransXld("convex");
}
}
if (convexHull.IsInitialized())
{
return convexHull.CopyObj(1, -1);
}
else
{
return new HXLDCont();
}
}
finally
{
tempContour.Dispose();
convexHull.Dispose();
}
}
/// <summary>
/// A helper method to ensure that a HALCON HImage is correctly disposed when assigned a new value.
/// </summary>
/// <param name="from">The source HImage.</param>
/// <param name="to">The destination HImage.</param>
/// <exception cref="ArgumentNullException">from must not be null.</exception>
/// <exception cref="System.ArgumentNullException"></exception>
public static void HCopy(this HImage from, ref HImage to)
{
try
{
if (@from == null)
{
throw new ArgumentNullException(nameof(from));
}
if (to == null)
{
to = new HImage();
}
to.Dispose();
to = @from.CopyObj(1, -1);
}
finally
{
if (@from != null)
{
@from.Dispose();
}
}
}
/// <summary>
/// A helper method to ensure that a HALCON HRegion is correctly disposed when assigned a
/// new value.
/// </summary>
/// <param name="from">The source HRegion.</param>
/// <param name="to">The destination HRegion.</param>
/// <exception cref="ArgumentNullException">from must not be null.</exception>
/// <exception cref="System.ArgumentNullException"></exception>
public static void HCopy(this HRegion from, ref HRegion to)
{
try
{
if (@from == null)
{
throw new ArgumentNullException(nameof(from));
}
if (to == null)
{
to = new HRegion();
}
to.Dispose();
to = @from.CopyObj(1, -1);
}
finally
{
if (@from != null)
{
@from.Dispose();
}
}
}
/// <summary>
/// A helper method to ensure that a HALCON HXLD is correctly disposed when assigned a new value.
/// </summary>
/// <param name="from">The source HXLD.</param>
/// <param name="to">The destination HXLD.</param>
/// <exception cref="ArgumentNullException">from must not be null.</exception>
/// <exception cref="System.ArgumentNullException"></exception>
public static void HCopy(this HXLD from, ref HXLD to)
{
try
{
if (@from == null)
{
throw new ArgumentNullException(nameof(from));
}
if (to == null)
{
to = new HXLD();
}
to.Dispose();
to = @from.CopyObj(1, -1);
}
finally
{
if (@from != null)
{
@from.Dispose();
}
}
}
/// <summary>
/// A helper method to ensure that a HALCON XLD Contour is correctly disposed when assigned a
/// new value.
/// </summary>
/// <param name="from">The source HXLDCont.</param>
/// <param name="to">The destination HXLDCont.</param>
/// <exception cref="ArgumentNullException">from must not be null.</exception>
/// <exception cref="System.ArgumentNullException"></exception>
public static void HCopy(this HXLDCont from, ref HXLDCont to)
{
try
{
if (@from == null)
{
throw new ArgumentNullException(nameof(from));
}
if (to == null)
{
to = new HXLDCont();
}
to.Dispose();
to = @from.CopyObj(1, -1);
}
finally
{
if (@from != null)
{
@from.Dispose();
}
}
}
/// <summary>
/// Converts a system color to a halcon color as string
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static string ToHalconColor(this Color color)
{
string colorStr = "";
try
{
colorStr = "#" + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2") + color.A.ToString("X2");
}
catch
{ }
return colorStr;
}
/// <summary>
/// Converts a List HObject to a HObject
/// </summary>
/// <param name="listObj"></param>
/// <returns></returns>
public static HObject ListHobject2HObject(this List<HObject> listObj)
{
HObject concatHObject;
HOperatorSet.GenEmptyObj(out concatHObject);
foreach (HObject reg in listObj)
{
HObject ExpTmpOutVar_0;
HOperatorSet.ConcatObj(concatHObject, reg, out ExpTmpOutVar_0);
concatHObject.Dispose();
concatHObject = ExpTmpOutVar_0;
}
return concatHObject;
}
#endregion
}
Halcon C# HObject HTuple 扩展方法
于 2022-12-05 11:09:30 首次发布