目录
一、边缘检查常用方法
二、MIL自带Tools Matrox Edge Finder操作
三、程序逻辑
四、程序代码
一、边缘检查常用方法
MgraAllocList | 分配一个图像列表 |
MedgeAlloc | 分配边缘检查器 |
MedgeAllocResult | 分配边缘检测数据 |
MedgeControl | 控制边缘检查设置 |
MedgeCalculate | 执行边缘提取和特征计算 |
MedgeGetResult | 从边缘结果缓冲区获取边的结果 |
MedgeSelect | 选择边进行计算和结果检索 |
二、MIL自带Tools Matrox Edge Finder操作
1)选择Matrox Edge Finder
2)点击File→OPEN(选择图像)
3)点击File→New Edge Finder→Contour Edge Finder
4)点击Calculate计算,显示边缘
5)在Contour Edge Finder中有Feature Selections,其中有相关参数查看,在本次边缘检查中,需要Moment Elongation参数参考
三、程序逻辑
四、程序代码
1)控制台代码
using System;
using Matrox.MatroxImagingLibrary;
namespace EdgeFindSeals
{
class Program
{
//private const string CONTOUR_IMAGE = "C:/Program Files (x86)/Matrox Imaging/Images/Seals.mim";
private const string CONTOUR_IMAGE = MIL.M_IMAGE_PATH + "Seals.mim";
private const int CONTOUR_MAX_RESULTS = 100;
private const double CONTOUR_MAXIMUM_ELONGATION = 0.8;
private static readonly int CONTOUR_DRAW_COLOR = MIL.M_COLOR_GREEN;
private static readonly int CONTOUR_LABEL_COLOR = MIL.M_COLOR_RED;
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL; //程序标识符
MIL_ID MilSystem = MIL.M_NULL; //系统标识符
MIL_ID MilDisplay = MIL.M_NULL; //显示标识符
MIL_ID MilImage = MIL.M_NULL; //图像缓存区标识符
MIL_ID GraphicList = MIL.M_NULL; //图形列表标识符
MIL_ID MilEdgeContext = MIL.M_NULL; //上下文标识符
MIL_ID MilEdgeResult = MIL.M_NULL; //边缘缓冲区标识符
double EdgeDrawColor = CONTOUR_DRAW_COLOR; //边缘颜色标识符
double LabelDrawColor = CONTOUR_LABEL_COLOR; //标签颜色标识符
MIL_INT NumEdgeFound = 0; //边缘数量标识符
MIL_INT NumResults = 0; //边缘最终数量
int i = 0;
double[] MeanFeretDiameter = new double[CONTOUR_MAX_RESULTS];//平均直径数组
//分配默认应用
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
//读取本地图像于图像缓存区
MIL.MbufRestore(CONTOUR_IMAGE, MilSystem, ref MilImage);
//选择图像缓存区放入显示中
MIL.MdispSelect(MilDisplay, MilImage);
//分配一个图像列表
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, ref GraphicList);
//控制显示设置
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
Console.Write("\nEDGE MODULE:\n");
Console.Write("------------\n\n");
Console.Write("This program determines the outer seal diameters in the displayed image \n");
Console.Write("by detecting and analyzing contours with the Edge Finder module.\n");
Console.Write("Press to continue.\n\n");
Console.ReadKey();
//分配边缘检查器
MIL.MedgeAlloc(MilSystem, MIL.M_CONTOUR, MIL.M_DEFAULT, ref MilEdgeContext);
//分配边缘检测数据于MilEdgeResult
MIL.MedgeAllocResult(MilSystem, MIL.M_DEFAULT, ref MilEdgeResult);
//控制边缘检查的
MIL.MedgeControl(MilEdgeContext, MIL.M_MOMENT_ELONGATION, MIL.M_ENABLE);
//控制边缘检查出的平均直径
MIL.MedgeControl(MilEdgeContext, MIL.M_FERET_MEAN_DIAMETER + MIL.M_SORT1_DOWN, MIL.M_ENABLE);
//使用MilEdgeContext及MilImage缓存数据计算结果于MilEdgeResult
MIL.MedgeCalculate(MilEdgeContext, MilImage, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MilEdgeResult, MIL.M_DEFAULT);
//从MilEdgeResult获得边缘数量并存放于NunEdgeFound
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_NUMBER_OF_CHAINS + MIL.M_TYPE_MIL_INT, ref NumEdgeFound);
//设置边缘前景颜色
MIL.MgraColor(MIL.M_DEFAULT, EdgeDrawColor);
//绘制图形在图像上
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT);
Console.Write("{0} edges were found in the image.\n", NumEdgeFound);
Console.Write("Press to continue.\n\n");
Console.ReadKey();
//指定残缺边缘将其从缓冲区删除
MIL.MedgeSelect(MilEdgeResult, MIL.M_EXCLUDE, MIL.M_MOMENT_ELONGATION, MIL.M_LESS, CONTOUR_MAXIMUM_ELONGATION, MIL.M_NULL);
//指定内边缘将其从缓冲区删除
MIL.MedgeSelect(MilEdgeResult, MIL.M_EXCLUDE, MIL.M_INCLUDED_EDGES, MIL.M_INSIDE_BOX, MIL.M_NULL, MIL.M_NULL);
//将图像缓冲区清除为指定的前景色,或从图形列表中删除图形
MIL.MgraClear(MIL.M_DEFAULT, GraphicList);
//设置边缘前景颜色
MIL.MgraColor(MIL.M_DEFAULT, EdgeDrawColor);
//绘制图形在图像上
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT);
Console.Write("Elongated edges and inner edges of each seal were removed.\n");
Console.Write("Press to continue.\n\n");
Console.ReadKey();
//检索MilEdgeResult边的数量并转化位MIL_INT类型存放于NumResults
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_NUMBER_OF_CHAINS + MIL.M_TYPE_MIL_INT, ref NumResults);
//若NumResults大于1且小于等于CONTOUR_MAX_RESULTS,计算出每个边缘的平均直径
if ((NumResults >= 1) && (NumResults <= CONTOUR_MAX_RESULTS))
{
MIL.MgraColor(MIL.M_DEFAULT, LabelDrawColor);
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_INDEX, MIL.M_DEFAULT, MIL.M_DEFAULT);
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_FERET_MEAN_DIAMETER, MeanFeretDiameter);
Console.Write("Mean diameter of the {0} outer edges are:\n\n", NumResults);
Console.Write("Index Mean diameter \n");
for (i = 0; i < NumResults; i++)
{
Console.Write("{0,-10}{1,-13:0.00}\n", i, MeanFeretDiameter[i]);
}
}
//反之结束
else
{
Console.Write("Edges have not been found or the number of found edges is greater than\n");
Console.Write("the specified maximum number of edges !\n\n");
}
Console.Write("\nPress to end.\n");
Console.ReadKey();
//释放资源
MIL.MgraFree(GraphicList);
MIL.MedgeFree(MilEdgeContext);
MIL.MedgeFree(MilEdgeResult);
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}
2)WinForm代码(待更新)
using System;
using System.Windows.Forms;
using Matrox.MatroxImagingLibrary;
namespace EdgeFindSealsForm
{
public partial class Form1 : Form
{
private const string CONTOUR_IMAGE = MIL.M_IMAGE_PATH + "Seals.mim";
private const int CONTOUR_MAX_RESULTS = 100;
private const double CONTOUR_MAXIMUM_ELONGATION = 0.8;
private static readonly int CONTOUR_DRAW_COLOR = MIL.M_COLOR_GREEN;
private static readonly int CONTOUR_LABEL_COLOR = MIL.M_COLOR_RED;
MIL_ID MilApplication = MIL.M_NULL; //程序标识符
MIL_ID MilSystem = MIL.M_NULL; //系统标识符
MIL_ID MilDisplay = MIL.M_NULL; //显示标识符
MIL_ID MilImage = MIL.M_NULL; //图像缓存区标识符
MIL_ID GraphicList = MIL.M_NULL; //图形列表标识符
MIL_ID MilEdgeContext = MIL.M_NULL; //上下文标识符
MIL_ID MilEdgeResult = MIL.M_NULL; //边缘缓冲区标识符
double EdgeDrawColor = CONTOUR_DRAW_COLOR; //边缘颜色标识符
double LabelDrawColor = CONTOUR_LABEL_COLOR; //标签颜色标识符
MIL_INT NumEdgeFound = 0; //边缘数量标识符
MIL_INT NumResults = 0; //边缘最终数量
int i = 0;
int a = 0;
double[] MeanFeretDiameter = new double[CONTOUR_MAX_RESULTS];//平均直径数组
public Form1()
{
InitializeComponent();
//分配默认应用
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
public void EdgeFind()
{
//读取本地图像于图像缓存区
MIL.MbufRestore(CONTOUR_IMAGE, MilSystem, ref MilImage);
//选择图像缓存区放入显示中
MIL.MdispSelectWindow(MilDisplay, MilImage, this.panel1.Handle);
//图像居中且自适应窗口
MIL.MdispControl(MilDisplay, MIL.M_SCALE_DISPLAY, MIL.M_ENABLE);
//分配一个图像列表
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, ref GraphicList);
//控制显示设置
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
//----------------------------------------------------------------------
//分配边缘检查器
MIL.MedgeAlloc(MilSystem, MIL.M_CONTOUR, MIL.M_DEFAULT, ref MilEdgeContext);
//分配边缘检测数据于MilEdgeResult
MIL.MedgeAllocResult(MilSystem, MIL.M_DEFAULT, ref MilEdgeResult);
//控制边缘检查的
MIL.MedgeControl(MilEdgeContext, MIL.M_MOMENT_ELONGATION, MIL.M_ENABLE);
//控制边缘检查出的平均直径
MIL.MedgeControl(MilEdgeContext, MIL.M_FERET_MEAN_DIAMETER + MIL.M_SORT1_DOWN, MIL.M_ENABLE);
//使用MilEdgeContext及MilImage缓存数据计算结果于MilEdgeResult
MIL.MedgeCalculate(MilEdgeContext, MilImage, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MilEdgeResult, MIL.M_DEFAULT);
//从MilEdgeResult获得边缘数量并存放于NunEdgeFound
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_NUMBER_OF_CHAINS + MIL.M_TYPE_MIL_INT, ref NumEdgeFound);
//设置边缘前景颜色
MIL.MgraColor(MIL.M_DEFAULT, EdgeDrawColor);
//绘制图形在图像上
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT);
//----------------------------------------------------------------------
//指定残缺边缘将其从缓冲区删除
MIL.MedgeSelect(MilEdgeResult, MIL.M_EXCLUDE, MIL.M_MOMENT_ELONGATION, MIL.M_LESS, CONTOUR_MAXIMUM_ELONGATION, MIL.M_NULL);
//指定内边缘将其从缓冲区删除
MIL.MedgeSelect(MilEdgeResult, MIL.M_EXCLUDE, MIL.M_INCLUDED_EDGES, MIL.M_INSIDE_BOX, MIL.M_NULL, MIL.M_NULL);
//将图像缓冲区清除为指定的前景色,或从图形列表中删除图形
MIL.MgraClear(MIL.M_DEFAULT, GraphicList);
//设置边缘前景颜色
MIL.MgraColor(MIL.M_DEFAULT, EdgeDrawColor);
//绘制图形在图像上
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT);
//----------------------------------------------------------------------
//检索MilEdgeResult边的数量并转化位MIL_INT类型存放于NumResults
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_NUMBER_OF_CHAINS + MIL.M_TYPE_MIL_INT, ref NumResults);
//若NumResults大于1且小于等于CONTOUR_MAX_RESULTS,计算出每个边缘的平均直径
if ((NumResults >= 1) && (NumResults <= CONTOUR_MAX_RESULTS))
{
MIL.MgraColor(MIL.M_DEFAULT, LabelDrawColor);
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_INDEX, MIL.M_DEFAULT, MIL.M_DEFAULT);
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_FERET_MEAN_DIAMETER, MeanFeretDiameter);
textBox1.AppendText("Mean diameter of the" + NumResults + "outer edges are:\r\n");
//Console.Write("Mean diameter of the {0} outer edges are:\n\n", NumResults);
textBox1.AppendText("Index Mean diameter \r\n");
//Console.Write("Index Mean diameter \n");
for (i = 0; i < NumResults; i++)
{
double TwoDecimal = double.Parse(MeanFeretDiameter[i].ToString("0.00"));
textBox1.AppendText(i + " " + TwoDecimal + "\r\n");
}
}
//反之结束
else
{
Console.Write("Edges have not been found or the number of found edges is greater than\n");
Console.Write("the specified maximum number of edges !\n\n");
}
}
public void ClearBuffer()
{
//释放资源
MIL.MgraFree(GraphicList);
MIL.MedgeFree(MilEdgeContext);
MIL.MedgeFree(MilEdgeResult);
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
private void From_Close(object sender, EventArgs e)
{
ClearBuffer();
}
private void button1_Click(object sender, EventArgs e)
{
EdgeFind();
button1.Enabled = false;
}
}
}
参考MIL Help