I'm learning Emgu.CV, I have successfully run the example, I prefer to read the image from byte array rather than direct from file, and also I prefer to save the result to byte array rather than to save directly to file..
any body can help?
Many thanks
var _img = CvInvoke.Imread(_jpg); // HOW TO READ FROM BYTE ARRAY
var _fd = new Emgu.CV.CascadeClassifier(_face_classifier);
var _img_gray = new UMat();
CvInvoke.CvtColor(_img, _img_gray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
foreach (Rectangle _face in _fd.DetectMultiScale(_img_gray, 1.1, 10, new System.Drawing.Size(20,20)))
{
CvInvoke.Rectangle(_img, _face, new Emgu.CV.Structure.MCvScalar(255, 255, 255));
}
_img.Save("result.jpg"); // SAVE TO BYTE ARRAY
I've understood your question to be in fact twofold: Converting an image to a byte[], and saving a byte[] to a file. I'll address the easier and latter first:
byte[] to Fileusing(FileStream fs = new FileStream("OutputFile.dat",FileMode.OpenOrCreate))
{
BinaryWriter bs = new BinaryWriter(fs);
bs.Write(byteBuffer);
}
byte[] from Filebyte[] byteBuffer;
using (FileStream fs = new FileStream("InputFile.dat", FileMode.OpenOrCreate))
{
BinaryReader br = new BinaryReader(fs);
// Where 0xF0000 is calculated as Image Width x Height x Bit Depth
byteBuffer = br.ReadBytes(0xF0000);
}
Now for the second part; I'm assuming you are familiar with pixel formats and how the true image structure of byte[,,] (or any other depth) is serialized to a single dimension byte[]. If not please do ask.
Image from byte[]In the following example, the byte[] is created rather than loaded as above, the source of the byte[] is irrelevant so long as it is a correct serialization of a known image dimension and depth.
int width = 640; // Image Width
int height = 512; // Image Height
int stride = 640 * 3; // Image Stide - Bytes per Row (3 bytes per pixel)
// Create data for an Image 512x640 RGB - 983,040 Bytes
byte[] sourceImgData = new byte[0xF0000];
// Pin the imgData in memory and create an IntPtr to it's location
GCHandle pinnedArray = GCHandle.Alloc(sourceImgData, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
// Create an image from the imgData
Image<Rgb, byte> img = new Image<Rgb, byte>(width, height, stride, pointer);
// Free the memory
pinnedArray.Free();
Image to byte[]// Convert the source Image to Bitmap
Bitmap bitmap = sourceImage.ToBitmap();
// Create a BitmapData object from the resulting Bitmap, locking the backing data in memory
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// Create an output byte array
byte[] destImgData = new byte[bitmapData.Stride * bitmap.Height];
// Copy the byte array to the destination imgData
Marshal.Copy(bitmapData.Scan0,
destImgData,
0,
destImgData.Length);
// Free the memory
bitmap.UnlockBits(bitmapData);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With