I would be most grateful if anyone could provide some C# emgu sample code regarding accessing cvb snap image in the form of an EMGU Cv.Mat.
Is there not some lib agnostic way of doing this? For example, if cvb could stream a snap into a memory buffer encoded as a PNG or bitmap, other libs could pic that up in an agnostic way, for example imdecode.
In this way we could save to memory in much the same way you can save to disk from one lib and load to disk from another lib and all the problems are taken care of.
I haven’t been having much success with trying to model the C# library bridge on the C++ lib bridge, or by adapting ideas found on the forum for Cvb pixel access.
I haven’t used C# much since 2011, which doesn’t help. The legacy application I am trying to modify is also based on an older version of Cvb, before the number of dlls was consolidated down.
- I first tried to see if I could get the basic types right, following the core essentials of the C++ style no_copy bridge without any checking (as I know it is 1 channel greyscale to greyscale):
static unsafe Emgu.CV.Mat bridge(Cvb.Image.IMG source, Emgu.CV.Mat mat)
{
int width = Cvb.Image.ImageWidth(source);
int height = Cvb.Image.ImageHeight(source);
Size size;
size.Width = width;
size.Height = height;
IntPtr sourceBase;
int sourceYInc;
int sourceXInc;
MIplImage retval = CvInvoke.cvCreateImageHeader(size, Emgu.CV.CvEnum.IplDepth.IplDepth_8U, 1);
Cvb.Utilities.GetLinearAccess(source, 0, out sourceBase, out sourceXInc, out sourceYInc);
CvInvoke.cvSetData(ref retval, ref sourceBase, ref sourceYInc); // << - tried a variety of approaches here.
- I also tried accessing the individual pixels of the cvb image, and using that data to reconstruct an image in the Emgu library.
It seems to work for the first few thousand pixels but ultimately crashed my debugger application itself. Very slow up to that point.
// static unsafe void CopyPixelsWithValue(Cvb.Image.IMG source, Emgu.CV.Mat mat)
static unsafe Emgu.CV.Mat CopyPixelsWithValue(Cvb.Image.IMG source, Emgu.CV.Mat mat)
{
int width = Cvb.Image.ImageWidth(source);
int height = Cvb.Image.ImageHeight(source);
IntPtr sourceBase;
int sourceYInc;
int sourceXInc;
Cvb.Utilities.GetLinearAccess(source, 0, out sourceBase, out sourceXInc, out sourceYInc);
for (int y = 0; y < height; y++)
{
byte* pSrcLine = (byte*)sourceBase + y * sourceYInc;
for (int x = 0; x < width; x++)
{
var srcVal = *(pSrcLine + x * sourceXInc);
Matrix<byte> mask = new Matrix<byte>(2056, 2464);
mask.Data[y, x] = 1;
mat.SetTo(new MCvScalar(srcVal), mask);
}
}
return mat;
}
Any help much appreciated!