Exemple #1
0
 /// <summary>
 /// 基于OpenCv捕获视频流
 /// </summary>
 /// <param name="url"></param>
 /// <param name="action"></param>
 public void OpenCvCaptureVideoStream(string url, Action <OpenCvSharp.Mat> action)
 {
     using (OpenCvSharp.VideoCapture capture = new OpenCvSharp.VideoCapture(url))
     {
         //FourCC fourCC = FourCC.FromFourChars('m', 'p', '4', 'v');
         if (!capture.IsOpened())
         {
             return;
         }
         //double fps = capture.Get(VideoCaptureProperties.Fps);
         //double width = capture.Get(VideoCaptureProperties.FrameWidth);
         //double height = capture.Get(VideoCaptureProperties.FrameHeight);
         //OpenCvSharp.Size size = new OpenCvSharp.Size(width, height);
         using OpenCvSharp.Mat frame = new OpenCvSharp.Mat();
         while (_stopPlay == false)
         {
             if (capture.Read(frame))
             {
                 //win?.ShowImage(frame);
                 action?.Invoke(frame);
             }
         }
         frame.Dispose();
         capture.Dispose();
     }
 }
Exemple #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult res = openFileDialog1.ShowDialog();

            if (res == DialogResult.OK)
            {
                string ext = System.IO.Path.GetExtension(openFileDialog1.FileName);

                if (ext.ToLower() == ".bmp" || ext.ToLower() == ".jpg" || ext.ToLower() == ".jpeg" || ext.ToLower() == ".png")
                {
                    pictureBox1.Image = CreateImage(openFileDialog1.FileName);
                }
                if (ext.ToLower() == ".avi" || ext.ToLower() == ".mp4")
                {
                    System.IO.Directory.SetCurrentDirectory(apppath);
                    System.IO.DirectoryInfo target = new System.IO.DirectoryInfo(@"main\LR\calendar");
                    foreach (System.IO.FileInfo file in target.GetFiles())
                    {
                        file.Delete();
                    }
                    int image_num = 1;
                    video = true;
                    OpenCvSharp.VideoCapture vcap = new OpenCvSharp.VideoCapture(openFileDialog1.FileName);
                    while (vcap.IsOpened())
                    {
                        if (stopping)
                        {
                            break;
                        }

                        OpenCvSharp.Mat mat = new OpenCvSharp.Mat();

                        if (vcap.Read(mat))
                        {
                            if (pictureBox1.Image != null)
                            {
                                pictureBox1.Image.Dispose();//Memory release
                            }

                            if (mat.IsContinuous())
                            {
                                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);

                                //OpenCvSharp.Cv2.Resize(mat, mat, OpenCvSharp.Size(), 0, 0);
                                string filename = string.Format(@"main\LR\calendar\{0:D4}.png", image_num);
                                //pictureBox1.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);

                                OpenCvSharp.Cv2.ImWrite(filename, mat, (int[])null);
                                image_num++;
                                //if (image_num > 10) break;
                            }
                            else
                            {
                                break;
                            }
                            Application.DoEvents(); // 非推奨
                        }
                        else
                        {
                            break;
                        }
                        System.Threading.Thread.Sleep((int)(1000 / vcap.Fps));

                        mat.Dispose();//Memory release
                    }
                    Fps = vcap.Fps;
                    vcap.Dispose();//Memory release
                    stopping = false;

                    pictureBox1.Image = CreateImage(@"main\LR\calendar\0001.png");
                }
            }
        }
Exemple #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            folder = "";
            DialogResult res = openFileDialog1.ShowDialog();

            if (res == DialogResult.OK)
            {
                label1.Text = "Image is loading";
                label1.Refresh();
                string ext = System.IO.Path.GetExtension(openFileDialog1.FileName);

                if (ext.ToLower() == ".bmp" || ext.ToLower() == ".jpg" || ext.ToLower() == ".jpeg" || ext.ToLower() == ".png")
                {
                    pictureBox1.Image = CreateImage(openFileDialog1.FileName);
                }
                if (ext.ToLower() == ".avi" || ext.ToLower() == ".mp4")
                {
                    System.IO.Directory.SetCurrentDirectory(apppath);
                    System.IO.DirectoryInfo target = new System.IO.DirectoryInfo(@"main\tecoGAN\LR\calendar");
                    foreach (System.IO.FileInfo file in target.GetFiles())
                    {
                        file.Delete();
                    }
                    int image_num = 1;
                    video = true;
                    OpenCvSharp.VideoCapture vcap = new OpenCvSharp.VideoCapture(openFileDialog1.FileName);
                    while (vcap.IsOpened())
                    {
                        if (stopping)
                        {
                            break;
                        }

                        OpenCvSharp.Mat mat = new OpenCvSharp.Mat();
                        if (image_num % 9 == 0)
                        {
                            label1.Text = "";
                            label1.Refresh();
                        }
                        else
                        {
                            label1.Text = "Image is loading";
                            label1.Refresh();
                        }
                        if (vcap.Read(mat))
                        {
                            if (pictureBox1.Image != null)
                            {
                                pictureBox1.Image.Dispose();//Memory release
                            }

                            if (mat.IsContinuous())
                            {
                                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
                                {
                                    //pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

                                    Bitmap bmp = new Bitmap(
                                        pictureBox1.Image,
                                        pictureBox1.Image.Width * 4,
                                        pictureBox1.Image.Height * 4);

                                    pictureBox1.Image = bmp;
                                }

                                if (!checkBox1.Checked)
                                {
                                    OpenCvSharp.Cv2.Resize(mat, mat, new OpenCvSharp.Size((int)((float)mat.Width * (float)numericUpDown1.Value), (int)((float)mat.Height * (float)numericUpDown1.Value)), 0, 0);
                                }
                                else
                                {
                                    OpenCvSharp.Cv2.Resize(mat, mat, new OpenCvSharp.Size((int)(0.5f + (float)mat.Width / (float)numericUpDown1.Value), (int)(0.5f + (float)mat.Height / (float)numericUpDown1.Value)), 0, 0);
                                }

                                string filename = string.Format(@"main\tecoGAN\LR\calendar\{0:D4}.png", image_num);
                                //pictureBox1.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);

                                OpenCvSharp.Cv2.ImWrite(filename, mat, (int[])null);
                                image_num++;
                                //if (image_num > 10) break;
                            }
                            else
                            {
                                break;
                            }
                            Application.DoEvents(); // 非推奨
                        }
                        else
                        {
                            break;
                        }
                        System.Threading.Thread.Sleep((int)(1000 / vcap.Fps));

                        mat.Dispose();//Memory release
                    }
                    Fps = vcap.Fps;
                    vcap.Dispose();//Memory release
                    stopping = false;

                    pictureBox1.Image = CreateImage(@"main\tecoGAN\LR\calendar\0001.png");
                }
                label1.Text = "";
                label1.Refresh();
                MessageBox.Show("finished");
            }
        }