使用C#检测扫描图像的区域

时间:2012-12-20 04:01:46

标签: c# winforms

我正在开发一个C#应用程序来处理扫描图像。扫描图像包含12个矩形子区域,如下所示。

是否有任何图书馆或代码示例可以帮助我确定每个地区的位置和尺寸?

sample scanned image

帮帮我。我搜索了codeproject(http://www.codeproject.com/Articles/265354/Playing-Card-Recognition-Using-AForge-Net-Framewor)中的一篇文章,找到了我想要的解决方案。它有2个项目。第一个项目是PlayingCardRecognition,我创建了文件

  

TestCard.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace PlayingCardRecognition
{
    public class TestCard
    {
        public string name { get; set; }
        public Bitmap bitmap { get; set; }
        public Point[] point { get; set; }
    }
}
  

TestCardCollection.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace PlayingCardRecognition
{
    public class TestCardCollection : CollectionBase
    {
        public void Add(TestCard card)
        {
            this.List.Add(card);
        }
    }
}
  

我更改了文件 CardRecognizer.cs ,我添加了一个从识别方法更改的方法 ExtractImages

public TestCardCollection ExtractImages(Bitmap source)
    {
        TestCardCollection col = new TestCardCollection();
        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);

        Blob[] blobs = extractor.GetObjectsInformation();

        for (int i = 0; i < blobs.Length; i++)
        {
            Blob blob = blobs[i];
            TestCard card = new TestCard();
            card.name = "" + i;


            List<IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob);
            //Calculate/Find corners of card on source image from edge points
            List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
            if (corners.Count < 4)
                quadTransformer.SourceQuadrilateral = new List<IntPoint>(edgePoints);
            else
                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
            card.bitmap = resizer.Apply(cardImg); //Normalize card size

            IntPoint[] cornerIntPoints = corners.ToArray();

            Point[] corner = new Point[cornerIntPoints.Length];
            for (int z = 0; z < cornerIntPoints.Length; z++)
            {
                corner[z].X = cornerIntPoints[z].X;
                corner[z].Y = cornerIntPoints[z].Y;
            }
            card.point = corner;
            col.Add(card);
        }

        return col;
    }
  

第二个项目PlayingCardRecognition_SampleImages我有 test.cs 文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Imaging.Filters;
using PlayingCardRecognition;

namespace PlayingCardRecognition_SampleImages
{
    public partial class test : Form
    {
        public test()
        {
            InitializeComponent();
            ResizeBilinear resizer = new ResizeBilinear(pictureBox1.Width, pictureBox1.Height);
            Bitmap bm = Properties.Resources.testtrip;
            pictureBox1.Image = resizer.Apply(bm);

        }
        private CardRecognizer recognizer = new CardRecognizer();

        private void button1_Click(object sender, EventArgs e)
        {
            int w = 0;
            TestCardCollection col =  recognizer.ExtractImages(Properties.Resources.testtrip);
            foreach (TestCard card in col)
            {
                PictureBox pic = new PictureBox();
                pic.Image = card.bitmap;
                pic.Width = card.bitmap.Width;
                pic.Height = card.bitmap.Height;
                pic.Location = new Point(w, 0);
                pnMain.Controls.Add(pic);
                w += card.bitmap.Width + 10;
            }
        }
    }
}

我调用了 ExtractImages 方法,我得到12张图片但区域错误 它显示如下图像。 show image return 为什么? 我的项目here

感谢和抱歉我的英语。

1 个答案:

答案 0 :(得分:1)

查看OpenCV,我认为它有一个.Net版本。

基本上,你需要定义什么是区域。然后使用边缘来分割它们,因为它们是矩形的。通常,您可以使用图像处理工具(即Matlab图像处理工具箱)来测试您的算法并确保它有效,然后编写C#。