Using static image in EmguCV capture - emgucv

I am currently using
Capture grabber = new Emgu.CV.Capture(#"M2U00253.wmv");
grabber.QueryFrame();
I want to know that how can I use an image file(.jpg) instead of the video file(.mpg)?

To load a static you can use the following method,
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>("MyImage.jpg");
refer to this link for more information.

Related

Copying an Image to PictureBox using ROI EmguCV C#

I want to copy an image from camera to anothe imagebox using ROI. I have searched a lot references about ROI but it still doesn't work. Anybody can help me, please ?
These are what I have done
Image<Bgr, Byte> sourceImage1 = CaptureCam1.QueryFrame();
SourceImageBox.Image = sourceImage1;
ImageCopy = new Image<Bgr, Byte>(Template_Box.Width, Template_Box.Height);
sourceImage1.ROI = new Rectangle(SourceImageBox.Location.X, SourceImageBox.Location.Y, SourceImageBox.Width, SourceImageBox.Height);
sourceImage1.CopyTo(ImageCopy);
Template_Box.Image = ImageCopy;
You should use it like below:
sourceImage1.ROI = new Rectangle(SourceImageBox.Location.X, SourceImageBox.Location.Y, SourceImageBox.Width, SourceImageBox.Height);
Template_Box.Image = sourceImage1.clone();

EmguCV capture frames QueryFrame()

I'm using emgu cv 3.0.0 and I would like to capture frames from a USB cam.
Unfortunately, I get an error while calling Image<Bgr, Byte> image = capture.QueryFrame();
It says, I can't convert from Emgu.CV.Mat to Emgu.CV.Image.
Try this line of code
Image<Bgr, Byte> image = capture.QueryFrame().ToImage<Bgr,Byte>();
In cv v 3.0.0 most methods use Mat for image representation.
You can try this to get the frame from the camera:
Mat frame = new Mat();
_capture.Retrieve(frame, 0);
For conversion to gray:
Mat grayFrame = new Mat();
CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
Also you can see how it works in examples here.

EmguCV bug using StereoBM class AccessViolationException

Hi i tried compute disparity map using EmguCV, but there must be some bug when i try use StereoBM class, always when i call function FindStereoCorrespondence i will receive this error:
System.AccessViolationException: Attempted to read or write protected memory.
My code is simple:
private Image<Gray, byte> Computer3DPointsFromStereoPair0(Image<Gray, byte> left, Image<Gray, byte> right)
{
Size size = left.Size;
Image<Gray, short> disparityMap = new Image<Gray, short>(size);
using (StereoBM stereoSolver = new StereoBM(Emgu.CV.CvEnum.STEREO_BM_TYPE.BASIC, 0)){
stereoSolver.FindStereoCorrespondence(left, right, disparityMap);
}
return disparityMap.Convert<Gray, byte>();
}
I think it can be somewere in OpenCV dll's...
Please if somebody know how to slove that problem!

trying to load a bitmap, but xna want´s a Texture2D

I'm currently trying to load a simple bitmap using XNA but I get the following error:
Error loading "Maps\standard". File contains Microsoft.Xna.Framework.Graphics.Texture2D but trying to load as System.Drawing.Bitmap.
code:
public Bitmap map;
public void load(Game game, String image) {
path = image; //path to image
map = game.Content.Load<Bitmap>("Maps/"+path);
sizeX = map.Width;
sizeY = map.Height;
}
You want the below:
map = game.Content.Load<Texture2D>("Maps/"+path);
The way XNA works is that there is a content pipeline, which takes inputs (like your bitmap image) and produces outputs (the Texture2D), which is in a different format to the input.
XNA works with a Texture2D object when displaying images.
Now, I just use the C# standard
Bitmap bmp = (Bitmap)Bitmap.FromFile(path);

Convert from Bitmap to MIplImage or IplImage c#

I have an algorithm in created in c++ to make background subtraction, and i want to call it from c# with an argument "IplImage" using dll(extern). The problem that i acquire the camera stream in c# and i have the frame as image (bitmap).
How could i convert the bitmap to IplImage to send it in c++ and vice versa to retrieve the frame treated?
Many thanks.
Using Emgucv, you can try
Bitmap bitmap = ....
Emgu.CV.Image<Bgr, Byte> img = new Image<Bgr, byte>(bitmap);
Emgu.CV.Structure.MIplImage = img.MIplImage;
to send it in C++ method you can pass it as IntPtr object
IntPtr r = IntPtr.Zero;
System.Runtime.InteropServices.Marshal.StructureToPtr(img, r, false);
// call your c++ method
...
also see detail about Marshal

Resources