網(wǎng)站維護(hù)優(yōu)化網(wǎng)絡(luò)推廣和信息流優(yōu)化一樣么
一、效果展示
二、上面是原展開工具CogPolarUnwrapTool;
第二種方法: 用Blob 和 CogCopyRegionTool
?
三、 用預(yù)處理工具? 加減常數(shù),讓圖片變得更亮點(diǎn)
四、圓展開工具
?
五、模板匹配
六、代碼分解
1.創(chuàng)建集合和文子顯示工具
CogGraphicCollection dt = new CogGraphicCollection();CogGraphicLabel Label;
2.加載工具,并實(shí)例化一個List集合
dt.Clear();CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"]as CogPolarUnwrapTool;CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;List<double> listX = new List<double>();
3.定義圖標(biāo)中的坐標(biāo)嗎,添加集合,排序
double total = 0;double x, y;for(int i = 0;i < pma.Results.Count;i++){listX.Add(pma.Results[i].GetPose().TranslationX);total += pma.Results[i].GetPose().TranslationY;}y = total / pma.Results.Count;listX.Sort();
4.進(jìn)行傳參,找出之前的x和y坐標(biāo)系,并用參數(shù)接收是否存在缺失
int a = 0;for(int i = 0;i < listX.Count - 1;i++){if((listX[i + 1] - listX[i]) > 50){x = (listX[i + 1] + listX[i]) / 2;y = total / pma.Results.Count;double lastx,lasty;polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out lastx, out lasty);dt.Add(Create(lastx, lasty));a++;}}
5.進(jìn)行判斷是否缺失
Label = new CogGraphicLabel();if(a > 0){Label.SetXYText(100, 100, "缺失"+a);Label.Color = CogColorConstants.Red;Label.Font = new Font("楷體", 30);dt.Add(Label);}else{Label.SetXYText(100, 100, "OK");Label.Color = CogColorConstants.Green;Label.Font = new Font("楷體", 30);dt.Add(Label);}
6.將上面?zhèn)魅雸A展開的x,y坐標(biāo),進(jìn)行創(chuàng)建圓
private CogCircle Create(double x, double y){CogCircle co = new CogCircle();co.CenterX = x;co.CenterY = y;co.Radius = 20;co.LineWidthInScreenPixels = 6;co.Color = CogColorConstants.Red;dt.Add(co);return co;}
7.輸出
for(int i = 0;i < dt.Count;i++){mToolBlock.AddGraphicToRunRecord(dt[i], lastRecord, "CogIPOneImageTool1.InputImage", "");}
8.代碼All
#region namespace imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.PMAlign;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregionCogGraphicCollection dt = new CogGraphicCollection();CogGraphicLabel Label;/// <summary>/// Called when the parent tool is run./// Add code here to customize or replace the normal run behavior./// </summary>/// <param name="message">Sets the Message in the tool's RunStatus.</param>/// <param name="result">Sets the Result in the tool's RunStatus</param>/// <returns>True if the tool should run normally,/// False if GroupRun customizes run behavior</returns>public override bool GroupRun(ref string message, ref CogToolResultConstants result){// To let the execution stop in this script when a debugger is attached, uncomment the following lines.// #if DEBUG// if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();// #endifdt.Clear();CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"]as CogPolarUnwrapTool;CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;List<double> listX = new List<double>();// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);double total = 0;double x, y;for(int i = 0;i < pma.Results.Count;i++){listX.Add(pma.Results[i].GetPose().TranslationX);total += pma.Results[i].GetPose().TranslationY;}y = total / pma.Results.Count;listX.Sort();int a = 0;for(int i = 0;i < listX.Count - 1;i++){if((listX[i + 1] - listX[i]) > 50){x = (listX[i + 1] + listX[i]) / 2;y = total / pma.Results.Count;double lastx,lasty;polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out lastx, out lasty);dt.Add(Create(lastx, lasty));a++;}}Label = new CogGraphicLabel();if(a > 0){Label.SetXYText(100, 100, "缺失"+a);Label.Color = CogColorConstants.Red;Label.Font = new Font("楷體", 30);dt.Add(Label);}else{Label.SetXYText(100, 100, "OK");Label.Color = CogColorConstants.Green;Label.Font = new Font("楷體", 30);dt.Add(Label);}return false;}private CogCircle Create(double x, double y){CogCircle co = new CogCircle();co.CenterX = x;co.CenterY = y;co.Radius = 20;co.LineWidthInScreenPixels = 6;co.Color = CogColorConstants.Red;dt.Add(co);return co;}#region When the Current Run Record is Created/// <summary>/// Called when the current record may have changed and is being reconstructed/// </summary>/// <param name="currentRecord">/// The new currentRecord is available to be initialized or customized.</param>public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord){}#endregion#region When the Last Run Record is Created/// <summary>/// Called when the last run record may have changed and is being reconstructed/// </summary>/// <param name="lastRecord">/// The new last run record is available to be initialized or customized.</param>public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){for(int i = 0;i < dt.Count;i++){mToolBlock.AddGraphicToRunRecord(dt[i], lastRecord, "CogIPOneImageTool1.InputImage", "");}}#endregion#region When the Script is Initialized/// <summary>/// Perform any initialization required by your script here/// </summary>/// <param name="host">The host tool</param>public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host){// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}
?
七、polar.RunParams.GetInputPointFromOutputPoint
該方法用于輸出圖像中的坐標(biāo)?(x, y)
?轉(zhuǎn)換為輸入圖像中的坐標(biāo)?(lastx, lasty)
polar.InputImage
?是輸入圖像對象polar.Region
?可能是指定的處理區(qū)域x, y
?是輸出圖像中的坐標(biāo)out lastx, out lasty
?是輸出參數(shù),表示轉(zhuǎn)換后的輸入圖像中的坐標(biāo)。
???????polar.RunParams.GetInputPointFromOutputPoint(
-
polar.InputImage, // 輸入圖像
-
polar.Region, // 處理區(qū)域
-
x, // 輸出圖像中的X坐標(biāo)
-
y, // 輸出圖像中的Y坐標(biāo)
-
out lastx, // 輸出:對應(yīng)的輸入圖像中的X坐標(biāo)
-
out lasty // 輸出:對應(yīng)的輸入圖像中的Y坐標(biāo) );
???????注意事項(xiàng):
- 方法的作用:該方法的主要作用是將輸出圖像中的坐標(biāo)映射回輸入圖像中的坐標(biāo),通常用于圖像變換后的逆向坐標(biāo)查找。
- 參數(shù)檢查:確保?
polar.InputImage
?和?polar.Region
?已正確初始化,并且?x, y
?坐標(biāo)在有效的范圍內(nèi)。 - 返回值:
lastx
?和?lasty
?是通過?out
?參數(shù)返回的,因此調(diào)用后可以直接使用這兩個變量獲取轉(zhuǎn)換后的坐標(biāo)。
八、第二種方法
1.定義值
2. 工具展示
3. Blob設(shè)置
4.CogCopyReigonTool設(shè)置
添加終端鏈接
?

?
?5.CogBlob覆蓋后設(shè)置
6.代碼
#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Blob;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregionCogGraphicLabel label = new CogGraphicLabel();/// <summary>/// Called when the parent tool is run./// Add code here to customize or replace the normal run behavior./// </summary>/// <param name="message">Sets the Message in the tool's RunStatus.</param>/// <param name="result">Sets the Result in the tool's RunStatus</param>/// <returns>True if the tool should run normally,/// False if GroupRun customizes run behavior</returns>public override bool GroupRun(ref string message, ref CogToolResultConstants result){// To let the execution stop in this script when a debugger is attached, uncomment the following lines.// #if DEBUG// if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();// #endifint count = (int) mToolBlock.Inputs["Count"].Value;CogBlobTool blob = mToolBlock.Tools["CogBlobTool2"]as CogBlobTool;// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);int currentCount = blob.Results.GetBlobs().Count;int diff = count - currentCount;string res = "";label.Color = CogColorConstants.Green;label.Font = new Font("宋體", 20);if (diff > 0){res = "缺失:" + diff;label.Color = CogColorConstants.Red;}else{res = "良品";}label.SetXYText(100, 250, res);return false;}#region When the Current Run Record is Created/// <summary>/// Called when the current record may have changed and is being reconstructed/// </summary>/// <param name="currentRecord">/// The new currentRecord is available to be initialized or customized.</param>public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord){}#endregion#region When the Last Run Record is Created/// <summary>/// Called when the last run record may have changed and is being reconstructed/// </summary>/// <param name="lastRecord">/// The new last run record is available to be initialized or customized.</param>public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogIPOneImageTool1.InputImage", "Script");}#endregion#region When the Script is Initialized/// <summary>/// Perform any initialization required by your script here/// </summary>/// <param name="host">The host tool</param>public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host){// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}