C# Emgu CV问题,代码较短
我一直在测试和搜索帮助,但无法解决此问题: 在C#和emgu中,我无法运行以下最简单的代码:C# Emgu CV问题,代码较短,c#,computer-vision,emgucv,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>
... //**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");
}
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)
{
}
}
}
命名空间CameraCapture
{
公共部分类摄影机捕捉:表单
{
//声明全局变量
private Capture Capture;//将相机中的图像作为图像帧进行拍摄
私人bool captureInProgress;
公共摄像
{
初始化组件();
}
//------------------------------------------------------------------------------//
//下面的Process Frame()是我们的用户定义函数,我们将在其中创建一个EmguCv
//键入名为ImageFrame的图像。从相机捕获一帧并将其分配给我们的
//图像框。然后在我们的EMGUCV图像框中显示此图像
//------------------------------------------------------------------------------//
私有void ProcessFrame(对象发送方、事件args arg)
{
Image ImageFrame=capture.QueryFrame();
CamImageBox.Image=ImageFrame;
}
//btnStart_Click()函数是处理“开始!”按钮“单击”的函数
//事件。如果尚未创建新的捕获对象,它将创建一个新的捕获对象。例如,在第一次
//开始。创建捕获后,它会检查捕获是否仍在进行中,
//如果是的话
私有void btnStart_单击(对象发送方,事件参数e)
{
#如果未创建捕获,请立即创建
如果(捕获==null)
{
尝试
{
捕获=新捕获();
}
捕获(NullReferenceExcpt)
{
MessageBox.Show(除消息外);
}
}
#端区
如果(捕获!=null)
{
if(captureInProgress)
{//如果相机正在获取帧,则停止捕获并设置按钮文本
//用于恢复捕获的“启动”
btnStart.Text=“开始!”//
Application.Idle-=ProcessFrame;
}
其他的
{
//如果相机未获取帧,则启动“捕获并设置”按钮
//用于暂停捕获的“停止”文本
btnStart.Text=“停止”;
Application.Idle+=ProcessFrame;
}
captureInProgress=!captureInProgress;
}
}
私有void ReleaseData()
{
如果(捕获!=null)
capture.Dispose();
}
私有void CameraCapture_加载(对象发送方,事件参数e)
{
}
}
}
谢谢你的帮助 可能您的命名空间名称与某个类或不同的结构相同,因此与命名空间冲突 使用*注释所有内容,并使用全名限定符(例如System.Console.WriteLine()…),然后它应该正常运行(如果这是一个问题)
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)
{
}
}
}