I'm a newbie in emgu cv and for a major project I'm trying to capture an image from a webcam and show it in an image box, but it's showing a black image.
What is wrong with the following code?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.Util;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
private Capture capture;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (capture == null)
capture = new Capture();
Image<Bgr,Byte> img=capture.QueryFrame();
imageBox1.Image = img;
}
}
}
I think there is a minor mistake.
Use this instead:
Declare as global variable:
Capture capture = default(Capture);
Put this in load:
capture = new Capture(0);
Control.CheckForIllegalCrossThreadCalls = false;
System.Threading.Thread t = new System.Threading.Thread(grab);
t.Start();
Create a sub grab and put in,
do {
ImageBox1.Image = capture.QueryFrame();
} while (true);
Cheers
Shreyas
Call QueryFrame() twice, it works for me:
if (capture == null)
capture = new Capture();
capture.QueryFrame();
Image<Bgr,Byte> img=capture.QueryFrame().ToImage<Bgr, Byte>();
imageBox1.Image = img.Bitmap;
In my case Kaspersky AntiVirus was blocking the access to my webcam. After changing some security settings I was able to get emgu capture back working.
Related
I have written C# code of live streaming of IP camera (JPEG) in windows form application using AForge library. It is working but it's lagging too much.
Here is the code:
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 AForge.Video;
namespace CCTV_software
{
public partial class Form1 : Form
{
JPEGStream stream;
JPEGStream stream1;
public Form1()
{
InitializeComponent();
stream = new JPEGStream("ip");
stream1 = new JPEGStream("ip1");
stream.Login = "username";
stream.Password = "password";
stream1.Login = "username1";
stream1.Password = "password1";
stream.NewFrame += stream_NewFrame;
stream1.NewFrame += stream1_NewFrame1;
stream.Start();
stream1.Start();
}
void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bmp;
}
void stream1_NewFrame1(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bp = (Bitmap)eventArgs.Frame.Clone();
pictureBox2.Image = bp;
}
}
Tried adding:
stream.FrameInterval = 0;
But it didn’t made any difference.
Kindly help me out with this issue.
Edit:
Edit 2:
#mjwills and my suggestions combined:
void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
var oldImage = pictureBox1.Image
pictureBox1.Image = eventArgs.Frame; // set to new image without making a copy
oldImage?.Dispose(); // = Dispose previous image, if not null
}
This spares you a clone which is expensive and allows the gc to clean up the images timely.
Those measures combined should make it possible to improve throughput (i.e. lessen "lag"). Further countermeasures could be to make the image size smaller or reduce quality ...
NOTE: Above measures should at least improve lag, yet it is not guaranteed to make it disappear completely, since lag may be caused at other places, too.
I am testing out EMGU for a school assignment, and I am following this tutorial to get a basic understanding with the way EMGU works, but I have a problem.
Here is my code:
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;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
namespace FacialRecognition
{
public partial class Form1 :Form
{
private Capture _capture;
bool CapturingProcess = false;
Image<Bgr, Byte> imgOrg;
Image<Gray, Byte> imgProc;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try {
_capture = new Capture(CaptureType.DShow);
} catch(NullReferenceException ex) {
MessageBox.Show(ex.Message);
return;
}
}
void processFunction(object sender, EventArgs e)
{
imgOrg = _capture.QueryFrame();
if(imgOrg == null) return;
imgProc = imgOrg.InRange(new Bgr(50, 50, 50), new Bgr(255, 255, 255));
imgProc = imgProc.SmoothGaussian(9);
original.Image = imgOrg;
processed.Image = imgProc;
}
}
}
However, I get the error:
The type or namespace name 'Capture' could not be found (are you missing a using directive or an assembly reference?)
and it suggests using System.Text.RegularExpressions;, which is quite strange.
I'm guessing I am missing something, but I referenced all of the DLL's
in the tutorial. Here are some screenshots:
Solution Explorer (They are set to Copy always)
References (Emgu.CV.World.NetStandard1_4.dll and Emgu.CV.World.dll were colliding)
Capture has been renamed to VideoCapture in new versions of EMGU , so try
private VideoCapture _capture;
You might consider using Emgu.CV.VideoCapture in EMGU.CV NuGet package (http://www.emgu.com/wiki/index.php/Main_Page) instead of Capture. So, your declaration becomes:
private VideoCapture _capture;
I have this code that uses NAudio to look for all microphones connected on laptop, andwhen I choose one it shows a meter of the sound in a progressbar.
when I gather all device, I collect them in a dropdwon list, my issue is that when I choose the one microphone from the list it will not be activated and display the sound on a meter unless I start WINDOWS SOUND RECORDER which seems to activate the mic.
how to enable or activate the microphone from the code without starting WINDOWS SOUND RECORDER?
here is the code:
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 NAudio;
using NAudio.CoreAudioApi;
using System.Threading;
using NAudio.Wave;
using NAudio.Mixer;
namespace NaudioVisualizer
{
public partial class Form1 : Form
{
public int a = 1;
public Form1()
{
InitializeComponent();
MMDeviceEnumerator de = new MMDeviceEnumerator();
var device = de.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
comboBox1.Items.AddRange(device.ToArray());
}
private void timer1_Tick(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
var device = (MMDevice)comboBox1.SelectedItem;
progressBar1.Value = (int)Math.Round(device.AudioMeterInformation.MasterPeakValue * 100);
}
}
}
}
I had the same problem try to use the following code :
var waveIn = new WaveIn();
waveIn.StartRecording();
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;
using Emgu.CV.Structure;
namespace WebcamTest
{
public partial class Form1 : Form
{
private Capture c;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
c = new Capture();
c.ImageGrabbed += ProcessFrame;
c.Start();
}
public void ProcessFrame(object sender, EventArgs e)
{
Image<Bgr, byte> image = c.QueryFrame();
c.ImageGrabbed -= ProcessFrame;
c.Stop();
c.Dispose();
}
}
}
So I have this really simple code to grab a webcam image and store it, just one time. It does nothing else currently, but I'm having a weird issue. In my file, one line seems to cause the issue:
Image<Bgr, byte> image = c.QueryFrame();
EDIT: I have tried changing this line to this as the wiki suggests:
Image<Bgr, byte> image = new Image<Bgr, byte>(c.RetreiveBgrFrame().ToBitmap());
This one always gives a null reference exception the first time; afterwards it acts the same as the other line. Every pixel's combined values are 0's after the first run.
Then I go a bit deeper into the problem,
I found that I keep getting a StackOverflowException in the Emgu.CV.dll itself. It continously says it is line 240 in this file: Capture.cs
The line is this:
bool grabbed = CvInvoke.cvGrabFrame(_ptr);
I have ran through their examples before and actually had them working before. I have never really had this issue before since it's coming from inside their dll. Why would this line keep causing this error? Is the pointer way off point for some unknown reason? The only thing I could think was that it was trying to access a memory location outside the actual bounds. It's always the very first run it does this. Then when it crashes, the camera stays on and so next time it runs. Though the image pixels are all 0's every time after that.
I am getting an unhandled exception when trying to create a D3d device.
This is literally the whole code in C#. I am just testing and trying to learn directx.
I am getting the Unhandled exception on this line
: device = new Device(0, DeviceType.Hardware, this,CreateFlags.SoftwareVertexProcessing, presentParams);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Device device = null; // Create rendering device
PresentParameters presentParams = new PresentParameters();
device = new Device(0, DeviceType.Hardware, this,CreateFlags.SoftwareVertexProcessing, presentParams);
RenderToSurface render = new RenderToSurface(device, 300, 300, Format.D32, true, DepthFormat.D32);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Kind of blowing my mind because it looks like all the params are right..
needed presentParams.Windowed = true in order for this to work.