/// <summary> /// Finds filpped block according to vector in dictionary and saves it in dictionary with vector of movment so it could be easier to find when searching filpped block of that block. /// </summary> /// <param name="block"></param> /// <param name="referentBlock"></param> /// <param name="image"></param> /// <param name="dictionary"></param> /// <returns></returns> public static PixelBlock?FindFlippedAndAddToVectorDictionary(this PixelBlock block, PixelBlock referentBlock, Image image, IDictionary <PixelBlock, Vector> dictionary) { if (!dictionary.ContainsKey(block)) { return(null); } var vector = dictionary[block]; var flip = new Point(block.Position.X + vector.X, block.Position.Y + vector.Y); if (referentBlock.IsInSearchWindow(flip, MedicalDiamondSearchSettings.SearchParameterP, MedicalDiamondSearchSettings.BlockSize, image.Width, image.Height)) { var pixelBlock = image.Blocks[flip]; if (pixelBlock.Equals(block)) { return(null); } if (!dictionary.ContainsKey(pixelBlock)) { dictionary.Add(pixelBlock, vector); } return(pixelBlock); } return(null); }
/// <summary> /// Medical Diamond Search Algorithm /// </summary> /// <param name="referentBlock"></param> /// <param name="centerBlock"></param> /// <param name="image"></param> /// <returns></returns> private static Vector CalculateVector(PixelBlock referentBlock, PixelBlock centerBlock, Image image) { var diamond = new LargeDiamond(referentBlock, centerBlock, image); var mins = diamond.GetMinimums(); //If first and second minimum are same as referent block return first of them. if (double.IsNaN(mins.Treshold)) { return(new Vector(referentBlock.Position, mins.Minimum.Position)); } if (mins.Treshold > _treshold) { if (diamond.IsCenterBlock(mins.Minimum)) { return(new Vector(referentBlock.Position, new SmallDiamond(referentBlock, diamond.CenterBlock, image).GetMinimums().Minimum.Position)); } var flippedPoint = mins.Minimum.FindFlippedBlock(centerBlock); if (referentBlock.IsInSearchWindow(flippedPoint, MedicalDiamondSearchSettings.SearchParameterP, MedicalDiamondSearchSettings.BlockSize, image.Width, image.Height)) { var flippedBlock = image.Blocks[flippedPoint]; return(flippedBlock.BlockDistortion(referentBlock) < mins.Minimum.BlockDistortion(referentBlock) ? new Vector(referentBlock.Position, flippedPoint) : new Vector(referentBlock.Position, mins.Minimum.Position)); } } #region Get flipped position of minimums var dict = new Dictionary <PixelBlock, Vector>(); var list = new List <PixelBlock> { mins.Minimum, mins.SecondMinimum }; var firstAddValue = mins.Minimum.FindFlippedAndAddToVectorDictionary(centerBlock, referentBlock, image, dict); if (firstAddValue.HasValue) { list.Add(firstAddValue.Value); } var secondAddValue = mins.SecondMinimum.FindFlippedAndAddToVectorDictionary(centerBlock, referentBlock, image, dict); if (secondAddValue.HasValue) { list.Add(secondAddValue.Value); } #endregion for (int i = 0; i < 3; i++) { mins = list.GetMinimums(referentBlock); if (mins.Treshold > _treshold) { return(new Vector(referentBlock.Position, mins.Minimum.Position)); } list = new List <PixelBlock> { mins.Minimum, mins.SecondMinimum }; firstAddValue = mins.Minimum.FindFlippedAndAddToVectorDictionary(referentBlock, image, dict); if (firstAddValue.HasValue) { list.Add(firstAddValue.Value); } secondAddValue = mins.SecondMinimum.FindFlippedAndAddToVectorDictionary(referentBlock, image, dict); if (secondAddValue.HasValue) { list.Add(secondAddValue.Value); } } _treshold += 0.05; return(new Vector(referentBlock.Position, mins.Minimum.Position)); }