Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Emgu.CV.CvEnum;
- using Emgu.CV.Structure;
- using Emgu.Util;
- using Emgu.CV;
- namespace Emgu_Cam
- {
- public partial class MainWindow : Form
- {
- // declaring global variables
- private Capture capture; //takes images from camera as image frames
- private bool captureInProgress; //checks if capture is executing
- public MainWindow()
- {
- InitializeComponent();
- }
- private void captureButton_Click(object sender, EventArgs e)
- {
- CaptureImage();
- captureButton.Enabled = false;
- stopCaptureButton.Enabled = true;
- }
- private void ProcessFrame(object sender, EventArgs arg)
- {
- //we start from taking a frame from the camera
- Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
- //now we must scale the image
- double scale;
- if((camImageBox.Width / (double)ImageFrame.Width) >= (camImageBox.Height / (double)ImageFrame.Height))
- {
- scale = camImageBox.Width / (double)ImageFrame.Width;
- }
- else
- {
- scale = camImageBox.Height / (double)ImageFrame.Height;
- }
- ImageFrame = ImageFrame.Resize(scale, INTER.CV_INTER_LINEAR);
- //insert the image
- camImageBox.Image = ImageFrame;
- }
- private void CaptureImage()
- {
- #region if capture is not created, create it now
- if (capture == null)
- {
- try
- {
- capture = new Capture();
- }
- catch (NullReferenceException excpt)
- {
- MessageBox.Show(excpt.Message);
- }
- }
- #endregion
- if (capture != null)
- {
- if(captureInProgress == false)
- {
- Application.Idle += ProcessFrame;
- captureInProgress = true;
- }
- else
- {
- Application.Idle -= ProcessFrame;
- captureInProgress = false;
- }
- }
- }
- private void stopCaptureButton_Click(object sender, EventArgs e)
- {
- CaptureImage();
- captureButton.Enabled = true;
- stopCaptureButton.Enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement