Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi. I want to convert a 32 bit frame to 24 bit to pass it to AForge filters. I am doing it using graphics as follow:

bitmap bmap = new Bitmap(640, 480 , System.Drawing.Imaging.PixelFormat.Format32bppRgb);
clone = new Bitmap(bmap.Width, bmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);


using (Graphics gr = Graphics.FromImage(clone))
{
gr.DrawImage(bmap, new System.Drawing.Rectangle(0, 0, clone.Width,
clone.Height));
}

but it takes large amounts of memory, is there any other way to do it?

Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 16-Dec-13 0:37am    
What, too much memory for 640x480? Are you kidding? I hope you want to clone it frame by frame, one at a time. If so, you method is reasonable enough; and the memory consumption should be quite moderate.
—SA
niloufar.M 16-Dec-13 2:07am    
Dear SA
Thanks for your reply, my project is real time and I'm capturing from kinect, so it is 30 FPS.
Sergey Alexandrovich Kryukov 16-Dec-13 2:11am    
Why would you need to convert on the fly? How about pre-mastering it? Anyway, then your problem could be performance, not memory consumption...
—SA
niloufar.M 16-Dec-13 2:35am    
Maybe, I am doing some process which I need it to be real time, so do you have any idea?
Sergey Alexandrovich Kryukov 16-Dec-13 10:10am    
Not really. I think your method is good. But, if its performance is insufficient (did you try?), maybe the System.Drawing is not fast enough for your purpose...
—SA

1 solution

It looks like you have a performance leak. Here is what happens: you to accomplish some task with AForge.NET, but it requires different bit format, so you also convert to the 24-bit pixel format. I think your steps are quite reasonable, but you still get a bit less performance than requited by real-time video. Does it mean that you cannot improve performance (without resorting to native code)? I don't think so.

Here is what happens: you really do at least some of double work. Internally, System.Drawing does a number of things: it reads data, interprets it, put bits to canvas, something like that. And AForge.NET later does at least part of it again. Logically, the resolution would be to remove redundancy. Here are the ideas I have:

  1. Exclude reformatting pixels by looking at the source code of AForge.NET. Modify or update AForge.NET with the methods you use now, but working with your pixel formats. I means considerable job, but I'm sure it's quite possible. You just copy and slightly modify the code.
  2. You say that you just need to shift pixels. It sounds very simple. Most likely, you can work it out without using AForge.NET. You need to lock the pixels (specialized data pinning for pixels available in System.Drawing. Here is how:
    http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits%28v=vs.110%29.aspx[^].

    The MSDN articles on two methods referenced above comes with code samples which can help you to understand how to use them. You have two possibilities to improve performance by removing redundant processing:
    a) you can have two bitmaps with different pixel format, to change pixel format of the target bitmap, but this time you can do your image transformation in the same "lock bit session", avoiding redundant marshaling of pixel back and forth, which should be a considerable overhear;
    b) you can work with the same pixel format, as before, the 32-bit one; in this case, you can do all the transformation on the same set of bits, marshaling the image to data and data to image only once.


Please try. Good luck!

—SA
 
Share this answer
 
v4
Comments
niloufar.M 17-Dec-13 3:55am    
Dear SA, Thanks for your kind help. I'm not using aforge for shifting pixel I'm using it for filtering. for pixel shifting I load all the pixels in an byte array size 4 times the original bitmap then I'm changing each pixel value to the value of the pixel I want to shift it to. then I want to change the bitmaps format to 24bpp and pass it to Aforge filter. I will try your solution as soon as I get time.
Sergey Alexandrovich Kryukov 17-Dec-13 3:59am    
I see. Anyway I hope you got the idea. Perhaps then the option #1 would be the closest to what you need. You can make some experimental code out of AForge.NET to estimate performance. Yes, it needs work...
—SA
niloufar.M 17-Dec-13 4:18am    
thanks a lot :)
Sergey Alexandrovich Kryukov 17-Dec-13 4:25am    
Please don't forget to accept the answer formally if you find it reasonable or useful.
—SA
niloufar.M 18-Dec-13 11:03am    
Dear SA
I was thinking I can clear the memory after each iteration, so I think it is going to be more efficient. can you help me by any code fro clearing memory? thanks in advance :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900