Opencvsharp 读取摄像头,图片叠加

  

  
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using IDCard.Service;
using System.Media;
using IDCard.Model;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Drawing.Printing;
using System.Data;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using OpenCvSharp.Extensions;

namespace IDCard
{
    public partial class FrmMain : Form
    {
        private VideoCapture cap ; 
        public FrmMain()
        {
            InitializeComponent(); 
        }  

        private void PlayCameraSound()
        {
            SoundPlayer player = new SoundPlayer();
            player.SoundLocation =Application.StartupPath+ "\\Sound\\5756.wav";
            player.Load(); //同步加载声音  
            player.Play(); //启用新线程播放  
        }
 

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;

            if (timer1.Enabled)
            {
                cap = new VideoCapture(0);
            }else
            {
                cap.Release();
            }

            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Mat src1, src2, src3, dst;
            Mat bk1, bk2 ;
            src1 = new Mat();
            src2 = new Mat();
            src3 = new Mat();
            dst  = new Mat();
            bk1 = new Mat();
            bk2 = new Mat();

            bk1 = Cv2.ImRead(@"D:\MyProgram\windows\IDCard\IDCard\IDCard\Image\man2.png");// new Mat(@"D:\MyProgram\windows\IDCard\IDCard\IDCard\Image\man2.png");
                                              
            bk2 = Cv2.ImRead(@"D:\MyProgram\windows\IDCard\IDCard\IDCard\Image\Eye_Invisible_128px.png");
            cap.Read(src1);

            Rect roi = new Rect(0, 0, 460, 460);
            Mat ImageROI = new Mat(bk1, roi);
            Mat mask = ImageROI.CvtColor(ColorConversionCodes.BGR2GRAY);
            Rect rect = new Rect(0, 0, ImageROI.Rows, ImageROI.Cols);
            Mat pos = new Mat(src1, rect); 
            ImageROI.CopyTo(pos,mask);
            pictureBox3.Image = src1.ToBitmap();


            //OpenCvSharp.Size s = new OpenCvSharp.Size(50, 80);
            //Cv2.Resize(src2, src3, s);
 
            //Mat dest = new Mat();
            //cap.Read(dest);
            ////Cv2.AddWeighted(src1, 1, src2, 1, 0.0, dest);
 
            //src1.CopyTo(src2);
            //pictureBox3.Image = src1.ToBitmap();

            //OpenCvSharp.Point pt1  ;

            //pt1.X = 100;
            //pt1.Y = 100;
            //OpenCvSharp.Point pt2;
            //pt2.X = 250;
            //pt2.Y = 150;

            //OpenCvSharp.Scalar sl = new Scalar() ;
            //sl.Val0 = 100;
            //sl.Val1 = 50;
 
        }
    }
}

 

你可能感兴趣的:(C#)