using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Threading;
namespace app01
{
public partial class Form1 : Form
{
Mat camera1_frame1 = new Mat();
//Mat camera1_frame2 = new Mat();
Mat camera2_frame1 = new Mat();
// Mat camera2_frame2 = new Mat();
Bitmap result1 = null;
Bitmap result2 = null;
Bitmap picdiff1, picdiff2;
public static AutoResetEvent autoEvent = new AutoResetEvent(false);
public static AutoResetEvent diffEvnet1 = new AutoResetEvent(false);
public static AutoResetEvent diffEvnet2 = new AutoResetEvent(false);
void readcamera1()
{
Mat src = new Mat();
Mat picMat = new Mat();
//FrameSource carmera2 = Cv2.CreateFrameSource_Video("video8.mp4");//
FrameSource carmera2 = Cv2.CreateFrameSource_Camera(0);
carmera2.NextFrame(src);
while (true)
{
camera1_frame1 = src;
carmera2.NextFrame(src);
//camera2_frame2 = src;
OpenCvSharp.Size size = new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height);
Cv2.Resize(camera1_frame1, picMat, new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height));
//picMat = src;
//Cv2.Resize(src, picMat, new OpenCvSharp.Size(250, 205));
//Cv2.Canny(src, dst, 90,150);
Cv2.Flip(picMat, picMat, FlipMode.Y);
Bitmap bitmap = BitmapConverter.ToBitmap(picMat);
Invalidate();
pictureBox1.Invalidate();
//Thread.Sleep(10);
result1 = bitmap;
Cv2.WaitKey(50);
diffEvnet1.Set();
Cv2.WaitKey(50);
}
}
void readcamera2()
{
Mat src = new Mat();
Mat picMat = new Mat();
FrameSource carmera2 = Cv2.CreateFrameSource_Video("video8.mp4");//
//FrameSource carmera2 = Cv2.CreateFrameSource_Camera(1);
carmera2.NextFrame(src);
while (true)
{
camera2_frame1 = src;
carmera2.NextFrame(src);
//camera2_frame2 = src;
OpenCvSharp.Size size = new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height);
Cv2.Resize(camera2_frame1, picMat, new OpenCvSharp.Size(pictureBox2.Width, pictureBox2.Height));
//picMat = src;
//Cv2.Resize(src, picMat, new OpenCvSharp.Size(250, 205));
//Cv2.Canny(src, dst, 90,150);
Cv2.Flip(picMat, picMat, FlipMode.Y);
Bitmap bitmap = BitmapConverter.ToBitmap(picMat);
Invalidate();
pictureBox2.Invalidate();
//Thread.Sleep(10);
result2 = bitmap;
Cv2.WaitKey(50);
diffEvnet2.Set();
Cv2.WaitKey(50);
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.Paint += pictureBox_Paint;
this.pictureBox2.Paint += pictureBox_Paint;
this.pictureBox3.Paint += pictureBox_Paint;
this.pictureBox4.Paint += pictureBox_Paint;
Thread threadA = new Thread(readcamera1);
//threadA.Start();
Thread threadB = new Thread(readcamera2);
threadA.Start();
threadB.Start();
}
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
if (true)
{
// Invalidate();
// pictureBox1.Invalidate();
if ((object)sender == pictureBox1)
{
if (result1 == null)
{
}
else
{
//pictureBox1.Invalidate();
e.Graphics.DrawImage(result1, 0, 0);
}
}
if ((object)sender == pictureBox2)
{
if (result2 == null)
{
}
else
{
//pictureBox1.Invalidate();
e.Graphics.DrawImage(result2, 0, 0);
}
}
if ((object)sender == pictureBox3)
{
if (picdiff1 == null)
{
}
else
{
//pictureBox1.Invalidate();
e.Graphics.DrawImage(picdiff1, 0, 0);
}
}
if ((object)sender == pictureBox4)
{
if (picdiff2 == null)
{
}
else
{
//pictureBox1.Invalidate();
e.Graphics.DrawImage(picdiff2, 0, 0);
}
}
//pictureBox1.Invalidate();
}
}
private void button1_Click(object sender, EventArgs e)
{
Thread treatplcthread1 = new Thread(findbuddle1);
treatplcthread1.Start();
Thread treatplcthread2 = new Thread(findbuddle2);
treatplcthread2.Start();
}
void savepic() {
Thread.Sleep(3000);
//加线程锁防止摄像头读取视频时破坏保存
autoEvent.WaitOne();
Cv2.ImWrite(DateTime.Now.ToString("yyMMddHHmmss") + ".jpg", camera2_frame1);
Thread.Sleep(3000);
autoEvent.WaitOne();
Cv2.ImWrite(DateTime.Now.ToString("yyMMddHHmmss") + ".jpg", camera2_frame1);
Thread.Sleep(3000);
autoEvent.WaitOne();
Cv2.ImWrite(DateTime.Now.ToString("yyMMddHHmmss") + ".jpg", camera2_frame1);
}
void findbuddle1()
{
Mat frame1 = new Mat();
Mat frame2 = new Mat();
Mat gray1 = new Mat();
Mat gray2 = new Mat();
//二值化矩阵
Mat thresh1 = new Mat();
Mat thresh2 = new Mat();
//两者不同矩阵
Mat diff1 = new Mat();
//等待图片读取完成
// diffEvnet2.WaitOne();
//frame1 = camera1_frame1;
while (true)
{
diffEvnet1.WaitOne();
camera1_frame1.CopyTo(frame1);
Thread.Sleep(50);
diffEvnet1.WaitOne();
camera1_frame1.CopyTo(frame2);
//frame2 = camera1_frame1;
//灰度处理
Cv2.CvtColor(frame1, gray1, ColorConversionCodes.BGR2GRAY);
Cv2.CvtColor(frame2, gray2, ColorConversionCodes.BGR2GRAY);
//二值化处理
Cv2.Threshold(gray1, thresh1, 127, 255, ThresholdTypes.Binary);
Cv2.Threshold(gray2, thresh2, 127, 255, ThresholdTypes.Binary);
//取不同地板
//需加线程锁,否则会有时候thresh1和thresh2的图片大小不同,会造型比较时异常
//Cv2.Resize(thresh1, thresh1, new OpenCvSharp.Size(pictureBox3.Width, pictureBox3.Height));
//Cv2.Resize(thresh2, thresh2, new OpenCvSharp.Size(pictureBox3.Width, pictureBox3.Height));
Cv2.BitwiseXor(thresh1, thresh2, diff1);
Cv2.BitwiseAnd(diff1, thresh2, diff1);
//Cv2.Resize(diff2, diff2, new OpenCvSharp.Size(pictureBox3.Width, pictureBox3.Height));
Cv2.Resize(diff1, diff1, new OpenCvSharp.Size(pictureBox4.Width, pictureBox4.Height));
Cv2.Flip(diff1, diff1, FlipMode.Y);
Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp. Size(3, 3), new OpenCvSharp.Point(-1, -1));
//Cv2.Erode(diff1, diff1, se);
//Cv2.Dilate(diff1, diff1, se);
picdiff1 = BitmapConverter.ToBitmap(diff1);
Invalidate();
pictureBox3.Invalidate();
// Cv2.ImShow("videothresh1", frame1);
// Cv2.ImShow("thresh2", frame2);
// Cv2.ImShow("idff2", diff2);
//Cv2.ImShow("diff", thresh1);
// Cv2.WaitKey(0);
}
}
void findbuddle2()
{
Mat frame1 = new Mat();
Mat frame2 = new Mat();
Mat gray1 = new Mat();
Mat gray2 = new Mat();
//二值化矩阵
Mat thresh1 = new Mat();
Mat thresh2 = new Mat();
//两者不同矩阵
Mat diff2 = new Mat();
//等待图片读取完成
// diffEvnet2.WaitOne();
//frame1 = camera1_frame1;
while (true)
{
diffEvnet2.WaitOne();
camera2_frame1.CopyTo(frame1);
Thread.Sleep(50);
diffEvnet2.WaitOne();
camera2_frame1.CopyTo(frame2);
//frame2 = camera1_frame1;
//灰度处理
Cv2.CvtColor(frame1, gray1, ColorConversionCodes.BGR2GRAY);
Cv2.CvtColor(frame2, gray2, ColorConversionCodes.BGR2GRAY);
//二值化处理
Cv2.Threshold(gray1, thresh1, 127, 255, ThresholdTypes.Binary);
Cv2.Threshold(gray2, thresh2, 127, 255, ThresholdTypes.Binary);
//取不同地板
//需加线程锁,否则会有时候thresh1和thresh2的图片大小不同,会造型比较时异常
//Cv2.Resize(thresh1, thresh1, new OpenCvSharp.Size(pictureBox3.Width, pictureBox3.Height));
//Cv2.Resize(thresh2, thresh2, new OpenCvSharp.Size(pictureBox3.Width, pictureBox3.Height));
Cv2.BitwiseXor(thresh1, thresh2, diff2);
Cv2.BitwiseAnd(diff2, thresh2, diff2);
//Cv2.Resize(diff2, diff2, new OpenCvSharp.Size(pictureBox3.Width, pictureBox3.Height));
Cv2.Resize(diff2, diff2, new OpenCvSharp.Size(pictureBox4.Width, pictureBox4.Height));
picdiff2 = BitmapConverter.ToBitmap(diff2);
Invalidate();
pictureBox4.Invalidate();
// Cv2.ImShow("videothresh1", frame1);
// Cv2.ImShow("thresh2", frame2);
// Cv2.ImShow("idff2", diff2);
//Cv2.ImShow("diff", thresh1);
// Cv2.WaitKey(0);
}
}
}
}