protected bool SavePointLists(int[][,] pointLists, string name, string ext) { try { string[] enumNames = Enum.GetNames(typeof(DoubleNamingConvention)); string additionalExtension = string.Empty; CultureInfo cultInfo = CultureInfo.InvariantCulture; // check: if (name == null) { return(false); } if (enumNames.Length < pointLists.Length) { _logger?.ErrorLog($"Error during poinlist saving. The number of pointlists is longer the possible enum names", ClassName); return(false); } for (int m = 0; m < pointLists.Length; m++) { if (enumNames.Length == pointLists.Length) { additionalExtension = "_" + enumNames[m]; } string finalOutputName = GeneralImageHandling.CheckOutputDirectoryOfImageSaving(name, "BorderSearch", ext + additionalExtension, ".csv"); using (StreamWriter sw = new StreamWriter(finalOutputName)) { for (int i = 0; i < _borderPoints.Length / 2; i++) { sw.WriteLine($"{i},{_borderPoints[i, 0].ToString(cultInfo)},{_borderPoints[i, 1].ToString(cultInfo)}"); } } } return(true); } catch (Exception ex) { _logger?.ErrorLog($"Exception occured: {ex}", ClassName); return(false); } }
protected virtual bool CheckInputData(Image <Gray, ushort> inputImage, Image <Gray, byte> maskImage, int[,] pointArray, Image <Gray, double> meanVector, Image <Gray, double> stdVector) { try { if (!GeneralImageHandling.CheckImages(inputImage, maskImage, _width, _height, _logger)) { return(false); } if (meanVector == null || stdVector == null || meanVector.Width != inputImage.Height || stdVector.Width != inputImage.Height) { _logger?.ErrorLog($"Error in the meanVector and stdVector length. meanVector height:{meanVector?.Height} stdVector height:{stdVector?.Height} meanVector height:{inputImage.Height}.", ClassName); return(false); } } catch (Exception ex) { _logger?.ErrorLog($"Exception occured: {ex}", ClassName); } return(true); }
protected override bool CalculatePoints(Image <Gray, ushort> origImage, Image <Gray, byte> maskImage, string name, string enumName) { using (VectorOfVectorOfPoint contour = new VectorOfVectorOfPoint()) { try { CvInvoke.FindContours(maskImage, contour, null, RetrType.List, ChainApproxMethod.ChainApproxNone); int verticalCenterLine = maskImage.Width / 2; int magicNumber1 = 2000; for (int i = 0; i < contour.Size; i++) { Point[] _coordinateList = contour[i].ToArray(); if (_coordinateList.Length > 300) { maskImage.SetValue(255.0, maskImage); if (_showImages) { using (var tempImage = new Image <Gray, byte>(maskImage.Size)) { maskImage.CopyTo(tempImage); tempImage.Draw(_coordinateList, new Gray(100.0), 2); ImageViewer.Show(tempImage, "BorderSearcher_Emgu2 - contour points"); GeneralImageHandling.SaveImage(name, _ownFolderNameForSaving, "MaskImage" + enumName, tempImage, _logger); } using (var tempImage = new Image <Gray, ushort>(origImage.Size)) { origImage.CopyTo(tempImage); tempImage.Draw(_coordinateList, new Gray(200.0), 2); ImageViewer.Show(tempImage, "BorderSearcher_Emgu2 - contour points"); GeneralImageHandling.SaveImage(name, _ownFolderNameForSaving, "OrigImage" + enumName, tempImage, _logger); } } for (int j = 0; j < contour[i].Size - 1; j++) { if ((_coordinateList[j].Y != _coordinateList[j + 1].Y) && (Math.Abs(_coordinateList[j].Y - _coordinateList[j + 1].Y) < magicNumber1)) { if (_coordinateList[j].X + _borderSkipSize < verticalCenterLine) { if (_borderPoints[_coordinateList[j].Y, 0] < _coordinateList[j].X + _borderSkipSize) { _borderPoints[_coordinateList[j].Y, 0] = _coordinateList[j].X + _borderSkipSize; } } else if (_coordinateList[j].X - _borderSkipSize > verticalCenterLine) { if (_borderPoints[_coordinateList[j].Y, 1] > _coordinateList[j].X - _borderSkipSize) { _borderPoints[_coordinateList[j].Y, 1] = _coordinateList[j].X - _borderSkipSize; } } } } } } return(true); } catch (Exception ex) { _logger?.ErrorLog($"Exception occured: {ex}.", ClassName); return(false); } } }
public bool Execute(List <NamedData> data, string name) { if (!IsInitialized) { _logger?.ErrorLog("Not initialized yet.", ClassName); return(false); } try { string[] enumNames = Enum.GetNames(typeof(DoubleNamingConvention)); string additionalExtensionName = string.Empty; Image <Gray, ushort>[] rawImages = GetEmguUShortImages("RawImages", data); int imageCounterRaw = rawImages?.Length ?? 0; Image <Gray, byte>[] maskImages = GetEmguByteImages("MaskImages", data); int imageCounterMask = maskImages?.Length ?? 0; if (imageCounterMask != imageCounterRaw || imageCounterRaw > (enumNames?.Length ?? 0)) { _logger?.InfoLog("Input and mask image number OR enumNames number is not the same!", ClassName); return(false); } int[][,] borderPointArrayLists = new int[imageCounterRaw][, ]; for (int m = 0; m < imageCounterRaw; m++) { if (!(GeneralImageHandling.CheckImages(rawImages[m], maskImages[m], _imageWidth, _imageHeight, _logger))) { continue; } if (!(AllocArrays() && ResetPointList())) { _logger?.InfoLog("Borderpoints array re-allocation failed.", ClassName); continue; } if (enumNames.Length == imageCounterRaw) { additionalExtensionName = "_" + enumNames[m]; } CalculatePoints(rawImages[m], maskImages[m], name, additionalExtensionName); borderPointArrayLists[m] = _borderPoints; } data.Add(new BorderPointArraysNamedData(new BorderPointArrays(borderPointArrayLists), "contains the found border points", "BorderPointArrayList")); if (_showImages) { SavePointLists(borderPointArrayLists, name, "PointList"); } } catch (Exception ex) { _logger?.ErrorLog($"Exception during border points calculation: {ex}", ClassName); return(false); } return(true); }
protected override bool CalculatePoints(Image <Gray, ushort> origImage, Image <Gray, byte> maskImage, string name, string enumName) { using (Mat hierarchy = new Mat()) { using (VectorOfVectorOfPoint contour = new VectorOfVectorOfPoint()) { try { CvInvoke.FindContours(maskImage, contour, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxSimple); int verticalCenterLine = maskImage.Width / 2; int magicNumber1 = 2000; for (int i = 0; i < contour.Size; i++) { Point[] _coordinateList = contour[i].ToArray(); if (_coordinateList.Length > 300) { if (_showImages) { using (var tempImage = new Image <Gray, byte>(maskImage.Size)) { maskImage.CopyTo(tempImage); maskImage.SetValue(255.0, maskImage); tempImage.Draw(_coordinateList, new Gray(100.0), 2); ImageViewer.Show(tempImage, "BorderSearcher_Emgu1 - contour points"); //SaveImage(name, tempImage, "MaskImage"); GeneralImageHandling.SaveImage(name, _ownFolderNameForSaving, "MaskImage" + enumName, tempImage, _logger); } using (var tempImage = new Image <Gray, ushort>(origImage.Size)) { origImage.CopyTo(tempImage); tempImage.Draw(_coordinateList, new Gray(200.0), 2); ImageViewer.Show(tempImage, "BorderSearcher_Emgu1 - contour points"); //SaveImage(name, tempImage, "OrigImage"); GeneralImageHandling.SaveImage(name, _ownFolderNameForSaving, "OrigImage" + enumName, tempImage, _logger); } } for (int j = 0; j < contour[i].Size - 1; j++) { if ((_coordinateList[j].Y != _coordinateList[j + 1].Y) && (Math.Abs(_coordinateList[j].Y - _coordinateList[j + 1].Y) < magicNumber1)) { LineSegment2D contourLineSegment = new LineSegment2D(new Point(_coordinateList[j].X, _coordinateList[j].Y), new Point(_coordinateList[j + 1].X, _coordinateList[j + 1].Y)); for (int k = 0; k < Math.Abs(_coordinateList[j + 1].Y - _coordinateList[j].Y); k++) { int difference = _coordinateList[j + 1].Y - _coordinateList[j].Y; int yCoord = _coordinateList[j].Y + k * (difference / Math.Abs(difference)); LineSegment2D horizontalLine = new LineSegment2D(new Point(0, yCoord), new Point(_imageWidth - 1, yCoord)); var resu = GetIntersection(horizontalLine, contourLineSegment); if (resu.X + _borderSkipSize < verticalCenterLine) { if (_borderPoints[yCoord, 0] < resu.X + _borderSkipSize) { _borderPoints[yCoord, 0] = resu.X + _borderSkipSize; } } else if (resu.X - _borderSkipSize > verticalCenterLine) { if (_borderPoints[yCoord, 1] > resu.X - _borderSkipSize) { _borderPoints[yCoord, 1] = resu.X - _borderSkipSize; } } } } } } } return(true); } catch (Exception ex) { _logger?.ErrorLog($"Exception occured: {ex}.", ClassName); return(false); } } } }
/// <summary> /// Makes image transpose and creates mask image /// </summary> /// <param name="inputImage"></param> /// <param name="maskImage"></param> /// <param name="name"></param> /// <returns></returns> public bool Execute(List <NamedData> data, string name) { try { Image <Gray, ushort>[] inputImages = GetEmguUShortImages("RawImages", data); int imageCounter = inputImages?.Length ?? 0; if (imageCounter == 0) { _logger?.InfoLog("No raw image was found dynamicresult!", ClassName); return(false); } Image <Gray, byte>[] maskImages = new Image <Gray, byte> [imageCounter]; for (int m = 0; m < imageCounter; m++) { CvInvoke.Transpose(inputImages[m], inputImages[m]); // calculate historamm for binarythreshold _hist.Calculate <ushort>(new[] { inputImages[0] }, false, null); float thresh; _thresholdcalculator.Execute(_hist, out thresh); if (_showImages) { GeneralImageHandling.SaveHistogram(name, "PreProcessor", "Histogram", _hist, _logger); } double maskValue = 255.0; if (thresh > 255) { thresh /= 16; } // tempimage for thresholding: _tempimage = inputImages[m].Convert <Gray, byte>(); CvInvoke.Threshold(_tempimage, _thresholdedImage, thresh, maskValue, ThresholdType.Binary); _tempimage?.Dispose(); CvInvoke.Erode(_thresholdedImage, _dilatedImage, null, new Point(-1, -1), 3, BorderType.Default, new MCvScalar(0)); CvInvoke.Dilate(_dilatedImage, _thresholdedImage, null, new Point(-1, -1), 4, BorderType.Default, new MCvScalar(0)); maskImages[m] = _thresholdedImage; maskImages[m].Reduce(_reducedMask, ReduceDimension.SingleCol, ReduceType.ReduceAvg); // this section tries to mask out the belts: int count = 0; double MagicThreshold1 = 0.4; // 40% double MagicThreshold2 = 0.18; // 15% for (int i = 0; i < _reducedMask.Height; i++) { if (_reducedMask[i, 0] > (1 - MagicThreshold1) * maskValue) { count++; } } if (count < maskImages[m].Height * (1 - MagicThreshold2)) { Rectangle fullRoi = new Rectangle(0, 0, maskImages[m].Width, maskImages[m].Height); Rectangle rectLeft = new Rectangle(0, _beltCoordinates.LeftBeltStart, maskImages[m].Width, _beltCoordinates.LeftBeltEnd - _beltCoordinates.LeftBeltStart); Rectangle rectRight = new Rectangle(0, _beltCoordinates.RightBeltStart, maskImages[m].Width, _beltCoordinates.RightBeltEnd - _beltCoordinates.RightBeltStart); maskImages[m].ROI = rectLeft; maskImages[m].SetValue(0.0); maskImages[m].ROI = rectRight; maskImages[m].SetValue(0.0); maskImages[m].ROI = fullRoi; // new Rectangle(0, 0, maskImage.Width, maskImage.Height); } if (_showImages) { ImageViewer.Show(inputImages[m], "ImagePreProcessor - transposed image"); ImageViewer.Show(maskImages[m], "ImagePreProcessor - maskImage"); //SaveMaskImage(name, maskImages[m], "MaskImage"); GeneralImageHandling.SaveImage(name, "ImagePreProcessor", "MaskImage", maskImages[m], _logger); } } data.Add(new EmguByteNamedData(maskImages, "Maskimages", "MaskImages")); return(true); } catch (Exception ex) { _logger?.ErrorLog($"Exception occured: {ex}", ClassName); return(false); } }