Emgu CV问题有一个简短的代码

时间:2013-08-04 15:20:15

标签: c# computer-vision emgucv

我一直在测试并搜索帮助,但无法解决此问题:

在C#和emgu中,我无法运行这个最简单的代码:

... //**after these 3 dots HERE I GOT THE FIRST WARNING :a namespace cannot directly contain members such as fields or methods**

//Create an image of 400x200 of Blue color
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0))) 
{
   //Create the font
   MCvFont f = new MCvFont(CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0);

   //Draw "Hello, world." on the image using the specific font
   img.Draw("Hello, world", ref f, new Point(10, 80), new Bgr(0, 255, 0)); 

   //Show the image using ImageViewer from Emgu.CV.UI
   ImageViewer.Show(img, "Test Window");
}

我收到了以下错误:

error CS0116: A namespace cannot directly contain members such as fields or methods
error CS1518: Expected class, delegate, enum, interface, or struct

PS: emgu cv已经正确安装,在C#中这个代码(更复杂)完美运行:

namespace CameraCapture
{
    public partial class CameraCapture : Form
    {
        //declaring global variables
        private Capture capture;        //takes images from camera as image frames
        private bool captureInProgress;

        public CameraCapture()
        {
            InitializeComponent();
        }
        //------------------------------------------------------------------------------//
        //Process Frame() below is our user defined function in which we will create an EmguCv 
        //type image called ImageFrame. capture a frame from camera and allocate it to our 
        //ImageFrame. then show this image in ourEmguCV imageBox
        //------------------------------------------------------------------------------//
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
            CamImageBox.Image = ImageFrame;
        }

        //btnStart_Click() function is the one that handles our "Start!" button' click 
        //event. it creates a new capture object if its not created already. e.g at first time
        //starting. once the capture is created, it checks if the capture is still in progress,
        //if so the
        private void btnStart_Click(object sender, EventArgs e)
        {
            #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)
                {  //if camera is getting frames then stop the capture and set button Text
                    // "Start" for resuming capture
                    btnStart.Text = "Start!"; //
                    Application.Idle -= ProcessFrame;
                }
                else
                {
                    //if camera is NOT getting frames then start the capture and set button
                    // Text to "Stop" for pausing capture
                    btnStart.Text = "Stop";
                    Application.Idle += ProcessFrame;
                }

                captureInProgress = !captureInProgress;
            }
        }

        private void ReleaseData()
        {
            if (capture != null)
                capture.Dispose();
        }

        private void CameraCapture_Load(object sender, EventArgs e)
        {

        }
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

也许您的命名空间名称与某个类或不同的结构相同,因此与命名空间冲突

使用*注释全部,并使用全名限定符(例如System.Console.WriteLine()...),然后它应该正常运行(如果这是一个问题)。