/// <summary> /// Increasing by unit - trapezium /// </summary> /// <param name="bwImg"></param> /// <returns></returns> public static Image <Gray, byte> fitTrapeziumToMask1(Image <Gray, byte> bwImg) { Point c = new Point((int)(bwImg.Width / 2.0), (int)(bwImg.Height - 1)); Trapezium t = new Trapezium(c); var black = bwImg.CopyBlank(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); var aux = black.And(bwImg); while (CvInvoke.CountNonZero(aux) == 0) { t.BaseWidth++; t.Height++; t.LeftTriangle++; t.RightTriangle++; black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); CvInvoke.BitwiseAnd(black, aux, aux); } t.BaseWidth--; t.Height--; t.LeftTriangle--; t.RightTriangle--; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 && (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0) && (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width)) { t.BaseWidth++; black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); } t.BaseWidth--; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 && (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0)) { t.LeftTriangle++; black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); } t.LeftTriangle--; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 && (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width)) { t.RightTriangle++; black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); } t.RightTriangle--; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); while (CvInvoke.CountNonZero(black.And(bwImg)) == 0 && t.Height < black.Height) { t.Height++; black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); } t.Height--; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); return(black); }
/// <summary> /// Increasing by dividing - trapezium /// </summary> /// <param name="bwImg"></param> /// <returns></returns> public static Image <Gray, byte> fitTrapeziumToMask(Image <Gray, byte> bwImg) { Point c = new Point((int)(bwImg.Width / 2.0), (int)(bwImg.Height - 1)); Trapezium t = new Trapezium(c); int inistep = 16; int step = inistep; var black = bwImg.CopyBlank(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); var aux = black.And(bwImg); if (CvInvoke.CountNonZero(aux) != 0) { bwImg._Not(); black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); aux = black.And(bwImg); } while (CvInvoke.CountNonZero(aux) == 0 && (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0) && (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width)) { t.BaseWidth += step; t.Height += step; t.LeftTriangle += step; t.RightTriangle += step; black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); CvInvoke.BitwiseAnd(black, bwImg, aux); } t.BaseWidth -= step; t.Height -= step; t.LeftTriangle -= step; t.RightTriangle -= step; t.BaseWidth = Math.Max(t.BaseWidth, 1); t.Height = Math.Max(t.BaseWidth, 1); t.LeftTriangle = Math.Max(t.BaseWidth, 1); t.RightTriangle = Math.Max(t.BaseWidth, 1); black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); while (step > 0) { if (CvInvoke.CountNonZero(aux) == 0 && (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0) && (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width)) { t.BaseWidth += step; } else { t.BaseWidth -= step; step /= 2; } black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); CvInvoke.BitwiseAnd(black, bwImg, aux); } if (t.BaseWidth < 0) { black.SetZero(); return(black); } while (CvInvoke.CountNonZero(aux) > 0) { t.BaseWidth--; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); CvInvoke.BitwiseAnd(black, bwImg, aux); } step = inistep; while (step > 0) { if (CvInvoke.CountNonZero(aux) == 0 && (c.X - t.BaseWidth / 2 - t.LeftTriangle > 0)) { t.LeftTriangle += step; } else { t.LeftTriangle -= step; step /= 2; } black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); CvInvoke.BitwiseAnd(black, bwImg, aux); } t.LeftTriangle--; step = inistep; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); while (step > 0) { if (CvInvoke.CountNonZero(aux) == 0 && (c.X + t.BaseWidth / 2 + t.RightTriangle < black.Width)) { t.RightTriangle += step; } else { t.RightTriangle -= step; step /= 2; } black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); CvInvoke.BitwiseAnd(black, bwImg, aux); } t.RightTriangle--; step = inistep; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); while (step > 0) { if (CvInvoke.CountNonZero(aux) == 0 && t.Height < black.Height) { t.Height += step; } else { t.Height -= step; step /= 2; } black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); CvInvoke.BitwiseAnd(black, bwImg, aux); } t.Height--; black.SetZero(); black.FillConvexPoly(t.getVertexCoordinates(), new Gray(255)); return(black); }