目錄
一、邊緣檢查常用方法
二、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 <Enter> 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 <Enter> 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 <Enter> 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 <Enter> 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/437956.html
標籤:AI
上一篇:關于馬爾可夫程序的一些學習筆記
