WriteableBitmap used in Mat.Mat constructor (Emgu.CV) - wpf

what I'm trying to do is using an let say pre-loaded image within Emgu.CV.Mat. It offers an constructor where its possible to specify a filename. But my image is already loaded (and manipulated) within the code, therefore it makes no sense to save the file here just to load it again.
I'm working within a WPF Environment and therefor have my image currently as a WriteableBitmap. Furthermore I found couple of hints (e.g. here) how to convert an Mat to WriteableBitmap. But what I'm looking for is the other way round, using a WriteableBitmap in order to Emgu.CV stuff with it.
According to Emgu CV 2.x it was possible to use a Bitmap but it seams that this feature was removed in Emgu CV 3.x.
Any ideas how to do it in Emgu CV Version 3?

I have been experimenting in this area for a work related application. To effectively use a WriteableBitmap with a Mat or an Image<,> you need access to the raw pixels.
Because I have images coming from hardware as well as from creating a RenderTargetBitmap what I found works for me is to allocate a buffer using HGlobalAlloc that is the proper size and use RenderTargetBitmap.CopyPixels() to move the data to this buffer. I also create an Image using an IntPtr to this data. Once I am done with my manipulations I use a WiteableBitmap.WritePixels() to put the image back into the WriteableBitmap.
Here is also a solution from Emgu: BitmapSourceConverter
Doug

Related

Writing text on a WriteableBitmap

I have searched around and found that most of the answers convert a WriteableBitmap to a System.Drawing.Bitmap. I am currently thinking of using WriteableBitmapEx's Blitz() function to overlay a bitmap with text onto the existing bitmap, but I don't really find functions that return WriteableBitmap.
Is there a easy way to draw text?
I managed to use the WriteableBitmapEx DrawString() method to achieve this directly.
Unfortunately it is not inside the Nuget Packages so it has to be compiled from the source of another project:
https://writeablebitmapex.codeplex.com/discussions/579148
https://fastwpfgrid.codeplex.com/SourceControl/latest#WriteableBitmapEx/LetterGlyphTool.cs

Generating tiles from BitmapImage

I am generating tiles from very large images to use with leaflet. I have a working solution that uses System.Drawing.Bitmap and loads the image from file. I already have the image in memory as a BitmapImage however, and would like to reuse it for memory purposes since these images can be very large. But I can not find a good way of doing this with BitmapImage.
What I basically need is something equivalent of
GraphicsHandle.DrawImage(sourceImage, destinationRect, sourceRect, GraphicsUnit.Pixel);
I need to crop and resize a part of the image, and draw this onto another image in a specified location and then save this to file. What is the best way of doing this using BitmapImage?

What is the best approach to render charts in WPF?

What is the best approach to render charts and then save them on a hard drive for further distribution using WPF?
I found a number of ways to accomplish this by using the following types:
DrawingVisual - creating a object of this type and then rendering graphics on its context;
Shape - deriving from the Shape class and then overriding its DefiningGeometry property where the actual rendering is happening;
PathFigure - adding LineSegment-s to an instance of this class and then adding this instance to a Canvas;
Adorner - deriving from it and then overriding its OnRender method;
WritableBitmap - rendering on it and then adding the bitmap to a Canvas.
Of course I'm going to write an app to test how fast each of these will be. But can anybody tell me:
whether am I on the right track?
are there any other means to do such rendering?
which one of them is the best in
terms of performance?
It all depends on your actual usage, in your case you mention saving on the hard drive for "further distribution" - I'm going to assume you are saving them as an image (jpg or png) and not as wpf objects (xaml).
You should consider if WPF is the right tool for the job, WPF is a UI framework and not a generic image processing library, it may be best to use something else entirely for generating images.
For a reasonable number of points your performance bottleneck will be encoding the image and saving it to disk - not actually rendering it - so you should choose the method that is easier for you to code.
All the articles about high performance WPF charts are a: about charts with 10,000 points and more (because that is where the performance problems are), b: about charts you display in your GUI (because otherwise you can use an image processing library to create the bitmap) and c: charts that change all the time (so they work nicely with data binding) - there's a reason why they don't talk about saving charts to disk.
For a very large number of points:
The fastest way to draw in WPF is to inherit from FrameworkElement (not Adorner) and override OnRender.
When the data changes often it is recommended to use multiple DrawingVisual objects because then you don't have to re-render everything when one value change - but this is not relevant for you since the image won't change after you save it anyway.
WritableBitmap is used for raw bitmap access, you use it when you decide to give up on all the nice layout and drawing WPF gives you because you can't take the overhead, if this is the case you should re-read my first point above.
So, to summarize, you are asking the wrong question :-) if you need to save images to disk than either the WPF rendering speed is not your bottleneck or you shouldn't be using WPF to begin with. If you do use WPF just pick whatever is easiest for you to code.
BTW: Adorners are used to display "floating" elements above the normal UI, you can use them for tooltip-like features but not for the main chart rendering (and you probably don't want them at all since your main usage is saving the image to disk), FrameworkElement is the base class you are looking for.

How should I use .ico files in a Winforms Application?

I'm developing a WinForms c# 3.0 application. Our designer created quite a lot of .ico files containing all the needed art. The choice of .ico was made because quite often, the same image is needed in several places in different dimensions.
Now, it seems .ico files are really annoying to use in visual studio. The only way to use those images seems to be through images list (which aren't supported by all controls).
Compared to other resources, you can't write this :
foo.Image = global::RFQHUB.RFQHUBClient.Properties.Resources.foo; // Cannot implicitly convert type 'System.Drawing.Icon' to 'System.Drawing.Image'
Here are the options I'm considering :
create ImageLists of all possible sizes referencing all my icons in my main window. Link these ImageLists from other windows and find a way to export Image objects from the ImageList when I can't use it directly ; since ImageList contains a Draw() method, this should probably be possible.
convert all the x.ico I've got in several x16.gif ...x48.gif, and use those through resources.
I'd be interested to know if some people have been successfully using .ico resources in a Winform application. In so, how did you set up things ?
ICO isn't quite an obsolete format, but it's close. It's still useful for your application icon, but for almost everything else, it's better to use an ImageList for each size that you need. And it's much faster to populate an ImageList from a bitmap that contains multiple images layed out in a grid.
You also want to use an Alpha channel transparency in your bitmaps to get the best result, so storing them as .PNG files in your resources is the best way to go, since PNG supports an alpha channel. ICO and GIF files support only single bit for transparency - every pixel is either fully opaque or fully transparent. An 8 bit alpha channel for transparency looks much nicer.
If you can send your artist back to the drawing board then you should do so, and have him/her do full anti-aliased images with alpha. If you can't, then I suggest that you write a small program to convert all of your icon files into bitmaps suitable for loading into ImageLists.
Convert the images into PNG. Point. Whoever decided to use .ico files to start with should get talked to in private - the argument holds no ground.

Working with bitmap in WPF

Is there any sane way to work with bitmaps in WPF? I'd like similar functionality as System.Drawing.Bitmap: be able to load image from file and get and set the color of particular pixels.
I know about WriteableBitmap, but I don't want to work with Arrays of gods-know-what (I couldn't find that on MSDN), or pointers. I just want to read or write pixel, it shouldn't be that hard!
I also know that I can use Interop to work with System.Drawing.Bitmap in WPF, but that's not nice either.
Here's something on how to do get pixel info from a System.Windows.Media.Imaging.BitmapImage.
Although, if you'd prefer to work with a System.Drawing.Bitmap you might want to add a reference to System.Drawing.dll and use it instead.
Here's a somewhat-related forum post on image manipulation in WPF.

Resources