Blank or Wrong Picture from EMGU capture - c#

I am developing a windows service which processes video input and sends results of interest to a separate platform. I do not need to display the frames in this context. I am having a problem getting the correct camera input.
I want to save a Bitmap resulting retrieved from am EMGU capture object. To make sure that the capture is actually reading the video stream, I save the bitmap to a file, as follows:
Mat frame = mCapture.QueryFrame();
Template = frame.Bitmap;
Template.Save("frozen.jpg");
The capture is initialized as follows:
CvInvoke.UseOpenCL = false;
int index = int.Parse(ConfigurationManager.AppSettings["Index"]);
mCapture = new Capture(index);
One test platform is a Lenovo laptop running 64-bit Windows 10, with a built-in camera and a second camera attached through a USB port. The input of interest is the second camera. However, whatever index I use to open the Capture object, the input comes from the built-in camera.
The other platform is a Meego Pad, running 32-bit Windows 10, with the same camera attached. In this case, I simply get blank frames as video input. For both platforms, running the camera application shows the video input as expected. What is the problem with my initialization of the Capture object?
Further Investigation Shows...
First, I was using the wrong index to create the capture, so that created some of the confusion. But more confusion follows.
When I call the QueryFrame() method in an event delegate, as shown in this simple example I successfully retrieve the frame from the camera. Example code looks like this:
Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
{ //run this until application closed (close button click on image viewer)
viewer.Image = capture.QueryFrame(); //draw the image obtained from camera
});
viewer.ShowDialog(); //show the image viewer
When I call the same method in a different thread (in response to a communication event) I get an empty image. On the gripping hand, when I call the method in a timer callback, I get the correct image.
I won't call this closed, because I would still like to know why QueryFrame() acts correctly in some threads, but not in others. However, I can work with this, so the question is now mostly academic.

Related

Is there any default time interval for mediaended event to be fired?

I have a WPF application which dynamically loads video or image files(depending on user's choice, to a MediaElement control.
It is working fine when it is a video and gets the MediaEnded event fired on ending the video.
But when I load an image, the MediaEndedevent is fired within 5 seconds.
Is it a default value? or can I change it programmatically?
Is there any property to change this interval or disable such an option?
Is it possible to make it paused until a specific action?
I have set the following properties as follows
MediaControl1.LoadedBehavior = MediaState.Manual;
MediaControl1.UnloadedBehavior = MediaState.Manual;
MediaElement is a (very thin) wrapper around Windows Media Player (or rather - uses the same framework which is used by Windows Media Player). If you open an image in Windows Media Player - you will see it will "play" it like a slideshow (even for 1 image), for about 5 seconds. That's why you get MediaEnded event in 5 seconds - Windows Media Player plays slideshow with your image for that duration. I doubt there is a way to change this from WPF (because it's behavior of external program\framework, not related to MediaElement itself) and I'm not aware of the way to change this for Windows Media Player (and even if there is such a way - it will have global effect and you probably don't want to modify your clients computer in such a way).
To solve your problem - just don't use MediaElement for displaying images - use something like Image control. If you have really strong reasons to do that - you can pause MediaElement with Pause method after your "slideshow" has been loaded, then it will not fire MediaEnded event. All in all - I cannot imagine any use case where you really have to use MediaElement for images.
You can repeat the media when you load image. Handle MediaEnded event like this:
void me_MediaEnded(object sender, EventArgs e)
{
//play video again
mediaElement.Position = new TimeSpan(0, 0, 1);
mediaElement.Play();
}
Additionally see this. It may help:
https://stackoverflow.com/a/3406857/5675763

How can we run camera as background view in windows phone 8?

I would like to test around a bit with augmented reality.How to get the picture that comes from the camera as background for my view? For what I want to do, I don't need to access the picture, I just need it as background.
You can get the preview-buffer from the camera from the page. We need this to capture a QR-Code without taking pictures with the camera.
Initialize the camera object
_phoneCamera = new PhotoCamera();
_phoneCamera.Initialized += CamInitialized;
In the Initialized - Event. Just create the buffer
private void CamInitialized(object sender, CameraOperationCompletedEventArgs e)
{
_previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int) _phoneCamera.PreviewResolution.Height);
}
Ant then you can take everytime a snapshot of the current view:
//grab a camera snapshot
_phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
_previewBuffer.Invalidate();
And then you can do what you want with this WriteableBitmap (show as background, or whatever).
Or (if I misunderstood your question) here a link with the info, how to add the camera-view to your page (this is also needed for the above solution): How to create a base camera app for Windows Phone 8

Stop preview after photo is taken

My app is supposed to take a photo, then stop there showing the photo, save it in a file and move on to another view.
I use the Microsoft.Devices.PhotoCamera and wait after a photo is taken for CaptureCompleted to take the image and for CaptureImageAvailable to store it and then move on.
Whan happens is: After a picture is taken, I get the camera preview again, which might be confusing for users (though a progress-bar is shown to tell them "wait pls". still not nice) and after 1-2 seconds it shows the taken picture again and moves on to the next view.
Is there a nice way to disable this preview-thing?
If I dispose the PhotoCamera after the first event, it hangs. If I dispose it after the 2nd event, it doesn't seem to make much difference..
Edit:
private void Cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
{
this.imageStream = e.ImageStream;
Deployment.Current.Dispatcher.BeginInvoke(delegate
{
this.image = new BitmapImage();
this.image.SetSource(this.imageStream);
this.PreviewImage.Source = this.image;
// Part where I store the image into a file.
// Part where I navigate to the next view.
});
}
I need that Dispatcher-Thing because setting images and navigating needs the UI-thread but the method runs in a different one.
Also a strange point: it doesn't matter that the PreviewImage lies OVER the canvas with the camera-preview. It is still overwritten as soon as the picture is taken and being processed... ôo
The problem might be that I do many things and the UI is never refreshed. I could add a small sleep (1-5ms) but that would be async again. Also I can't do this completely serparated because the storing part makes a "screenshot" which should contain the final image instead of the camera-preview-thing.
Edit2:
I see the main problem in the capture itself. It takes a picture, shows it for a while, then switches to the camera preview and then back to my shown picture.

OpenCv CvWindow removal

I am trying to get the output of a camera and display it in a c# gui using OpenCvSharp. This is the code I have based on samples. However, this code pops up a second window, CvWindow, which I would like to remove. Is this possible? Removing the CvWindow from my code makes the camera stream not appear.
CvCapture cap= CvCapture.FromCamera(0);
CvWindow win= new CvWindow("camera");
while (CvWindow.WaitKey(10) < 0)
{
IplImage img= Capture.QueryFrame();
Bitmap bmp= BitmapConverter.ToBitmap(img);
pictureBox1.Image = bmp;
}
win.Close();
This is how I display OpenCvSharp video feeds in a Windows Form. http://www.prodigyproductionsllc.com/articles/programming/use-opencv-in-a-windows-form-application-in-c/.
The best way to hide the window is to not show it in the first place. So, remove the line where you construct a new CvWindow. Then, you need to move your while-loop into a new thread and make it continuous with something like "while(true)". If you do not run it on a separate thread, your app will freeze.
Well, it turns out using the Win32 API to hide the window was the way to go. However, you have to make sure to hide parent window of the CvWindow.

C# Screen Capture in Parallel?

I'd like to capture a screenshot of an application's window. I can do this using the below. However, is it possible to do this in parrall or does it have to happen sequentially?
I am currently making use of this:
ScreenCapture sc = new ScreenCapture(); // capture entire screen, and save it to a file
Image img = sc.CaptureScreen(); // display image in a Picture control named imageDisplay
this.imageDisplay.Image = img; // capture this window, and save it
sc.CaptureWindowToFile(this.Handle,"C:\\temp2.gif",ImageFormat.Gif);
Is there any way I can take screen-shots in parallel for two or more application windows?
I have never used that library before but from your code example it seems you are not capturing a specific window but your entire screen.
it seems you want to use the function
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
and pass it the handle of the window of the application you want to capture. It seems you can use that on as many windows as you want with the library it is just a matter of getting the handle of the correct window.
you may want to look into "threads":
http://www.albahari.com/threading/
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
Basically, you should launch concurrent threads to handle a specific work, eg giving the window handle as a parameter for the thread job.

Categories