public Button RecognizeOneButton(Bitmap source, Rectangle rect, string filePath, int id) { var seq = new FiltersSequence(); var card = new Button(source); //Create Card Object card.Rect = rect; seq.Clear(); seq.Add(Grayscale.CommonAlgorithms.BT709); seq.Add(new OtsuThreshold()); source = seq.Apply(source); card.Tip = ScanButtonTip(source); //Scan Rank of non-face card //if (card.Tip == ButtonTip.NOT_RECOGNIZED) //{ // if (!string.IsNullOrEmpty(filePath)) // { // while (File.Exists(filePath + id + ".bmp")) // id++; // top.Save(filePath + id + ".bmp", ImageFormat.Bmp); // } //} return(card); }
private void cmdDelFilter_Click(object sender, EventArgs e) { if (lsvFilterStack.SelectedItems.Count > 0) { var item = lsvFilterStack.SelectedItems[0]; (item.Tag as frmFilterSettings).Dispose(); item.Tag = null; lsvFilterStack.Items.Remove(item); filterStack.Clear(); foreach (ListViewItem lsvItem in lsvFilterStack.Items) { var filter = lsvItem.Tag as frmFilterSettings; filterStack.Add(filter.FilterToModify); } item = null; } this.Redraw(); }
/// <summary> /// Detects and recognizes cards from source image /// </summary> /// <param name="source">Source image to be scanned</param> /// <returns>Recognized Cards</returns> public CardCollection Recognize(Bitmap source) { CardCollection collection = new CardCollection(); //Collection that will hold cards Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image FiltersSequence seq = new FiltersSequence(); seq.Add(Grayscale.CommonAlgorithms.BT709); //First add grayScaling filter seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter temp = seq.Apply(source); // Apply filters on source image //Extract blobs from image whose size width and height larger than 150 BlobCounter extractor = new BlobCounter(); extractor.FilterBlobs = true; extractor.MinWidth = extractor.MinHeight = 150; extractor.MaxWidth = extractor.MaxHeight = 350; extractor.ProcessImage(temp); //Will be used transform(extract) cards on source image QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation(); //Will be used resize(scaling) cards ResizeBilinear resizer = new ResizeBilinear(CardWidth, CardHeight); foreach (Blob blob in extractor.GetObjectsInformation()) { //Get Edge points of card List <IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob); //Calculate/Find corners of card on source image from edge points List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints); quadTransformer.SourceQuadrilateral = corners; //Set corners for transforming card quadTransformer.AutomaticSizeCalculaton = true; Bitmap cardImg = quadTransformer.Apply(source); //Extract(transform) card image if (cardImg.Width > cardImg.Height) //If card is positioned horizontally { cardImg.RotateFlip(RotateFlipType.Rotate90FlipNone); //Rotate } cardImg = resizer.Apply(cardImg); //Normalize card size Card card = new Card(cardImg, corners.ToArray()); //Create Card Object char color = ScanColor(card.GetTopLeftPart()); //Scan color bool faceCard = IsFaceCard(cardImg); //Determine type of card(face or not) if (!faceCard) { card.Suit = ScanSuit(cardImg, color); //Scan Suit of non-face card card.Rank = ScanRank(cardImg); //Scan Rank of non-face card } else { Bitmap topLeft = card.GetTopLeftPart(); seq.Clear(); seq.Add(Grayscale.CommonAlgorithms.BT709); seq.Add(new BradleyLocalThresholding()); topLeft = seq.Apply(topLeft); BlobsFiltering bFilter = new BlobsFiltering(5, 5, 150, 150); bFilter.ApplyInPlace(topLeft); //Filter blobs that can not be a suit topLeft.Save("topleft.bmp", ImageFormat.Bmp); card.Suit = ScanFaceSuit(topLeft, color); //Scan suit of face card card.Rank = ScanFaceRank(topLeft); //Scan rank of face card } collection.Add(card); //Add card to collection } return(collection); }
private void FillPictureBoxes(ref Bitmap image) { Bitmap tmpImg = image; Bitmap tmpImg2 = image; try { bool hasFilter = false; //setup resize and filtersequesce //resize img to fit picturebox ResizeBicubic resizeFilter = new ResizeBicubic(0, 0); resizeFilter = new ResizeBicubic(pbCapture.Width, pbCapture.Height); tmpImg = resizeFilter.Apply(tmpImg); resizeFilter = new ResizeBicubic(pbShapes.Width, pbShapes.Height); tmpImg2 = resizeFilter.Apply(tmpImg2); FiltersSequence processingFilter = new FiltersSequence(); //List all filters IFilter ConservativeSmoothingFilter = new AForge.Imaging.Filters.ConservativeSmoothing(); IFilter InvertFilter = new AForge.Imaging.Filters.Invert(); IFilter HSLFilteringFilter = new AForge.Imaging.Filters.HSLFiltering(); IFilter SepiaFilter = new AForge.Imaging.Filters.Sepia(); IFilter grayscaleFilter = new AForge.Imaging.Filters.GrayscaleBT709(); IFilter SkeletonizationFilter = new AForge.Imaging.Filters.SimpleSkeletonization(); IFilter pixFilter = new AForge.Imaging.Filters.Pixellate(); ////apply filter and process img--------------------------------------------- if (ConservativeSmoothing) { processingFilter.Add(ConservativeSmoothingFilter); hasFilter = true; } if (Invert) { processingFilter.Add(InvertFilter); hasFilter = true; } if (HSLswitch) { processingFilter.Add(HSLFilteringFilter); hasFilter = true; } if (sepiaSwitch) { processingFilter.Add(SepiaFilter); hasFilter = true; } if (Skeletonization) { processingFilter.Add(grayscaleFilter); processingFilter.Add(SkeletonizationFilter); hasFilter = true; } //apply the filter(s) to image if (hasFilter) { //tmpImg = processingFilter.Apply(tmpImg); tmpImg2 = processingFilter.Apply(tmpImg2); } processingFilter.Clear(); if (bwSwitch) { switchBandW(ref tmpImg); } if (CannyEdgeDetector) { // create filter CannyEdgeDetector filter = new CannyEdgeDetector(); // apply the filter tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg); filter.ApplyInPlace(tmpImg); // image = DrawFocusArea(gsImage); } else { // image = DrawFocusArea(image); } if (DifferenceEdgeDetector) { DifferenceEdgeDetector dFilter = new DifferenceEdgeDetector(); // apply the filter tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg); dFilter.ApplyInPlace(tmpImg); } if (HomogenityEdgeDetector) { HomogenityEdgeDetector hFilter = new HomogenityEdgeDetector(); // apply the filter tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg); hFilter.ApplyInPlace(tmpImg); } if (SobelEdgeDetector) { SobelEdgeDetector hFilter = new SobelEdgeDetector(); // apply the filter tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg); hFilter.ApplyInPlace(tmpImg); BlobCounter bc = new BlobCounter(tmpImg); Rectangle[] brecs = bc.GetObjectsRectangles(); //Graphics pg = Graphics.FromImage(tmpImg); //Pen p = new Pen(Color.White, 2); //foreach (Rectangle r in brecs) //{ // pg.DrawRectangle(p, r); //} } if (findShapes) { tmpImg = FindShapes(tmpImg, ref tmpImg2); //ProcessImage(image); } else { pbCapture.Image = tmpImg; //set picturebox image---------------- pbShapes.Image = tmpImg2; //set picturebox image---------------- } // Graphics g = Graphics.FromImage(tmpImg); // Pen p = new Pen(Color.Red, 2); // Rectangle lr = new Rectangle(100, 120, 80, 40); //// Rectangle rr = new Rectangle(360, 220, 80, 40); // g.DrawRectangle(p, lr); // //g.DrawRectangle(p, rr); } catch (Exception ex) { MessageBox.Show(ex.Message); } // pbCapture.Image = tmpImg;//set picturebox image---------------- // pbShapes.Image = tmpImg2;//set picturebox image---------------- }
/// <summary> /// Detects and recognizes cards from source image /// </summary> /// <param name="source">Source image to be scanned</param> /// <returns>Recognized Cards</returns> public CardCollection Recognize(Bitmap source, string filePath, int id, int minSize, Rectangle suitRect, Rectangle rankRect ) { CardCollection collection = new CardCollection(); //Collection that will hold cards Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image FiltersSequence seq = new FiltersSequence(); seq.Add(Grayscale.CommonAlgorithms.BT709); //First add grayScaling filter seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter temp = seq.Apply(source); // Apply filters on source image //if (!string.IsNullOrEmpty(fileName)) //{ // temp.Save(fileName, ImageFormat.Bmp); //} //Extract blobs from image whose size width and height larger than 150 BlobCounter extractor = new BlobCounter(); extractor.FilterBlobs = true; extractor.MinWidth = extractor.MinHeight = minSize;//TODO card size //extractor.MaxWidth = extractor.MaxHeight = 70;//TODO card size extractor.ProcessImage(temp); //Will be used transform(extract) cards on source image //QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation(); foreach (Blob blob in extractor.GetObjectsInformation()) { var cardImg = source.Clone(blob.Rectangle, PixelFormat.DontCare); Card card = new Card(cardImg); //Create Card Object Bitmap suitBmp = card.GetPart(suitRect); char color = ScanColor(suitBmp); //Scan color seq.Clear(); seq.Add(Grayscale.CommonAlgorithms.BT709); seq.Add(new OtsuThreshold()); suitBmp = seq.Apply(suitBmp); card.Suit = ScanSuit(suitBmp, color); //Scan suit of face card Bitmap rankBmp = card.GetPart(rankRect); seq.Clear(); seq.Add(Grayscale.CommonAlgorithms.BT709); seq.Add(new OtsuThreshold()); rankBmp = seq.Apply(rankBmp); //var ext = new BlobsFiltering(0, 0, 40, 40); //ext.ApplyInPlace(rankBmp); card.Rank = ScanRank(rankBmp); //Scan Rank of non-face card //if (card.Rank == Rank.NOT_RECOGNIZED) //{ // if (!string.IsNullOrEmpty(filePath)) // { // while (File.Exists(filePath + id + ".bmp")) // id++; // top.Save(filePath + id + ".bmp", ImageFormat.Bmp); // } //} if (card.Rank != Rank.NOT_RECOGNIZED && card.Suit != Suit.NOT_RECOGNIZED) { collection.Add(card); //Add card to collection } } collection.SortByRank(); return(collection); }