public static List <Rectangle> getRectsByColorHsv(Image <Bgr, byte> original, Hsv lower, Hsv higher) { List <Rectangle> rectangles = new List <Rectangle>(); var filtered = ColorFilterer.filterByHsv(original, lower, higher); VectorOfVectorOfPoint contoursDetected = new VectorOfVectorOfPoint(); CvInvoke.FindContours(filtered, contoursDetected, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); VectorOfVectorOfPoint contoursArray = new VectorOfVectorOfPoint(); int count = contoursDetected.Size; for (int i = 0; i < count; i++) { using (VectorOfPoint currContour = contoursDetected[i]) { if (currContour.Size > 50) { Rectangle rect = CvInvoke.BoundingRectangle(currContour); contoursArray.Push(currContour); rectangles.Add(rect); } } } return(rectangles); }
public Image <Bgr, byte> Lab5Process(double area = 100) { var thresh = MainImageExp.Convert <Gray, byte>(); thresh._ThresholdBinaryInv(new Gray(128), new Gray(255)); thresh._Dilate(5); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(thresh, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple); var output = MainImageExp.Convert <Bgr, byte>(); int[,] hierarchy = CvInvoke.FindContourTree(thresh, contours, ChainApproxMethod.ChainApproxNone); for (int i = 0; i < contours.Size; i++) { if (hierarchy[i, 3] == -1) { if (CvInvoke.ContourArea(contours[i], false) > area && CvInvoke.ContourArea(contours[i], false) < 50000) //игнорирование маленьких контуров { Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); RoiList.Add(rect); output.Draw(rect, new Bgr(Color.Blue), 1); } } } MainImageExp = output.Convert <Bgr, byte>(); return(MainImageExp); }
public static List <NeuronBodyMask> GenerateNeuronBodyMasks(List <VectorOfPoint> input) { List <NeuronBodyMask> result = new List <NeuronBodyMask>(); NeuronBodyMask tmp_mask = new NeuronBodyMask(); Rectangle R = new Rectangle(); Image <Gray, Byte> maskImg = new Image <Gray, Byte>(1, 1, new Gray(0)); Point C = new Point(); VectorOfVectorOfPoint tmp_vvop = new VectorOfVectorOfPoint(); MCvMoments moments = new MCvMoments(); for (int i = 0; i < input.Count; i++) { R = CvInvoke.BoundingRectangle(input[i]); maskImg = new Image <Gray, byte>(184, 140, new Gray(0)); tmp_vvop = new VectorOfVectorOfPoint(); tmp_vvop.Push(input[i]); CvInvoke.DrawContours(maskImg, tmp_vvop, -1, new MCvScalar(255), -1, LineType.EightConnected); moments = CvInvoke.Moments(input[i]); C = new Point((int)(moments.M10 / moments.M00), (int)(moments.M01 / moments.M00)); //maskImg.Draw(new Point[] { C }, new Gray(100), 1); tmp_mask = new NeuronBodyMask(R, maskImg, C); result.Add(tmp_mask); } return(result); }
public void PutDescriptions(Image <Bgr, byte> imgInput, VectorOfVectorOfPoint contours, int i, VectorOfPoint approx) { var moments = CvInvoke.Moments(contours[i]); int x = (int)(moments.M10 / moments.M00); int y = (int)(moments.M01 / moments.M00); if (approx.Size == 3) { CvInvoke.PutText(imgInput, "Triangle", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, new MCvScalar(0, 0, 255), 2); } if (approx.Size == 4) { Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); if (Math.Abs(1 - (rect.Width / rect.Height)) < 0.06) { CvInvoke.PutText(imgInput, "Square", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, new MCvScalar(0, 0, 255), 2); } else { CvInvoke.PutText(imgInput, "Rectangle", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, new MCvScalar(0, 0, 255), 2); } } if (approx.Size == 5) { CvInvoke.PutText(imgInput, "Pentagon", new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, new MCvScalar(0, 0, 255), 2); } }
public static Rectangle detect_blue_rectangle(Bitmap b1, Bitmap b2) { Rectangle ret = Rectangle.Empty; Image <Bgr, Byte> img1 = new Image <Bgr, byte>(b1); Image <Bgr, Byte> img2 = new Image <Bgr, byte>(b2); Mat diff = new Mat(); CvInvoke.AbsDiff(img1, img2, diff); Mat tmp = new Mat(); CvInvoke.CvtColor(diff, tmp, ColorConversion.Bgr2Gray); CvInvoke.Threshold(tmp, diff, 0, 255, ThresholdType.Binary | ThresholdType.Otsu); using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint()) { CvInvoke.FindContours(diff, contours, null, RetrType.External, ChainApproxMethod.ChainApproxNone); int count = contours.Size; double m = 0.0; for (int i = 0; i < count; i++) { double d = CvInvoke.ContourArea(contours[i]); if (d > m) { m = d; ret = CvInvoke.BoundingRectangle(contours[i]); } } } Program.logIt(string.Format("detect_blue_rectangle: -- {0}", ret)); return(ret); }
/// <summary> /// Filter the license plate to remove noise /// </summary> /// <param name="plate">The license plate image</param> /// <returns>License plate image without the noise</returns> private static Image <Gray, Byte> FilterPlate(Image <Gray, Byte> plate) { Image <Gray, Byte> thresh = plate.ThresholdBinaryInv(new Gray(120), new Gray(255)); using (Image <Gray, Byte> plateMask = new Image <Gray, byte>(plate.Size)) using (Image <Gray, Byte> plateCanny = plate.Canny(100, 50)) using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint()) { plateMask.SetValue(255.0); CvInvoke.FindContours(plateCanny, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple); int count = contours.Size; for (int i = 1; i < count; i++) { using (VectorOfPoint contour = contours[i]) { Rectangle rect = CvInvoke.BoundingRectangle(contour); if (rect.Height > (plate.Height >> 1)) { rect.X -= 1; rect.Y -= 1; rect.Width += 2; rect.Height += 2; rect.Intersect(plate.ROI); plateMask.Draw(rect, new Gray(0.0), -1); } } } thresh.SetValue(0, plateMask); } thresh._Erode(1); thresh._Dilate(1); return(thresh); }
/// <summary> /// re-order the hulls by x-axis. Then crop characters from the original image in that order. /// </summary> /// <param name="plate_after_preprocessing"></param> /// <param name="contours"></param> /// <returns></returns> private static List <Image <Gray, byte> > ordered_character(Image <Gray, byte> plate_after_preprocessing, List <VectorOfPoint> contours) { List <Image <Gray, byte> > order_character_image = new List <Image <Gray, byte> >(); Dictionary <Rectangle, VectorOfPoint> dict_to_order = new Dictionary <Rectangle, VectorOfPoint>(); for (int i = 0; i < contours.Count; i++) { Rectangle r = CvInvoke.BoundingRectangle(contours[i]); dict_to_order.Add(r, contours[i]); } var ordered_result = dict_to_order.OrderByDescending(i => i.Key.X); List <VectorOfPoint> ordered_contours = new List <VectorOfPoint>(); foreach (KeyValuePair <Rectangle, VectorOfPoint> kvp in ordered_result) { ordered_contours.Add(kvp.Value); plate_after_preprocessing.ROI = kvp.Key; Image <Gray, byte> roi = new Image <Gray, byte>(kvp.Key.Width, kvp.Key.Height); plate_after_preprocessing.CopyTo(roi); order_character_image.Add(roi); plate_after_preprocessing.ROI = Rectangle.Empty; } return(order_character_image); }
public Image <Bgr, byte> ReturnMovingArea2(Mat frame, int minArea) { Image <Gray, byte> cur = frame.ToImage <Gray, byte>(); Image <Bgr, byte> curBgr = frame.ToImage <Bgr, byte>(); if (bg == null) { bg = cur; } Image <Gray, byte> diff = bg.AbsDiff(cur); diff._ThresholdBinary(new Gray(100), new Gray(255)); diff.Erode(3); diff.Dilate(4); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(diff, contours, null, RetrType.External, ChainApproxMethod.ChainApproxTc89L1); var output = curBgr; for (int i = 0; i < contours.Size; i++) { if (CvInvoke.ContourArea(contours[i]) > minArea) //игнорирование маленьких контуров { Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); output.Draw(rect, new Bgr(Color.LawnGreen), 2); } } return(output); }
private (Image <Bgr, byte>, List <Rectangle>) detectTextSegments(Image <Bgr, byte> img, Mat kernel = null) { try { if (kernel == null) { kernel = Mat.Ones(3, 9, DepthType.Cv8U, 1); } var binary = img.Convert <Gray, byte>() .ThresholdBinaryInv(new Gray(240), new Gray(255)) .MorphologyEx(MorphOp.Dilate, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(255)); var temp = img.Clone(); var conours = new VectorOfVectorOfPoint(); var h = new Mat(); CvInvoke.FindContours(binary, conours, h, RetrType.External, ChainApproxMethod.ChainApproxSimple); var bboxes = new List <Rectangle>(); for (int i = 0; i < conours.Size; i++) { var bbox = CvInvoke.BoundingRectangle(conours[i]); bboxes.Add(bbox); CvInvoke.Rectangle(temp, bbox, new MCvScalar(0, 0, 255), 2); } bboxes = bboxes.OrderBy(x => x.Y).ToList(); return(temp, bboxes); } catch (Exception ex) { throw new Exception(ex.Message); } }
public Image <Bgr, byte> Binarization() { var resultImage = sourceImage.CopyBlank(); var grayImage = sourceImage.Convert <Gray, byte>(); grayImage._ThresholdBinaryInv(new Gray(100), new Gray(255)); grayImage._Dilate(5); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(grayImage, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple); var output = sourceImage.Copy(); for (int i = 0; i < contours.Size; i++) { if (CvInvoke.ContourArea(contours[i], false) > 50) //игнорирование маленьких контуров { Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); rois.Add(rect); output.Draw(rect, new Bgr(Color.Blue), 1); } } resultImage = output.Convert <Bgr, byte>(); return(resultImage); }
public Image <Bgr, byte> ReturnTextAreas(Image <Bgr, byte> image) { var resultImage = image.Copy(); var thresh = resultImage.Convert <Gray, byte>(); thresh._ThresholdBinaryInv(new Gray(128), new Gray(255)); thresh._Dilate(5); Mat hierarchy = new Mat(); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(thresh, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple); for (int i = 0; i < contours.Size; i++) { if (CvInvoke.ContourArea(contours[i], false) > 50) //игнорирование маленьких контуров { Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); rois.Add(rect); resultImage.Draw(rect, new Bgr(Color.Blue), 2); } } return(resultImage); }
public List <Rectangle> sort_contours(Emgu.CV.Util.VectorOfVectorOfPoint cnts, string method) { List <Rectangle> BoundingBoxes = new List <Rectangle>(); //get the boxes into a list for (int i = 0; i < cnts.Size; i++) { Emgu.CV.Util.VectorOfPoint contour = cnts[i]; Rectangle BoundingBox = CvInvoke.BoundingRectangle(contour); BoundingBoxes.Add(BoundingBox); } // if no method specified defaut to l to r top to bottom List <Rectangle> SortedBoxes = BoundingBoxes.OrderBy(Rectangle => Rectangle.X).ThenBy(Rectangle => Rectangle.Y).ToList(); if (method == "right-to-left") { SortedBoxes = BoundingBoxes.OrderByDescending(Rectangle => Rectangle.X).ThenBy(Rectangle => Rectangle.Y).ToList(); } else if (method == "top-to-bottom") { SortedBoxes = BoundingBoxes.OrderBy(Rectangle => Rectangle.Y).ThenBy(Rectangle => Rectangle.X).ToList(); } else if (method == "bottom-to-top") { SortedBoxes = BoundingBoxes.OrderByDescending(Rectangle => Rectangle.Y).ThenBy(Rectangle => Rectangle.X).ToList(); } return(SortedBoxes); }
void findContours(Mat threshold, Mat cam) { Mat temp; temp = threshold.Clone();// threshold.copyTo(temp); //these two vectors needed for output of findContours VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hierarchy = new Mat(); //find contours of the filtered image using openCV findContours function CvInvoke.FindContours(temp, contours, hierarchy, Emgu.CV.CvEnum.RetrType.Ccomp, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); for (int i = 0; i < contours.Size; i++) { Mat c = new Mat(); //if the contour is too small, ignore it if (CvInvoke.ContourArea(contours[i]) < 1000) { continue; } //compute the bounding box for the contour, draw it on the frame Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); if (rect.Width > 40 && rect.Height > 40) { CvInvoke.Rectangle(cam, rect, new MCvScalar(0, 0, 255), 2); } } }
/// <summary> /// 画外接矩 /// </summary> private void DrawContourRectangle(ContoursArgs e) { VectorOfPoint cnt = e.Contours[e.ContourIndex]; Rectangle rectangle = CvInvoke.BoundingRectangle(cnt); CvInvoke.Rectangle(mTempImage, rectangle, e.Color, e.Thickness); }
private void btStep3_Click(object sender, EventArgs e) { Mat paper = new Mat(); Image <Gray, Byte> input = inputMat.ToImage <Gray, Byte>(); cropped = new Mat(); VectorOfPointF coners = new VectorOfPointF(); //List<Point> contourPoints; ////find bounding contour //contourPoints = docContours.ToArrayOfArray() // .Where(group => group.Length == docContours.ToArrayOfArray().Max(points => points.Length)) // .SingleOrDefault().ToList(); Rectangle rect = CvInvoke.BoundingRectangle(docContours[0]); Mat quad = new Mat(); quad.Create(answerSheetRealSize.Height, answerSheetRealSize.Width, DepthType.Cv8U, 0); VectorOfPointF quadPts = new VectorOfPointF(); quadPts.Push(new PointF[] { new PointF(0, 0), new PointF(quad.Cols, 0), new PointF(quad.Cols, quad.Rows), new PointF(0, quad.Rows) }); Mat transmat = CvInvoke.GetPerspectiveTransform(docConers, quadPts); CvInvoke.WarpPerspective(grayInput, cropped, transmat, quad.Size); imageResult.Image = cropped; //input.DrawPolyline(contourPoints.ToArray<Point>(), true, new Gray(0), 10); //imageResult.Image = input; }
public DetectorResult VocrColorInformation(Frame frame, int threshold, double gain, int brightnessThreshold, bool show) { Image <Bgr, Byte> img = new Image <Bgr, byte>((Bitmap)frame.Image.Clone()); var imgGray = img.Convert <Gray, Byte>(); var imgSobel = imgGray.Sobel(1, 0, 3).Convert <Gray, Byte>(); var imgRes = new Image <Gray, byte>(imgSobel.Size); CvInvoke.Threshold(imgSobel, imgRes, 0, 255, ThresholdType.Binary | ThresholdType.Otsu); var element = CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(2, 2), new Point(-1, -1)); CvInvoke.Dilate(imgRes, imgRes, element, new Point(0, 0), 2, BorderType.Default, new MCvScalar(0)); CvInvoke.Erode(imgRes, imgRes, element, new Point(0, 0), 16, BorderType.Default, new MCvScalar(0)); DetectorResult detectorResult = new DetectorResult(); using (Mat hierachy = new Mat()) using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint()) { CvInvoke.FindContours(imgRes, contours, hierachy, RetrType.List, ChainApproxMethod.ChainApproxSimple); for (int i = 0; i < contours.Size; i++) { Rectangle rectangle = CvInvoke.BoundingRectangle(contours[i]); var area = rectangle.Width * rectangle.Height; if (area > 1000)//} && rectangle.Width < img.Width * 0.7 && rectangle.Width > rectangle.Height * 1.5) { { detectorResult.Regions.Add(new DetectorRegion(rectangle)); } } } return(detectorResult); }
/// <summary> /// Crop a line of text to the given dimensions /// </summary> private static Image <Gray, byte> cropLineOfText(Image <Gray, byte> image, Size dimension) { VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hier = new Mat(); List <Mat> letters = new List <Mat>(); // Better contouring with inverted image (black background on white text) image._Not(); // Get rid of extra space around the text Mat structuringElement = CvInvoke.GetStructuringElement(ElementShape.Rectangle, dimension, new Point(-1, -1)); Mat dilation = new Mat(); CvInvoke.Dilate(image, dilation, structuringElement, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); CvInvoke.FindContours(dilation, contours, hier, RetrType.External, ChainApproxMethod.ChainApproxNone); // Invert image back to original image._Not(); // Create new image cropped by the new lineBoundry Image <Gray, byte> croppedLine = null; Rectangle lineBoundry = CvInvoke.BoundingRectangle(contours[0]); croppedLine = image.Copy(lineBoundry); return(croppedLine); }
public string SearchText(Image <Bgr, byte> img, int iterations = 251, int thresholdValue = 80, string language = "eng") { VectorOfVectorOfPoint contours = GetContours( GaussianBlur(img, 5).ThresholdBinary(new Gray(thresholdValue), new Gray(255)).Dilate(iterations) ); List <Image <Bgr, byte> > roiImages = new List <Image <Bgr, byte> >(); for (int i = 0; i < contours.Size; i++) { if (CvInvoke.ContourArea(contours[i], false) > 50) { Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); img.ROI = rect; roiImages.Add(img.Copy()); img.ROI = Rectangle.Empty; } } Tesseract ocr = new Tesseract(LANGUAGES_PATH, language, OcrEngineMode.Default); string text = ""; roiImages.ForEach((Image <Bgr, byte> roiImage) => { ocr.SetImage(roiImage); ocr.Recognize(); text += ocr.GetUTF8Text(); }); return(text); }
static void test_blue_color() { Mat img = CvInvoke.Imread(@"C:\test\save_00.jpg", ImreadModes.AnyColor); Mat img1 = new Mat(); CvInvoke.GaussianBlur(img, img1, new Size(11, 11), 0); img1.Save("temp_1.jpg"); Bgr c1 = new Bgr(100, 50, 0); //new Bgr(160, 50, 30); Bgr c2 = new Bgr(250, 220, 100); //new Bgr(250, 200, 100); Bgr c3 = new Bgr(80, 20, 10); //new Bgr(160, 50, 30); Bgr c4 = new Bgr(160, 100, 60); //new Bgr(250, 200, 100); Image <Gray, Byte> g1 = (img1.ToImage <Bgr, Byte>()).InRange(c3, c4); g1.Save("temp_2.jpg"); Rectangle maxR = new Rectangle(0, 0, 60, 60); using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint()) { CvInvoke.FindContours(g1, contours, null, RetrType.External, ChainApproxMethod.ChainApproxNone); int count = contours.Size; for (int i = 0; i < count; i++) { Rectangle r = CvInvoke.BoundingRectangle(contours[i]); if (r.Height * r.Width > maxR.Height * maxR.Width) { maxR = r; } } } Program.logIt(string.Format("{0}", maxR)); if (maxR.X > 0 && maxR.Y > 0) { g1.GetSubRect(maxR).Save("temp_3.jpg"); } }
private PointF GetLocation() { int x = CvInvoke.BoundingRectangle(contour).Right - CvInvoke.BoundingRectangle(contour).Width / 2; int y = CvInvoke.BoundingRectangle(contour).Bottom - CvInvoke.BoundingRectangle(contour).Height / 2; PointF p = new PointF(x, y); Mat usefulMat = new Mat(); if (noteType > 2) { CvInvoke.Erode(blobMat, usefulMat, CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(27, 27), new Point(13, 13)), new Point(1, 1), 1, BorderType.Default, new MCvScalar(1)); CvInvoke.Dilate(usefulMat, usefulMat, CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(27, 27), new Point(13, 13)), new Point(1, 1), 1, BorderType.Default, new MCvScalar(1)); CvInvoke.BitwiseNot(usefulMat, usefulMat); } else { usefulMat = blobMat; } VectorOfKeyPoint keyPoints = new VectorOfKeyPoint(detector.Detect(usefulMat)); for (int i = 0; i < keyPoints.Size; i++) { if (keyPoints[i].Point.Y - width <p.Y && keyPoints[i].Point.Y + width> p.Y) { p.X = keyPoints[i].Point.X; p.Y = keyPoints[i].Point.Y; break; } } return(p); }
public Image <Bgr, byte> FilterMask(Image <Gray, byte> mask, Image <Bgr, byte> image) { var anchor = new Point(-1, -1); var borderValue = new MCvScalar(1); var kernel = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(3, 3), anchor); var closing = mask.MorphologyEx(MorphOp.Close, kernel, anchor, 1, BorderType.Default, borderValue); var opening = closing.MorphologyEx(MorphOp.Open, kernel, anchor, 1, BorderType.Default, borderValue); var dilation = opening.Dilate(7); var threshold = dilation.ThresholdBinary(new Gray(240), new Gray(255)); var copy = image.Copy(); var contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(threshold, contours, null, RetrType.External, ChainApproxMethod.ChainApproxTc89L1); for (int i = 0; i < contours.Size; i++) { if (CvInvoke.ContourArea(contours[i]) > 700) { Rectangle boundingRect = CvInvoke.BoundingRectangle(contours[i]); copy.Draw(boundingRect, new Bgr(Color.GreenYellow), 2); } } return(copy); }
private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { try { if (cc == null) { return; } int label = (int)cc[e.Y, e.X].Intensity; if (label > 0) { var tempImg1 = cc.InRange(new Gray(label), new Gray(label)); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat m = new Mat(); CvInvoke.FindContours(tempImg1, contours, m, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); if (contours.Size > 0) { Rectangle rectBox = CvInvoke.BoundingRectangle(contours[0]); imgInput.ROI = rectBox; var img = imgInput.Copy(); imgInput.ROI = Rectangle.Empty; pictureBox2.Image = img.ToBitmap(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static PadSegmentInfo[] GetPadSegmentInfo(Image <Gray, byte> image, Rectangle ROI) { List <PadSegmentInfo> padinfo = new List <PadSegmentInfo>(); using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint()) { CvInvoke.FindContours(image, contours, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); for (int i = 0; i < contours.Size; i++) { Moments mm = CvInvoke.Moments(contours[i]); if (mm.M00 == 0) { continue; } Point ctCnt = new Point(Convert.ToInt32(mm.M10 / mm.M00), Convert.ToInt32(mm.M01 / mm.M00)); ctCnt.X += ROI.X; ctCnt.Y += ROI.Y; Rectangle bound = CvInvoke.BoundingRectangle(contours[i]); bound.X += ROI.X; bound.Y += ROI.Y; double s = CvInvoke.ContourArea(contours[i]); PadSegmentInfo pad = new PadSegmentInfo(); pad.Contours = contours[i].ToArray(); for (int j = 0; j < pad.Contours.Length; j++) { pad.Contours[j] = new Point(pad.Contours[j].X + ROI.X, pad.Contours[j].Y + ROI.Y); } pad.Bouding = bound; pad.Area = s; pad.Center = ctCnt; padinfo.Add(pad); } } return(padinfo.ToArray()); }
private void btnBox_Click(object sender, EventArgs e) { VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); var tempimg1 = WorkingImg.Clone(); var tempimg2 = WorkingImg.Clone(); CvInvoke.FindContours(tempimg1, contours, null, RetrType.External, ChainApproxMethod.ChainApproxNone); Image <Bgr, byte> colorimg = tempimg2.Convert <Bgr, byte>(); for (int i = 0; i < contours.Size; i++) { CvInvoke.DrawContours(colorimg, contours, i, new MCvScalar(14, 200, 40)); } List <System.Drawing.Rectangle> PassBoxArr = new List <System.Drawing.Rectangle>(); for (int i = 0; i < contours.Size; i++) { System.Drawing.Rectangle rc = CvInvoke.BoundingRectangle(contours[i]); PassBoxArr.Add(rc); } Parallel.For(0, PassBoxArr.Count, i => { colorimg.Draw(PassBoxArr[i], new Bgr(20, 5, 165), 1); }); rtxLog.AppendText("Box" + Environment.NewLine); RegistHisroty(WorkingImg); }
public static Tuple <bool, Rectangle, Bitmap> extrac_context_menu(Bitmap b1, Bitmap b2) { bool retB = false; Rectangle retR = Rectangle.Empty; Bitmap retImg = null; Image <Bgra, byte> img1 = new Image <Bgra, byte>(b1); Image <Bgra, byte> img2 = new Image <Bgra, byte>(b2); Image <Bgra, byte> diff = img2.AbsDiff(img1); //diff.Save("temp_2.jpg"); img1 = diff.PyrDown(); diff = img1.PyrUp(); //img2.Save("temp_2.jpg"); UMat uimage = new UMat(); CvInvoke.CvtColor(diff, uimage, ColorConversion.Bgr2Gray); //uimage.Save("temp_3.jpg"); //MCvScalar mean = new MCvScalar(); //MCvScalar std_dev = new MCvScalar(); //CvInvoke.MeanStdDev(uimage, ref mean, ref std_dev); UMat bimage = new UMat(); //CvInvoke.Threshold(uimage, bimage, mean.V0 + std_dev.V0, 255, ThresholdType.Binary); CvInvoke.Threshold(uimage, bimage, 0, 255, ThresholdType.Binary | ThresholdType.Otsu); //bimage.Save("temp_2.jpg"); Rectangle roi = Rectangle.Empty; using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint()) { CvInvoke.FindContours(bimage, contours, null, RetrType.External, ChainApproxMethod.ChainApproxNone); int count = contours.Size; for (int i = 0; i < count; i++) { double a1 = CvInvoke.ContourArea(contours[i], false); Rectangle r = CvInvoke.BoundingRectangle(contours[i]); //Program.logIt(string.Format("{0}", r)); if (a1 > 500.0) { //Program.logIt(string.Format("{0}", r)); if (roi.IsEmpty) { roi = r; } else { roi = Rectangle.Union(roi, r); } } } } if (!roi.IsEmpty && img2.Width > roi.Width && img2.Height > roi.Height) { //Image<Bgra, byte> cropped = img2.GetSubRect(roi); retB = true; retR = roi; retImg = img2.GetSubRect(retR).Bitmap; //Program.logIt(string.Format("rect={0}", retR)); } return(new Tuple <bool, Rectangle, Bitmap>(retB, retR, retImg)); }
/// <summary> /// Detect scene text from the given image /// </summary> /// <param name="image">The image</param> /// <returns>The detected scene text.</returns> public DetectedObject[] Detect(IInputArray image) { using (VectorOfVectorOfPoint vvp = new VectorOfVectorOfPoint()) using (VectorOfFloat confidents = new VectorOfFloat()) { _textDetector.Detect(image, vvp, confidents); Point[][] detectionResults = vvp.ToArrayOfArray(); float[] confidentResult = confidents.ToArray(); List <DetectedObject> results = new List <DetectedObject>(); for (int i = 0; i < detectionResults.Length; i++) { DetectedObject st = new DetectedObject(); PointF[] detectedPointF = Array.ConvertAll(detectionResults[i], p => new PointF((float)p.X, (float)p.Y)); st.Region = CvInvoke.BoundingRectangle(detectionResults[i]); st.Confident = confidentResult[i]; using (Mat textSubMat = new Mat()) { FourPointsTransform(image, detectedPointF, new Size(100, 32), textSubMat); String text = _ocr.Recognize(textSubMat); st.Label = text; } results.Add(st); } return(results.ToArray()); } }
public Image <Gray, Byte> RescaleImage(Image <Gray, Byte> image) { var contours = new VectorOfVectorOfPoint(); var hierarchy = new Mat(); CvInvoke.FindContours(image, contours, hierarchy, Emgu.CV.CvEnum.RetrType.Tree, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); double largestArea = 0; int index = -1; for (int i = 0; i < contours.Size; i++) { double a = CvInvoke.ContourArea(contours[i], false); if (a > largestArea) { largestArea = a; index = i; } } VectorOfPoint poly = new VectorOfPoint(); CvInvoke.ApproxPolyDP(contours[index], poly, 3, true); Rectangle rect = CvInvoke.BoundingRectangle(poly); image.ROI = rect; var croppedImage = new Image <Gray, Byte>(image.Width, image.Height); var resizedImage = new Image <Gray, Byte>(Width, Height); croppedImage = image.Copy(); CvInvoke.Resize(croppedImage, resizedImage, new Size(Width, Height)); return(resizedImage); }
private async void segmentTextToolStripMenuItem_Click(object sender, EventArgs e) { imgOutput = imgInput.Convert <Gray, byte>().ThresholdBinaryInv(new Gray(50), new Gray(255)); Emgu.CV.Util.VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hierar = new Mat(); CvInvoke.FindContours(imgOutput, contours, hierar, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); show = true; if (contours.Size > 0) { for (int i = 0; i < contours.Size; i++) { //double area = CvInvoke.ContourArea(contours[i]); Rectangle rect = CvInvoke.BoundingRectangle(contours[i]); imgInput.ROI = rect; img = imgInput.Copy().ToBitmap(); imgInput.ROI = Rectangle.Empty; this.Invalidate(); await Task.Delay(1000); } show = false; } }
public static List <Point> FindQuadroPoints(VectorOfPoint Input) { VectorOfVectorOfPoint drawable = new VectorOfVectorOfPoint(); Image <Gray, Byte> img = new Image <Gray, Byte>(184, 140, new Gray(255)); drawable.Push(Input); CvInvoke.DrawContours(img, drawable, -1, new MCvScalar(0), 1, LineType.EightConnected); //поиск точек с четыремя соседями. небольшая оптимизация подсчета благодаря ВB Rectangle BB = CvInvoke.BoundingRectangle(Input); List <Point> quadro = new List <Point>(); for (int y = BB.Y; y < BB.Y + BB.Height; y++) { for (int x = BB.X; x < BB.X + BB.Width; x++) { if ((img[y - 1, x].Intensity == 0) && (img[y + 1, x].Intensity == 0) && (img[y, x - 1].Intensity == 0) && (img[y, x + 1].Intensity == 0)) { quadro.Add(new Point(x, y)); } } } return(quadro); }
private void DisplayBoxes(Image <Bgr, byte> imgInput, VectorOfVectorOfPoint contours) { var list = new List <Rectangle>(); if (contours.Size > 0) { for (int i = 0; i < contours.Size; i++) { var brect = CvInvoke.BoundingRectangle(contours[i]); double ar = brect.Width / brect.Height; if (ar > 2 && brect.Width > 25 && brect.Height > 8 && brect.Height < 100) { list.Add(brect); } } var imgOut = imgInput.CopyBlank(); foreach (var r in list) { CvInvoke.Rectangle(imgInput, r, new MCvScalar(0, 0, 255), 2); CvInvoke.Rectangle(imgOut, r, new MCvScalar(0, 255, 255), -1); } imgOut._And(imgOut); contrastBox.Image = imgOut.Bitmap; outputBox.Image = imgInput.Bitmap; } }