public Bitmap SomeMethod(Image<Bgr, byte> img) { using (Image<Gray, byte> gray = img.Convert<Gray, Byte>()) using (Image<Gray, float> sobel = new Image<Gray, float>(img.Size)) { CvInvoke.cvSmooth(gray, gray, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_GAUSSIAN, 5, 5, 25, 25); CvInvoke.cvSobel(gray, sobel, 0, 1, 3); CvInvoke.cvConvert(sobel, gray); // Image<Gray, float> --> Image<Gray, Byte> } return null; }
public LineSegment2D[][] HoughLinesBinary( double rhoResolution, double thetaResolution, int threshold, double minLineWidth, double gapBetweenLines )
public Bitmap SomeMethod(Image<Bgr, byte> img) { LineSegment2D[] lines = null; using (Image<Gray, byte> gray = img.Convert<Gray, Byte>()) using (Image<Gray, float> sobel = new Image<Gray, float>(img.Size)) { CvInvoke.cvSmooth(gray, gray, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_GAUSSIAN, 5, 5, 20, 20); CvInvoke.cvSobel(gray, sobel, 0, 1, 3); CvInvoke.cvConvert(sobel, gray); // Image<Gray, float> --> Image<Gray, Byte> lines = gray.HoughLinesBinary(1, Math.PI / 45, 50, img.Width / 2, 0)[0]; } return null; }
LineSegment2D avr = new LineSegment2D(); foreach (LineSegment2D seg in lines) { avr.P1 = new Point(avr.P1.X + seg.P1.X, avr.P1.Y + seg.P1.Y); avr.P2 = new Point(avr.P2.X + seg.P2.X, avr.P2.Y + seg.P2.Y); } avr.P1 = new Point(avr.P1.X / lines.Length, avr.P1.Y / lines.Length); avr.P2 = new Point(avr.P2.X / lines.Length, avr.P2.Y / lines.Length);
LineSegment2D horizontal = new LineSegment2D(avr.P1, new Point(avr.P2.X, avr.P1.Y));
double c = horizontal.P2.X - horizontal.P1.X; double a = Math.Abs(horizontal.P2.Y - avr.P2.Y); double b = Math.Sqrt(c * c + a * a); angle = (a / b * (180 / Math.PI)) * (horizontal.P2.Y > avr.P2.Y ? 1 : -1);
public Bitmap SomeMethod(Image<Bgr, byte> img) { LineSegment2D[] lines = null; using (Image<Gray, byte> gray = img.Convert<Gray, Byte>()) using (Image<Gray, float> sobel = new Image<Gray, float>(img.Size)) { CvInvoke.cvSmooth(gray, gray, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_GAUSSIAN, 5, 5, 20, 20); CvInvoke.cvSobel(gray, sobel, 0, 1, 3); CvInvoke.cvConvert(sobel, gray); // Image<Gray, float> --> Image<Gray, Byte> lines = gray.HoughLinesBinary(1, Math.PI / 45, 50, img.Width / 2, 0)[0]; } if (lines != null && lines.Length > 0) { double angle = 0; LineSegment2D avr = new LineSegment2D(); foreach (LineSegment2D seg in lines) { avr.P1 = new Point(avr.P1.X + seg.P1.X, avr.P1.Y + seg.P1.Y); avr.P2 = new Point(avr.P2.X + seg.P2.X, avr.P2.Y + seg.P2.Y); img.Draw(seg, new Bgr(255, 0, 0), 1); } avr.P1 = new Point(avr.P1.X / lines.Length, avr.P1.Y / lines.Length); avr.P2 = new Point(avr.P2.X / lines.Length, avr.P2.Y / lines.Length); LineSegment2D horizontal = new LineSegment2D(avr.P1, new Point(avr.P2.X, avr.P1.Y)); img.Draw(new LineSegment2D(avr.P1, new Point(avr.P2.X, avr.P1.Y)), new Bgr(0, 255, 0), 2); img.Draw(avr, new Bgr(0, 255, 0), 2); double c = horizontal.P2.X - horizontal.P1.X; double a = Math.Abs(horizontal.P2.Y - avr.P2.Y); double b = Math.Sqrt(c * c + a * a); angle = (a / b * (180 / Math.PI)) * (horizontal.P2.Y > avr.P2.Y ? 1 : -1); img = img.Rotate(angle, new Bgr(0, 0, 0)); } return img.ToBitmap(); }
Source: https://habr.com/ru/post/263291/
All Articles