Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Image in c#

Edit: SOLVED! Please see my answer down below for details. I was unable to find an answer to the original question but I found an alternate solution

This question may be asked somewhere else but I have been searching for days and can't find anything that helps.

Question: I need to convert "Stream" to "image(bgr, byte)" in one go, Is there a way/command to convert directly from System.Drawing.Image.FromStream to Emgu.CV.Image(Bgr, Byte) without converting from stream to image to bitmap to image(bgr, byte)?

Information: I'm coding in c# in Visual Studio 2010 as part of my dissertation project. I am taking a image stream from an IP camera on a network and applying many algorithms to detect faces/extract facial features and recognise an individuals face. On my laptops local camera I can achieve FPS of about 25~ (give or take) including algorithms because I don't have to convert the image. For an IP camera stream I need to convert it many times to achieve the desired format and the result is around 5-8fps.

(I know my current method is extremely inefficient which is why I'm here, I'm actually converting an image 5 times total (even gray scaling too), actually only using half of my processors memory (i7, 8gb RAM)). It does have to be image(bgr, byte) as that is the only format the algorithms will function with.

The code I'm using to get the image:

//headers
using System.IO
using System.Threading;
using System.Net;
//request a connection
req = (HttpWebRequest)HttpWebRequest.Create(cameraUrl);
//gives chance for timeout for errors to occur or loss of connection
req.AllowWriteStreamBuffering = true;
req.Timeout = 20000;
//retrieve response (if successfull)
res = req.GetResponse();
//image returned
stream = res.GetResponseStream();

I have alot of stuff in the background managing connections, data, security etc which I have shortened to the above code. My current code to covert the image to the desired output:

//Convert stream to image then to bitmap
Bitmap bmpImage = new Bitmap(System.Drawing.Image.FromStream(stream));                    
//Convert to emgu image (desired goal)
currentFrame = new Emgu.CV.Image<Bgr, Byte>(bmpImage);
//gray scale for other uses
gray = currentFrame.Convert<Gray, Byte>();

I understand there is a method to save an image locally temporarily but I would need to avoid that for security purposes. I'm looking more for a direct conversion to help save processing power. Am I overlooking something? All help is appreciated.

Thanks for reading. (I will update this if anyone requests any more details) -Dave

like image 762
Hughsie28 Avatar asked Mar 24 '15 13:03

Hughsie28


People also ask

How do you convert an image to binary?

BW = im2bw( I , level ) converts the grayscale image I to binary image BW , by replacing all pixels in the input image with luminance greater than level with the value 1 (white) and replacing all other pixels with the value 0 (black). This range is relative to the signal levels possible for the image's class.

Can images be converted to binary?

Images also need to be converted into binary in order for a computer to process them so that they can be seen on our screen. Digital images are made up of pixels . Each pixel in an image is made up of binary numbers.

How does an image converter work?

An image converter is a kind of file converter that converts one image file format (like a JPG, BMP, or TIF) into another. If you're unable to use a photo, graphic, or any kind of image file the way you'd like because the format isn't supported, this kind of software can help.


1 Answers

You've got a couple potential bottlenecks, not the least of which is that you're probably jpeg decoding the stream into an image and then converting that into a bitmap and then into an openCV image.

One way around this is to bypass the .NET imaging entirely. This would involve trying to use libjpeg directly. There's a free port of it here in C#, and IIRC you can hook into it to get called on a per-scanline basis to fill up a buffer.

The downside is that you're decoding JPEG data in managed code which will run at least 1.5X slower than equivalent the C, although quite frankly I would expect network speed to dwarf this immensely.

OpenCV should be able to read jpeg images directly (wanna guess what they use under the hood? Survey says: libjpeg), which means that you can buffer up the entire stream and hand it to OpenCV and bypass the .NET layer entirely.

like image 70
plinth Avatar answered Sep 23 '22 05:09

plinth