private static void HKCameraCltr_HardTriggerEvent(object sender, HObject e) { string camName = (string)sender; HObject inImage = e; SoftwareOnceEvent?.Invoke(null, new OutImage(camName, inImage)); foreach (var testItem in UseTestItems) { if (testItem.camName == camName) { testItem.Find(inImage); testItem.Show(inImage, out HObject resultImage); OutResult outResult = new OutResult() { TestItemName = testItem.name, CamName = testItem.camName, IsOK = testItem.isOK, ResultMessage = testItem.outMessage, OriginalImage = new OutImage(testItem.camName, inImage), EffectImage = new OutImage(testItem.camName, resultImage) }; DetectionOnceEvent?.Invoke(null, outResult); } } inImage.Dispose(); }
/// <summary> /// 触发所有相机拍照 /// </summary> /// <param name="imageType">存储与传输图片的格式</param> /// <returns></returns> public static bool TriggerAllCamera(ImageType imageType = ImageType.HObject) { if (!MyRun.havInit) { StrErrorMsg = "视觉模块未初始化"; return(false); } lock (obj) { if (UseCamsName.Count == 0) { StrErrorMsg = "未选择模板"; return(false); } foreach (var camName in UseCamsName.ToArray()) { if (imageType == ImageType.HObject) { MyRun.TriggerCamera(camName, out HObject outImage); if (outImage != null) { SoftwareOnceEvent?.Invoke(null, new OutImage(camName, outImage)); outImage.Dispose(); } else { StrErrorMsg = MyRun.StrErrorMsg; return(false); } } if (imageType == ImageType.Bitmap) { MyRun.TriggerCamera(camName, out Bitmap outImage); if (outImage != null) { SoftwareOnceEvent?.Invoke(null, new OutImage(camName, outImage)); outImage.Dispose(); } else { StrErrorMsg = MyRun.StrErrorMsg; return(false); } } } return(true); } }
/// <summary> /// 触发检测,更具当前切换的型号模板进行拍照识别 /// 该方法会开辟新线程 /// 该方法不会返回识别结果,识别结果需要在调用WaitDetectionResult获取 /// 同一时刻只能有一个TriggerDetection在运行 /// </summary> /// <returns></returns> public static bool TriggerDetection() { if (!MyRun.havInit) { StrErrorMsg = "视觉模块未初始化"; return(false); } if (DetectionThread != null && DetectionThread.IsAlive) { StrErrorMsg = "上一次拍照识别并未结束"; return(false); } DetectionThread = new Thread(() => { //防止之前暂停的信号影响到这次检测 stopDetectionSignal.Reset(); OutResults.Clear(); ConcurrentDictionary <string, HObject> camImages = new ConcurrentDictionary <string, HObject>(); ConcurrentDictionary <string, HObject> matchImages = new ConcurrentDictionary <string, HObject>(); ConcurrentDictionary <string, HTuple> matchHomMat2D = new ConcurrentDictionary <string, HTuple>(); bool hav = true; while (true) { Parallel.ForEach(UseCamsName.ToArray(), camName => { if (MyRun.TriggerCamera(camName, out HObject camImage)) { if (camImage != null) { //待升级:这里添加对图片质量的判断,如果图片模糊,撕裂则将图片丢弃 camImages.AddOrUpdate(camName, camImage, (k, v) => v = camImage); } } else { StrErrorMsg = MyRun.StrErrorMsg; } }); //待升级:这里添加对产品是否到位的判断,通过特征检测的方式实现,不符合要求的则将图片丢弃 //待升级:这里添加模板匹配矫正图片的功能,检测项检测时使用的是矫正后的图片 //要将矫正的变换参数保存,界面显示的需要是把ROI区域等检测效果进行矫正的图片 Parallel.ForEach(UseMatchingFun.ToArray(), matchingFun => { if (!matchingFun.matching.Name.Equals("无模板定位") && camImages.TryGetValue(matchingFun.matching.CamName, out HObject camImage)) { int nRet = matchingFun.Find(camImage, out HObject matchImage); if (nRet == 0) { matchImages.AddOrUpdate(matchingFun.matching.Name, matchImage, (k, v) => v = matchImage); matchHomMat2D.AddOrUpdate(matchingFun.matching.Name, matchingFun.homMat2D, (k, v) => v = matchingFun.homMat2D); } SoftwareOnceEvent?.Invoke(null, new OutImage(matchingFun.matching.CamName, camImage)); } }); Parallel.ForEach(UseTestItems.ToArray(), testItem => { if (testItem.hav) { return; } HObject inImage = null; if (testItem.matchName.Equals("无模板定位")) { if (camImages.TryGetValue(testItem.camName, out inImage)) { testItem.Find(inImage); testItem.Show(inImage, out HObject resultImage); testItem.hav = true; OutResult outResult = new OutResult() { TestItemName = testItem.name, CamName = testItem.camName, IsOK = testItem.isOK, ResultMessage = testItem.outMessage, OriginalImage = new OutImage(testItem.camName, inImage), EffectImage = new OutImage(testItem.camName, resultImage) }; DetectionOnceEvent?.Invoke(null, outResult); OutResults.Add(outResult); } } else { if (matchImages.TryGetValue(testItem.matchName, out inImage)) { testItem.Find(inImage); camImages.TryGetValue(testItem.camName, out inImage); testItem.Show(inImage, matchHomMat2D[testItem.matchName], out HObject resultImage); testItem.hav = true; OutResult outResult = new OutResult() { TestItemName = testItem.name, CamName = testItem.camName, IsOK = testItem.isOK, ResultMessage = testItem.outMessage, OriginalImage = new OutImage(testItem.camName, inImage), EffectImage = new OutImage(testItem.camName, resultImage) }; DetectionOnceEvent?.Invoke(null, outResult); OutResults.Add(outResult); } } }); foreach (var camImage in camImages.Values) { camImage.Dispose(); } hav = true; foreach (var testItem in UseTestItems.ToArray()) { hav &= testItem.hav; } if (hav) { foreach (var testItem in UseTestItems.ToArray()) { testItem.hav = false; } break; } if (stopDetectionSignal.WaitOne(20)) { break; } Thread.Sleep(20); } }) { IsBackground = true }; DetectionThread.Start(); return(true); }