//打开摄像头 private void btn_play_Click(object sender, EventArgs e) { if (!bPlayflag) { m_vCapture = new VideoCapture(CaptureDevice.Any); if (!m_vCapture.IsOpened()) { MessageBox.Show("摄像头打不开", "摄像头故障", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } m_vCapture.Set(CaptureProperty.FrameWidth, 640); //宽度 m_vCapture.Set(CaptureProperty.FrameHeight, 480); //高度 bPlayflag = true; ThreadCam = new Thread(Play_Camera); ThreadCam.Start(); pic_cam.Image = null; btn_play.Text = "关闭摄像头"; } else { bPlayflag = false; ThreadCam.Abort(); m_vCapture.Release(); btn_play.Text = "打开摄像头"; } }
static void Main(string[] args) { //size of tag is in meters and camera parameters are obtained from calibration float tag_size = 0.1F; float fx = 1200F; float fy = 1400F; double px = 817.143; double py = 387.159; //array of floats to carry values of image points (x and y * 4 points) float[] ptsry = new float[8]; //initialize video capture from camera var capt = new OpenCvSharp.VideoCapture(); capt.Open(0); //window for displaying video Window window = new Window("capture"); //main task; display video and find tags using (Mat frame = new Mat()) { //looping untill key is pressed while (true) { //read from camera and show it capt.Read(frame); window.ShowImage(frame); //detect tags and find how many and print the number Apriltag ap = new Apriltag("canny", true, "tag16h5"); var current_tags = ap.detect(frame); Console.WriteLine("Number of tags = " + current_tags.Count); Console.WriteLine(); //sleep for 10 msec System.Threading.Thread.Sleep(10); //if a key is pressed, close the window and exit if (Cv2.WaitKey(1) >= 0) { capt.Release(); Cv2.DestroyAllWindows(); break; } } } }
private async Task CaptureCamera(CancellationToken token) { if (capture == null) { capture = new cv.VideoCapture(0, cv.VideoCaptureAPIs.ANY); } capture.Open(0); if (capture.IsOpened()) { try { while (!token.IsCancellationRequested) { using MemoryStream memoryStream = capture.RetrieveMat().ToMemoryStream(); await Application.Current.Dispatcher.InvokeAsync(() => { try { var imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.CacheOption = BitmapCacheOption.OnLoad; imageSource.StreamSource = memoryStream; imageSource.EndInit(); img_WebCam.Source = imageSource; }catch (Exception e) { } }); bitmapImage = new Bitmap(memoryStream); //await ParseWebCamFrame(bitmapImage, token); } capture.Release(); } catch (Exception e) { } } }
private async Task CaptureCamera(CancellationToken token) { if (capture == null) { capture = new OpenCvSharp.VideoCapture(CaptureDevice.DShow); } capture.Open(0); m_capture.Start(); if (capture.IsOpened()) // if(m_capture.IsOpened) { while (!token.IsCancellationRequested) { using MemoryStream memoryStream = capture.RetrieveMat().Flip(FlipMode.Y).ToMemoryStream(); // using MemoryStream memoryStream = m_capture.QueryFrame()..RetrieveMat().Flip(FlipMode.Y).ToMemoryStream(); await Application.Current.Dispatcher.InvokeAsync(() => { var imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.CacheOption = BitmapCacheOption.OnLoad; imageSource.StreamSource = memoryStream; imageSource.EndInit(); OpenCVSharpImageSource.Source = imageSource; }); var bitmapImage = new Bitmap(memoryStream); await ParseWebCamFrame(bitmapImage, token); } capture.Release(); } }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { released = true; cap.Release(); cap.Dispose(); }