public void AddStepImageReference(ModelStep modelStep, Bitmap referenceBitmap) { modelStep.imageSources.Add(new ModelStep.imageSource()); modelStep.imageSources[modelStep.imageSources.Count - 1].bitmap = referenceBitmap; if (modelStep.imageSources[modelStep.imageSources.Count - 1].bitmap != null) { using (Bitmap bmp = (Bitmap)modelStep.imageSources[modelStep.imageSources.Count - 1].bitmap.Clone()) { string fileNameSave = this.MotherFolder + this.ModelName + @"_Image\" + modelStep.Name + "_" + (modelStep.imageSources.Count - 1) + "_" + modelStep.PBA.ToString() + "_" + modelStep.Func.ToString() + ".png"; Console.WriteLine(fileNameSave); bmp.Save(fileNameSave, ImageFormat.Png); modelStep.imageSources[modelStep.imageSources.Count - 1].ImageName = fileNameSave; } } var options = new JsonSerializerOptions { WriteIndented = true }; if (!Directory.Exists(MotherFolder)) { Directory.CreateDirectory(MotherFolder); } string ModelJson = JsonSerializer.Serialize(this, options); File.WriteAllText(MotherFolder + this.ModelName + ".imdl", ModelJson); }
public void ChangerSpectvalue(ModelStep modelStep, string value) { modelStep.Spect = value; var options = new JsonSerializerOptions { WriteIndented = true }; if (!Directory.Exists(MotherFolder)) { Directory.CreateDirectory(MotherFolder); } string ModelJson = JsonSerializer.Serialize(this, options); File.WriteAllText(MotherFolder + this.ModelName + ".imdl", ModelJson); }
public Bitmap CODT(ModelStep modelStep, Bitmap bitmap) { Console.Write(modelStep.Name + "_" + modelStep.PBA.ToString() + "_" + modelStep.Func.ToString() + ".png :"); var returnBitmap = new Bitmap(10, 10); for (int i = 0; i < modelStep.imageSources.Count; i++) { if (modelStep.imageSources[i].bitmap != null && bitmap != null) { Console.Write(i + " "); returnBitmap = modelStep.imageSources[i].bitmap; Bitmap bitmapSource = resizeImage(modelStep.imageSources[i].bitmap, new Size(100, 100)); Bitmap bitmapArea = resizeImage(bitmap, new Size(100, 100)); Mat matSource = bitmapSource.ToMat(); Mat matArea = bitmapArea.ToMat(); Mat scoreMat = new Mat(); try { Cv2.MatchTemplate(matArea, matSource, scoreMat, TemplateMatchModes.CCoeffNormed); Cv2.MinMaxLoc(scoreMat, out _, out double maxScore); modelStep.Value = maxScore.ToString("f2"); if (Convert.ToDouble(modelStep.Value) >= Convert.ToDouble(modelStep.Spect)) { modelStep.Result = Global.ICOP_tester_OK; break; } else { modelStep.Result = Global.ICOP_tester_NG; } modelStep.sourceImage = matSource.ToBitmap(); modelStep.templateImage = matArea.ToBitmap(); } catch (Exception err) { modelStep.Result = Global.ICOP_tester_NG; modelStep.Value = err.Message; } } } Console.WriteLine(); return(returnBitmap); }
public void QRDT(ModelStep modelStep, Bitmap bitmap) { Mat matImage = new Mat(); Mat outImage = matImage; matImage = bitmap.ToMat(); bitmap.Dispose(); var codeDetector = new BarcodeReader(); Result result = codeDetector.Decode(matImage.ToBitmap()); if (result == null || result.Text.Length != Convert.ToInt32(modelStep.Spect)) { if (matImage.Width > 10 && matImage.Height > 10) { Cv2.CvtColor(matImage, outImage, ColorConversionCodes.RGB2GRAY); Cv2.AdaptiveThreshold(outImage, matImage, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, modelStep.block, modelStep.gama); result = codeDetector.Decode(matImage.ToBitmap()); if (result == null || result.Text.Length != Convert.ToInt32(modelStep.Spect)) { try { if (result == null || result.Text.Length != Convert.ToInt32(modelStep.Spect)) { for (int j = 0; j <= 10; j++) { for (int i = 5; i < 25; i++) { Cv2.AdaptiveThreshold( outImage, matImage, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 2 * i + 1, j ); result = codeDetector.Decode(matImage.ToBitmap()); if (result != null && result.Text.Length == Convert.ToInt32(modelStep.Spect)) { modelStep.block = 2 * i + 1; modelStep.gama = j; break; } } if (result != null && result.Text.Length == Convert.ToInt32(modelStep.Spect)) { break; } } } } catch (Exception e) { MessageBox.Show(e.Message + e.ToString()); } } } } if (result != null && result.Text.Length == Convert.ToInt32(modelStep.Spect)) { modelStep.Result = Global.ICOP_tester_OK; modelStep.Value = result.Text; } else { modelStep.Result = Global.ICOP_tester_NG; modelStep.Value = "QR_not_detect"; } }