static IEnumerable <IplImage> SliceCols(IplImage input, int count, int skip) { for (int i = 0; i <= input.Width - skip; i += skip) { var rectangle = new Rect(i, 0, count, input.Height); yield return(input.GetSubRect(rectangle)); } }
public IObservable <IplImage> Process(IObservable <IplImage> source) { return(source.Select(input => { var offset = Offset; var copyOffset = new Point(Math.Max(0, offset.X), Math.Max(0, offset.Y)); var size = new Size(input.Width + Math.Abs(offset.X), input.Height + Math.Abs(offset.Y)); Arr output = new IplImage(size, input.Depth, input.Channels); CV.CopyMakeBorder(input, output, copyOffset, BorderType, FillValue); output = output.GetSubRect(new Rect( Math.Max(0, -offset.X), Math.Max(0, -offset.Y), input.Width, input.Height)); return output.GetImage(); })); }
public static void DrawConnectedComponent(IplImage image, ConnectedComponent component, Point2f offset) { if (component.Area <= 0) { return; } var centroid = component.Centroid + offset; var orientation = component.Orientation; var minorAxisOrientation = orientation + Math.PI / 2.0; var halfMajorAxis = component.MajorAxisLength * 0.5; var halfMinorAxis = component.MinorAxisLength * 0.5; var major1 = new Point((int)(centroid.X + halfMajorAxis * Math.Cos(orientation)), (int)(centroid.Y + halfMajorAxis * Math.Sin(orientation))); var major2 = new Point((int)(centroid.X - halfMajorAxis * Math.Cos(orientation)), (int)(centroid.Y - halfMajorAxis * Math.Sin(orientation))); var minor1 = new Point((int)(centroid.X + halfMinorAxis * Math.Cos(minorAxisOrientation)), (int)(centroid.Y + halfMinorAxis * Math.Sin(minorAxisOrientation))); var minor2 = new Point((int)(centroid.X - halfMinorAxis * Math.Cos(minorAxisOrientation)), (int)(centroid.Y - halfMinorAxis * Math.Sin(minorAxisOrientation))); if (component.Patch != null) { var target = image; var patch = component.Patch; var mask = patch.Channels == 1 ? patch : null; try { if (component.Contour != null) { var rect = component.Contour.Rect; mask = new IplImage(patch.Size, patch.Depth, 1); mask.SetZero(); CV.DrawContours(mask, component.Contour, Scalar.All(255), Scalar.All(0), 0, -1, LineFlags.Connected8, new Point(-rect.X, -rect.Y)); if (image.Width != rect.Width || image.Height != rect.Height) { target = image.GetSubRect(component.Contour.Rect); } } if (patch.Channels != target.Channels) { var conversion = patch.Channels > image.Channels ? ColorConversion.Bgr2Gray : ColorConversion.Gray2Bgr; patch = new IplImage(patch.Size, patch.Depth, image.Channels); CV.CvtColor(component.Patch, patch, conversion); } CV.Copy(patch, target, mask); } finally { if (patch != component.Patch) { patch.Dispose(); } if (mask != component.Patch) { mask.Dispose(); } if (target != image) { target.Dispose(); } } } else if (component.Contour != null) { CV.DrawContours(image, component.Contour, Scalar.All(255), Scalar.All(0), 0, -1, LineFlags.Connected8, new Point(offset)); } if (component.Contour != null) { CV.DrawContours(image, component.Contour, Scalar.Rgb(255, 0, 0), Scalar.Rgb(0, 0, 255), 0, 1, LineFlags.Connected8, new Point(offset)); } CV.Line(image, major1, major2, Scalar.Rgb(0, 0, 255)); CV.Line(image, minor1, minor2, Scalar.Rgb(255, 0, 0)); CV.Circle(image, new Point(centroid), 2, Scalar.Rgb(255, 0, 0), -1); }