public int FindPlates() { IplImage labelImg = new IplImage(src.Size, CvBlobLib.DepthLabel, 1); blobs = new CvBlobs(); plaka.Clear(); CvBlobLib.Label(timg, labelImg, blobs); CvBlobLib.FilterByArea(blobs, 600, 10000); IplImage srctemp = src.Clone(); CvBlobLib.RenderBlobs(labelImg, blobs, src, srctemp, RenderBlobsMode.BoundingBox | RenderBlobsMode.Angle); foreach (var item in blobs) { item.Value.SetImageROItoBlob(pimg); // ratio values of plate between 3.5 and 5.4 double ratio = (double)item.Value.Rect.Width / item.Value.Rect.Height; double angle = (double)item.Value.CalcAngle(); if (ratio > 3.5 && ratio < 5.4 && angle > -15 && angle < 15) { // IplImage plakatemp = new IplImage(new CvSize(pimg.ROI.Width, pimg.ROI.Height), pimg.Depth, pimg.NChannels); IplImage plakatemp = new IplImage(new CvSize(140, 27), pimg.Depth, pimg.NChannels); Cv.Resize(pimg, plakatemp); // Cv.Copy(pimg, plakatemp); plaka.Add(plakatemp); src.Rectangle(item.Value.Rect, new CvScalar(0, 0, 255), 2, LineType.Link4); } } // CvBlobLib.RenderBlobs(labelImg, blobs, src, src, RenderBlobsMode.BoundingBox); src.ResetROI(); return(plaka.Count); }
private CvBlobs PreProcessImage2_old(IplImage img) { CvBlobs blobs = null; IplConvKernel element = null; IplImage temp = null; IplImage dest = null; IplImage tmpImage = null; IplImage tmpImage2 = null; IplImage labelImg = null; try { element = Cv.CreateStructuringElementEx(180, 5, 90, 1, ElementShape.Rect, null); tmpImage = new IplImage(img.Size, BitDepth.U8, 1); temp = tmpImage.Clone(); dest = tmpImage.Clone(); img.CvtColor(tmpImage, ColorConversion.RgbaToGray); tmpImage.Rectangle(new CvPoint(0, 0), new CvPoint((Int32)(tmpImage.Size.Width), (Int32)((tmpImage.Size.Height / 9) * 3)), new CvScalar(255, 255, 255), -1); tmpImage.Rectangle(new CvPoint(0, (Int32)((tmpImage.Size.Height / 5) * 4)), new CvPoint((Int32)(tmpImage.Size.Width), (Int32)(tmpImage.Size.Height)), new CvScalar(255, 255, 255), -1); tmpImage.Rectangle(new CvPoint((Int32)((tmpImage.Size.Width / 9) * 7), 0), new CvPoint((Int32)((tmpImage.Size.Width)), (Int32)(tmpImage.Size.Height)), new CvScalar(255, 255, 255), -1); Cv.Smooth(tmpImage, tmpImage, SmoothType.Gaussian); Cv.MorphologyEx(tmpImage, dest, temp, element, MorphologyOperation.TopHat, 1); Cv.Threshold(dest, tmpImage, 128, 255, ThresholdType.Binary | ThresholdType.Otsu); Cv.Smooth(tmpImage, dest, SmoothType.Median); labelImg = new IplImage(img.Size, CvBlobLib.DepthLabel, 1); blobs = new CvBlobs(); tmpImage2 = tmpImage.Clone(); CvBlobLib.Label(tmpImage2, labelImg, blobs); //Cv.ReleaseImage(tmpImage); //tmpImage = img.Clone(); //blobs.RenderBlobs(labelImg, img, tmpImage); //tmpImage.SaveImage(@"c:\temp\newImages\RenderBlobsNOFiltered.png"); CvBlobLib.FilterByArea(blobs, 850, 4850); Cv.ReleaseImage(tmpImage); tmpImage = img.Clone(); //CvTracks tracks = new CvTracks(); //CvBlobLib.UpdateTracks(blobs, tracks, 200.0, 5); //CvBlobLib.RenderTracks(tracks, tmpImage, tmpImage, RenderTracksMode.ID); blobs.RenderBlobs(labelImg, img, tmpImage, RenderBlobsMode.BoundingBox | RenderBlobsMode.Angle); //tmpImage.SaveImage(@"c:\temp\newImages\RenderBlobsFiltered.png"); } finally { if (null != temp) { Cv.ReleaseImage(temp); } if (null != dest) { Cv.ReleaseImage(dest); } if (null != tmpImage) { Cv.ReleaseImage(tmpImage); } if (null != tmpImage2) { Cv.ReleaseImage(tmpImage2); } if (null != labelImg) { Cv.ReleaseImage(labelImg); } } return(blobs); }
private CvBlobs PreProcessImage1(IplImage mainSubImage, IplImage imgGray) { CvBlobs blobs = null; IplImage tmpImage = null; IplImage gray = null; IplImage tgray = null; IplImage labelImg = null; IplImage temp = null; try { tgray = imgGray.Clone(); gray = new IplImage(tgray.Size, tgray.Depth, 1); Cv.Smooth(tgray, tgray, SmoothType.Gaussian); Cv.Canny(tgray, gray, 500, 2, ApertureSize.Size5); temp = gray.Clone(); //IplConvKernel element = Cv.CreateStructuringElementEx(5, 1, 3, 0, ElementShape.Rect, null); IplConvKernel element = Cv.CreateStructuringElementEx(7, 1, 3, 0, ElementShape.Rect, null); Cv.MorphologyEx(gray, gray, temp, element, MorphologyOperation.BlackHat, 1); Cv.Threshold(gray, gray, 100, 255, ThresholdType.Binary | ThresholdType.Otsu); Cv.Smooth(gray, gray, SmoothType.Gaussian); labelImg = new IplImage(mainSubImage.Size, CvBlobLib.DepthLabel, 1); blobs = new CvBlobs(); CvBlobLib.Label(gray, labelImg, blobs); CvBlobLib.FilterByArea(blobs, 1550, 4850); tmpImage = mainSubImage.Clone(); //CvTracks tracks = new CvTracks(); //CvBlobLib.UpdateTracks(blobs, tracks, 200.0, 5); //CvBlobLib.RenderTracks(tracks, tmpImage, tmpImage, RenderTracksMode.ID); blobs.RenderBlobs(labelImg, mainSubImage, tmpImage, RenderBlobsMode.BoundingBox | RenderBlobsMode.Angle); /* * img.SetROI(subImageRect); * Cv.Copy(tmpImage, img); * img.ResetROI(); * Cv.ReleaseImage(tmpImage); * */ } finally { if (null != temp) { Cv.ReleaseImage(temp); } if (null != tgray) { Cv.ReleaseImage(tgray); } if (null != gray) { Cv.ReleaseImage(gray); } if (null != labelImg) { Cv.ReleaseImage(labelImg); } if (null != tmpImage) { Cv.ReleaseImage(tmpImage); } } return(blobs); }