EmguCV not reading camera - c#

I have the the following code in a normal windows form application with EmguCV 3.1
public Form1()
{
InitializeComponent();
_capture = new Capture("http://root:pass#192.168.1.27:80/axis-cgi/mjpg/video.cgi");
_capture.ImageGrabbed += ProcessFrame;
}
private void Form1_Load(object sender, EventArgs e)
{
_capture.Start();
}
private void ProcessFrame(object sender, EventArgs e)
{
Mat image = new Mat();
_capture.Retrieve(image);
imageBox1.BackgroundImage = image.Bitmap;
}
I have tested the above link in a browser it worked, I have also tested this using iSpy it also works there but using EmguCV ProcessFrame is never reached
I have also tried to connect to the camera using Luxand and it worked well but Luxand is not free so I have to use EmguCV to do face detection & recognition

Looking at this post, try adding ?x.mjpeg after .cgi in your url.

Related

c# VLC.Dotnet play and then pause issue

I hope someone will be able to help me with my issue. i want to take a screenshot of the video at a specific time index, however when i try to change the time all i get is a blank black screen. i added buttons which play and pause, it allows me to play and pause the video, if i do that and then change the time index, i get an image. im confused as to why it doesn’t work using code. i even preform btnplay.PeformClick(); to play the video and when i do btnpause.PerformClick() to pause the video it doesn’t.
it seems that i can only get an image of the video if i have to physically hit the play and then pause button on my form, im trying to achieve this using code
private void Form1_Load(object sender, EventArgs e)
{
////////////////////LC4 VLC Settings///////////////////////////////////////////////////////////////////////////////////////////
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
main_form_LC4_data();
}
void main_form_LC4_data()
{
long vOut3 = 20;
playfile("path to file");
First_Frame(vOut3);
}
void playfile(string final)
{
control.SetMedia(new Uri(final).AbsoluteUri);
control.Time = 0;
control.Update();
}
void First_Frame(long vOut3)
{
control.Time = vOut3;
}
private void button9_Click(object sender, EventArgs e)
{
control.Play();
Console.WriteLine("PLAY");
}
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
Console.WriteLine("PAUSE");
}
Above is my code in a nut shell
i have tried things like this
private void button10_Click(object sender, EventArgs e)
{
First_Frame(first_frame); // jump to index
}
and then calling up button10.PerformClick(); however it doesnt seem to work. once again if i physically hit the buttons on my form it works perfectly, however not in the way of coding it.
as an example :
play.PeformClick();
Pause.PeformClick();
time = vOut3;
I do hope this isnt to confusing im really stuck and am still hoping someone can help me
Thank you
A few things:
control.Update();
this does nothing.
You need to wait for the Playing event to be raised after setting the time, otherwise libvlc doesn't have the time to decode the frame and display it (setting the time is asynchronous)

Cant view webcam after being ran once. C#

the title may sound confusing but ill explain it better here. Im making a program which displays the webcam capture in a picturebox using the "easy web cam" external reference. If i turn on my computer, go into VS, open the project and run, it will work, displaying my webcam capture. If i stop the program and then run it again, when i try to display it, i get a popup asking me to select a video source, none of the options is even my webcam and then another popup will appear saying
"An error ocurred while capturing the video image. The video capture will now be terminated.
Object reference not set to ann instance of an object"
The only thing i can think of is that the first time its setting up the camera but when i close it im not turning it off properly so when i run it again, it wont work. anyway heres the relevant code, bare in mind if answering, im not that experienced when coding so sometimes you might have to spell stuff out
using WebCam_Capture;
namespace WindowsWebRef
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
WebCam webcam;
private void button1_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void Frm_Main_Load(object sender, EventArgs e)
{
webcam = new WebCam();
webcam.InitializeWebCam(ref WebCamIMG);
}
And the webcam class...
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Forms.PictureBox _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}
Restart your computer and be sure to add in webcam.Stop(); otherwise your program will hold on to the webcam and make it unavailable to use in any other application (or a different instance of the program.)

Turn a webcam off

Currently My Program is able to turn on the webcam, but right now I have no idea how to turn the webcam off by code.
Here's the code I use to capture some pictures:
private void webcamStart_Click(object sender, EventArgs e)
{
image = new Capture();
image.QueryFrame();
Application.Idle += new EventHandler(FrameGrabber);
}
PS: I using emguCV
Try This,not sure may be help you
Camera.Dispose()
I think this might work for you
image.Close()

How to display multiple images in a single picturebox in c# windows form?

I'm a beginner in developing c# windows form in visual studio and i'm a little bit confused with its programming constructs compared to VB.NET that i'm used to program. I want to know how to display multiple images in a single picturebox because in vb.net you will just import the image then load it from the resources not like in c# that I think the coding is different. Any help would be appreciated.
I would create an ImageList with all the Images you want to show in your PictureBox, add the three Buttons to your Form and change the Picture on Click.
public partial class Form1 : Form
{
private ImageList imagelst;
public Form1()
{
InitializeComponent();
imagelst = new ImageList();
}
private void Form1_Load(object sender, EventArgs e)
{
//pictures from your Harddrive
Image i = new Bitmap("rock.jpg");
imagelst.Images.Add("rock", i);
i = new Bitmap("scissors.jpg");
imagelst.Images.Add("scissors", i);
i = new Bitmap("paper.jpg");
imagelst.Images.Add("paper", i);
}
private void btnRock_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["rock"];
}
private void btnScissors_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["scissors"];
}
private void btnPaper_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["paper"];
}
}
I hope I got what you want to do. If not excuse my bad english and slow-wittedness, please.

Visual C# BackgroundWorker Object is currently in use elsewhere

Hello and thanks for reading my thread! I would like to seek advice for my code because after a lot of searching I couldn't find anything to solve this particular problem. I've googled and searched on stackoverflow and all the solutions somehow didn't work (or I didn't know how to implement them). 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.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Threading ;
namespace Motion_Detection
{
public partial class Form1 : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;
private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = video;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
devicesList.Items.Add(VideoCaptureDevice.Name);
devicesList.SelectedIndex = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[devicesList.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
FinalVideo.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void button4_Click(object sender, EventArgs e)
{
worker.CancelAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
while (!worker.CancellationPending)
{
Bitmap map = new Bitmap(pictureBox1.Image); // this line throws the error
for (int x = 0; x < pictureBox1.Width; x++)
{
for (int y = 0; y < pictureBox1.Height; y++)
{
Color pixel = map.GetPixel(x, y);
if (pixel.R == 255 && pixel.G == 0 && pixel.B == 0)
{
// detected red pixel
}
}
}
Thread.Sleep(100); // check every 100ms or any other given interval
}
}
}
}
So I'm using the Aforge video dlls to access my web camera. This part works, I can access and read the stream from it, when I dump it into picturebox1 it shows up perfectly, without any lag whatsoever.
Now I'm toying a bit around with motion detection and for the first, I wanted to see if I can detect a pixel of a certain color appearing in front of the camera. Because I need to loop through every pixel, I had to put this on a different thread else it kept freezing my GUI and the display started to lag.
The issue is because I did this, I don't know how to properly access the picturebox.image content from the background worker without triggering the error from the title. Some people on the internet suggested using lock() but I never did this nor I know what I should lock() here. I never worked with multithreading before just because in the end I could never handle the access violations..
To fix this problem I tried things like try finally block although even within the try block, I've gotten the same exception. I assume that there is a cleaner way to do what I've mentioned but I can't really get my head around which one that might be.
I hope that my first post here on the forums was clear and understandable as possible.
Thanks in Regards
~ Ilhan
You cant/shouldn't access pictureBox1 unless on the UI thread.
I think you need to do something like this:
private void GetImage(out Bitmap img)
{
img = new Bitmap(pictureBox1.Image);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
Bitmap img = null;
Invoke(new Action(() => GetImage(out img)));
// Do what you want with the bitmap
}
Accessing a control on a winform will throw an exception if its not on the UI thread.
You can find out if you are on the correct thread with pictureBox1.InvokeRequired.
Calling Invoke will send a message to the UI thread to do the passed delegate and then it will wait for the passed delegate to complete. Calling BeginInvoke will send the message but not wait.

Categories