基本信息
源码名称:AForge通过摄像头识别物体(扑克牌识别程序源码)
源码大小:7.23M
文件格式:.zip
开发语言:C#
更新时间:2018-08-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
打开摄像头后,可识别移动/倾斜的扑克牌,图一是 手机上找到的扑克牌,也识别出来了



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging.Filters;

using PlayingCardRecognition;

namespace PlayingCardRecognition_WebCam
{
    public partial class Form1 : Form
    {
        private const int CameraWidth = 640;  // constant Width
        private const int CameraHeight = 480; // constant Height

        private FilterInfoCollection cameras; //Collection of Cameras that connected to PC
        private VideoCaptureDevice device; //Current chosen device(camera) 
        private Dictionary<string, string> cameraDict = new Dictionary<string, string>();
        private Pen pen = new Pen(Brushes.Orange, 4); //is used for drawing rectangle around card
        private Font font = new Font("Tahoma", 15, FontStyle.Bold); //is used for writing string on card
        private CardRecognizer recognizer = new CardRecognizer();
        private CardCollection cards;

        private int frameCounter = 0;

        public Form1()
        {
            InitializeComponent();

            //Fetch cameras 
            this.cameras = new FilterInfoCollection(AForge.Video.DirectShow.FilterCategory.VideoInputDevice);
            int i = 1;
            foreach (AForge.Video.DirectShow.FilterInfo camera in this.cameras)
            {
                if (!this.cameraDict.ContainsKey(camera.Name))
                    this.cameraDict.Add(camera.Name, camera.MonikerString);
                else
                {
                    this.cameraDict.Add(camera.Name   "-"   i.ToString(), camera.MonikerString);
                    i  ;
                }
            }
            this.cbCamera.DataSource = new List<string>(cameraDict.Keys); //Bind camera names to combobox

            if (this.cbCamera.Items.Count == 0)
                button1.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Start")
            {
                this.button1.Text = "Stop";
                this.device = new VideoCaptureDevice(this.cameraDict[cbCamera.SelectedItem.ToString()]);
                this.device.NewFrame  = new NewFrameEventHandler(videoNewFrame);
                this.device.DesiredFrameSize = new Size(CameraWidth, CameraHeight);

                device.Start(); //Start Device
            }
            else
            {
                this.StopCamera();
                button1.Text = "Start";
                this.pictureBox1.Image = null;
            }
        }
        private Bitmap ResizeBitmap(Bitmap bmp)
        {
            ResizeBilinear resizer = new ResizeBilinear(pictureBox1.Width, pictureBox1.Height);

            return resizer.Apply(bmp);
        }

        private void videoNewFrame(object sender, NewFrameEventArgs args)
        {

            Bitmap temp = args.Frame.Clone() as Bitmap;

            try
            {
                frameCounter  ;

                if (frameCounter > 10)
                {
                    cards = recognizer.Recognize(temp);
                    frameCounter = 0;
                }

                //Draw Rectangle around cards and write card strings on card
                using (Graphics graph = Graphics.FromImage(temp))
                {
                    foreach (Card card in cards)
                    {
                        graph.DrawPolygon(pen, card.Corners); //Draw a polygon around card
                        PointF point = CardRecognizer.GetStringPoint(card.Corners); //Find Top left corner
                        point.Y  = 10;
                        graph.DrawString(card.ToString(), font, Brushes.White, point); //Write string on card
                    }
                }
            }
            catch { }
            this.pictureBox1.Image = ResizeBitmap(temp);
        }
        private void StopCamera()
        {
            if (device != null && device.IsRunning)
            {
                device.SignalToStop(); //stop device
                device.WaitForStop();
                device = null;
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.StopCamera();
        }
    }
}