12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using OpenCvSharp;
- using System;
- namespace ImageOcr
- {
- class Program
- {
- static void Main(string[] args)
- {
- Mat img = new Mat();
- img = Cv2.ImRead("F:/IMG_20200927_150909.jpg");
- Cv2.ImShow("src", img);
- Mat grayImage = new Mat();
- Cv2.CvtColor(img, grayImage, ColorConversionCodes.BGR2GRAY);
- var Width= grayImage.Width;
- var Height = grayImage.Height;
- Mat binaryImage = new Mat();
- Cv2.AdaptiveThreshold(grayImage, binaryImage,255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary,35,10);
- Cv2.ImShow("binaryImage", binaryImage);
- Mat hierarchy = new Mat();
- Cv2.FindContours(binaryImage,out Mat[] contours, hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
- Cv2.DrawContours(img, contours, 0, new Scalar(0,0,255),1);
- Cv2.ImShow("imgray", img);
- foreach (var i in contours) {
- double arclen = Cv2.ArcLength( i, true);
- double epsilon = Math.Max(2, arclen * 0.02);
- Mat approx = new Mat();
- Cv2.ApproxPolyDP(i, approx, epsilon, true);
- double area =Cv2.ContourArea(i);
- Console.WriteLine("面积:" + area);
- RotatedRect rect = Cv2.MinAreaRect(i);
- Console.WriteLine("rect:" + rect);
- Point2f[] re= Cv2.BoxPoints(rect);
- var w = re[1].X;
- var h = re[1].Y;
- var rotion = 0F;
- if (Math.Min(h, w) == 0)
- {
- rotion = 0;
- }
- else {
- rotion = Math.Max(h, w) / Math.Min(h, w);
- }
- var imageArea = Width * Height;
- if(rotion<10 && area>imageArea*0.2 && area< Width* Height * 0.99 && approx==4)
- }
- }
- }
- }
|