2

I am developing a program for image processing, and I need to save some pictures from a video and do some processing on them. When dealing with 1 picture it doesn't really take time, But when I'm dealing with 100 pictures it makes difference I'm saving the files to my hard disk, and that's why it takes time the thing is, the function I'm using is a ready made function and it only accepts (file name) the function is really complicated so i cannot build my own function ( if that's what you are thinking )

I'm thinking of 2 things right now and would like to have your opinions about them:

  1. change the input of the function, but how ? is there a way to change this input from a ( file name ) to an array which holds these pictures ?
  2. save the file to ram. but how to save files to ram by names, and be able to use them as ( file name ) in the function ?

I appreciate your help , thanks so much

this is my code but i still have problems:

            Capture video = new Capture("c:\\5.avi");
            Image<Bgr, Byte> Imageframe ;

            Dictionary<string, MemoryStream> dict = new Dictionary<string, MemoryStream>();

                Imageframe = video.QueryFrame();
                Bitmap bmp = Imageframe.ToBitmap();
                dict.Add("mypicture.png", new MemoryStream());
                bmp.Save(dict["mypicture.png"],imageformat.png);    

its saying imageformat does not exist in the context

and this is the function im using :

Image<Bgr, byte> result;
                 result = DrawMatches.Draw("box.png", "box_in_scene.png", out matchTime,i); 

3 Answers3

5

You could save to RAM (in the loosest of senses) using a MemoryStream. If you wanted to name them you could use a Dictionary<string,MemoryStream>, such as:

Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>();

dict.Add("mypicture.png",new MemoryStream());

image.Save(dict["mypicture.png"]);

However you'll need to write cleanup code for these streams and this doesn't take into account that eventually you could use up all the physical RAM and then start going into the paging file, which is disk based anyhow.

As an aside, if this is on a server you control and you're not releasing this software to end users you could get a RAM disk drive (plenty around just Google) that uses physical RAM as an actual disk available to Windows. Then you could just load/save to that.

Very very rough EmguCv variant:

// Global dictionary of memory streams
Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>();

// Add image memory stream to dictionary
dict.Add("mypicture.png",new MemoryStream());

// Get bitmap from EmguCv
Bitmap bmp = image.ToBitmap();

// Save bitmap to image memory stream
bmp.Save(dict["mypicture.png"],ImageFormat.Png);

// Get bitmap from memory stream
dict["mypicture.png"].Seek(0,SeekOrigin.Begin);
Bitmap new_bmp = Image.FromStream(dict["mypicture.png"]);

// Convert bitmap to EmguCv image
Image<Bgr,Byte> new_img = new Image<Bgr,Byte>(new_bmp);
Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • I might be wrong, but is the cleanup of the memorystreams not done with the garbage-collector at the end of the application? – Tomtom Jan 28 '13 at 12:39
  • @tomtom Yeh you are right as they're marked IDisposable, but I think it's bad form to rely on that :) – Lloyd Jan 28 '13 at 12:40
  • Look here and read answer by Jon Skeet http://stackoverflow.com/questions/234059/is-a-memory-leak-created-if-a-memorystream-in-net-is-not-closed – Likurg Jan 28 '13 at 12:40
  • I'm terrible sorry I didn't provide my code. Thank you Lloyd for your fast reply, but I'm again having a problem. Here is my code: Image Imageframe ; Dictionary dict = new Dictionary(); Imageframe = video.QueryFrame(); dict.Add("mypicture.png", new MemoryStream()); Imageframe.Save(dict["mypicture.png"]); the thing is, Imageframe is byte type, and its giving me an error when im doing the last line of the code ( overloaded ) im really lost what to do , and I appriciate your help – Mahmood Shahin Jan 28 '13 at 13:01
  • What methods does Image have (I'm assuming looking at this this isn't the standard Image class)? – Lloyd Jan 28 '13 at 13:09
  • Actually i'm using EmguCv If you are familiar with it the method that the file gets into is DrawMatches which draws key points of features in the image and compare it with another image – Mahmood Shahin Jan 28 '13 at 13:11
  • I'm so sorry, im new to this :S – Mahmood Shahin Jan 28 '13 at 13:14
  • @MahmoodShahin Looking at their docs `Save()` only works on files. You'd have to use `ToBitmap()` and instead store the `Bitmap` to the stream. Not sure how you'd get it back from the `Bitmap` as I'm not directly familiar with the library. – Lloyd Jan 28 '13 at 13:20
  • @MahmoodShahin Actually seems `Image` supports `Bitmap` in the constructor. – Lloyd Jan 28 '13 at 13:21
  • you are right, it supports bitmap but in your code above, what is ( image ) data type? – Mahmood Shahin Jan 28 '13 at 13:25
  • The EmguCv image type `Emgu.CV.Image` which I presume is what you're using given what you've said. – Lloyd Jan 28 '13 at 13:28
  • i updated my code up there ,im still having a problem to save :( – Mahmood Shahin Jan 28 '13 at 13:42
1

Try to use MemoryStream for work with memmory like with file, and try to save files on harddisk by thread

Likurg
  • 2,742
  • 17
  • 22
0

I understand you are using a DLL you dont have the source for. You might be able to load it into a reflector, and modify it to take the image as an argument instead of the file name. I used Red Gate's reflector in the past and it worked for me: http://www.reflector.net/

Vall3y
  • 1,181
  • 8
  • 18