Augmented Reality – BallGame v2 video

Ok, so here is another video of my game.. I have added quite a few things since the previous one. A new model for the ball, textures, obstacles and some game logic. If you want to watch it live and play, i’ll be presenting it tomorrow at Microsoft’s Innovation Day.

 

Share/Bookmark
Posted by jupiter | 2 comment(s)
Filed under: ,

Speech Recognition with C# – Dictation and Custom grammar

I’ve been working on a project lately (i will post more about it in a couple of weeks) where i needed to add speech recognition. Thanks to Managed Wrapper for the Speech API (included in .NET since v3), it only takes a few lines of code to add the functionality you want. Of course, the framework provides the classes, etc to write more complex speech recognition software if you need to.  If you are interested for the reverse operation – Text to Speech – you can check a previous post here: Text to Speech using C#.

First of all you need to add a reference to the System.Speech assembly:

Add Reference: System.Speech

So, the simplest thing we can do is the following:

First of all, we create a new instance of SpeechRecognitionEngine and tell the engine to get the input from the default audio device. We can also set the input to a .wav file or an audio stream. Then we have to load a grammar. For dictating text, we use an instance of the DictationGrammar class. If we only need to recognize specific specific words, we need to define a custom grammar, more on that later. The SpeechRecognitionEngine.Recognize() is a synchronous method, meaning that will block till you speak or the time defined in the constructor elapses. It performs a single recognition and then returns. We can then iterate through recognized words and process them as we want. A very helpful property of the RecognizedWordUnit is the Confidence which returns a float (between 0 and 1) with the measure of the engine certainty as to the word correctness.

Since the above example won’t be used in (almost) any real application due to the blocking call, let us see an asynchronous version of the above example:

This time, the recognition will continue until we explicitly call the RecognitionEngine.RecognizeAsyncStop() and not immediately after the first recognition. If you only need a single recognition, change the parameter to RecognizeMode.Single. I have use a lambda expression for handling the recognized words. This is the equivalent code if you’d like to use a typed method:

Now consider a scenario where we need to control a program with our voice. We don’t need the full dictation grammar for it, only the available commands, like “Open”, “Close”, “Start”, “Delete”, … . In this case we can create a custom grammar. This can be a very complex thing to do but i’ll just show the basics here.

So what this function basically does, is create a list of alternative items for an element of the grammar and then build the grammar. First we create the list of options (“Calculator”, “Notepad”, …) and then we append it to our grammarBuilder. A GrammarBuilder is used to create grammar objects programmatically. So this grammar will understand the following type of phrases:

“Start [choice]”

where [choice] is one of the 4 string.

You can give a friendly name to your grammar if you want, but you don’t have to. Now we can load this grammar to the recognition engine and start the recognition as in the previous examples:

That’s all for now. Here are a couple of links in MSDN with some useful info:

  1. How to use GrammarBuilder class – Also has a nice example
  2. SpeechRecognitionEngine class

You can download a sample project i made with the code above:

zip_6DD4E0831_5AD3E58B[1]

Download

Share/Bookmark
Posted by jupiter | 5 comment(s)
Filed under: , ,

Παρουσίαση Web Services, WCF στο ΠΜΣ Πληροφοριακά Συστήματα ΟΠΑ – Τεχνολογία Λογισμικού

Σήμερα έκανα μια μικρή παρουσίαση στο Μεταπτυχιακό Πληροφοριακών Συστημάτων του Οικονομικού Πανεπιστημίου Αθηνών στα πλαίσια του μαθήματος Τεχνολογία Λογισμικού. Το πρώτο μέρος που ήταν μια εισαγωγή στο .NET και την C# έγινε από τον Άγγελο Μπιμπούδη. Η  δική μου παρουσίαση αφορούσε ανάπτυξη SOA εφαρμογών με δημιουργία Web Services μέσω του WCF. Δυστυχώς δεν είχα πολύ χρόνο στην διάθεση μου για να εμβαθύνουμε περισσότερο. Ανεβάζω την παρουσίαση αλλά όχι και το απλό demo που έδειξα. Αντί αυτού προτείνω να κατεβάσετε περισσότερα demos που είχα δείξει στο traning για το Imagine Cup. Παραθέτω και το link προς το zip με τα demos παρακάτω.

Μπορείτε να βρείτε μερικά ακόμα πράγματα σε προηγούμενα posts μου.

 

 

PowerPointLogo[1] zip_6DD4E083[1]

Presentation

IC10 Training WCF Demos

Share/Bookmark
Posted by jupiter | 1 comment(s)
Filed under: ,

Augmented Reality Game - Video

The first time i read about Augmented Reality, i was amazed by all the great things you could create. Merging the real world with virtual objects is fascinating. The last couple of weeks I've been playing with the Goblin XNA platform and here is a video of my first AR game based on it. The project is far from complete. More info (and videos) will be posted here the following weeks.

Share/Bookmark
Posted by jupiter | 11 comment(s)
Filed under: ,

Get client’s IP in WCF 3.5

If you want to get the client’s IP address in WCF 3.5 you just need these 4 lines of code (well it would be better if you could just get it in one but this isn’t hard anyway):

If you need the client’s port number as well, it can be found in endPrp.Port.

Share/Bookmark
Posted by jupiter | 3 comment(s)
Filed under:

Creating an Image Processing Library with C# – Part 2

Creating an image Processing Library – Part 1

In the first post we covered the basics of Convolution operation and how it can be applied on an image. I posted an implementation (SafeImageConvolution) of the algorithm together with some helping classes we needed and the Sharpen filter. In this post, I'm going to show a much more efficient implementation of the algorithm. If you apply the Sharpening Filter from the previous post, using the SafeImageConvolution function, on a 1024x768, you will notice that it takes 6-7 seconds for the operation to complete. That’s, at least, unacceptable. The cause of this delay are the two GDI+ functions: Bitmap.GetPixel() and Bitmap.SetPixel().

So, here is a new implementation of the same algorithm. This time, i use pointer arithmetic to iterate through the image.

Let’s see some important points on the above code. First of all, the Bitmap.LockBits() locks the Bitmap to the memory and returns a BitmapData class which contains, among others, the following 2 properties that are later used: Scan0 and Stride. Scan0 is the address (IntPtr) of the first pixel in the bitmap. Stride is slightly more complicated to understand, but basically is the width of each row of pixels, rounded up to a 4-byte boundary. See the following image to better understand it:

Stride and Scan0

If we have a 24bpp (= 3 Bytes per pixel) image with a width of 130 pixels then the actual pixel data on each row are 130 * 3 = 390 bytes, but the stride will be 392 (rounded up to 4-bytes). So we have 2 unused pixels on each row. Based on the above, to get the pixel at (X, Y) we can use the following formula:

pixel = Scan0 + (Y * Stride) + (X * PixelSize);

where PixelSize is the size of each pixel in bytes. This leads me to the second thing i want to explain on the code above.

You can see that i use the methods Bitmap.GetPixelSize() and Bitmap.HasAlpha(). As you may now, the Bitmap class doesn’t include these methods, i have implemented them as Extension methods and they can be found in the BitmapExtensions.cs file.

One last important thing to notice is that the pixels are saved in a BGR format, which means that Blue is the first byte, Green the second and Red the third (with a possible 4th byte for alpha channel).

You can find the whole source code, together with a Windows Forms demo application that uses the library. Feel free to send me any comments you have, suggestions or bugs that you may find.
Download 

Download

Share/Bookmark
Posted by jupiter | 4 comment(s)
Filed under: ,

WCF Part 2, Windows Azure and SQL Azure presentation at ImagineCup 2010 Training

Σήμερα στο training του ImagineCup συνέχισα τις πρώτες ώρες με μερικά ακόμα demos για WCF. Στην συνέχεια προχωρήσαμε σε μια εισαγωγή στο Windows Azure και στο SQL Azure. Τα demos του WCF βρίσκονται στο zip που είχα ανεβάσει στο χτεσινό μου post. Ανεβάζω και το demo του Azure που έδειξα σήμερα. Το μόνο που πρέπει να αλλάξετε για να τρέξει είναι το username και το password στο ConnectionString για την βάση δεδομένων που βρίσκεται στο web.config.

Όσοι θέλετε να εγγραφείτε για να πάρετε token (θυμίζω μέχρι το τέλος του χρόνου) για Azure και SQL Azure πηγαίνετε εδώ: http://go.microsoft.com/fwlink/?LinkID=129453 Λογικά θα σας έρθουν τα tokens σε διάστημα 3-4 ημερών.

Για να αρχίσετε το development πρέπει να κατεβάσετε τα Tools & SDK. Η τελευταία έκδοση είναι November CTP και βρίσκεται εδώ:http://www.microsoft.com/downloads/details.aspx?FamilyID=6967ff37-813e-47c7-b987-889124b43abd&displaylang=en

Για να βλέπετε τις βάσεις στο SQL Management Studio κατέβαστε την 2008 R2 November CTP έκδοση από εδώ: http://www.microsoft.com/downloads/details.aspx?FamilyID=c772467d-e45b-43e1-9208-2c7b663d7ad1&displaylang=en (υπάρχει δυνατότητα για download μόνο του SSMS ή και του Server μαζί.. Σας χρειάζεται μόνο το Management Studio)

Παραθέτω και μερικά χρήσιμα links:

  1. Πάρα πολύ υλικό και tutorials για το πώς να ξεκινήσετε απ’το channel 9: http://channel9.msdn.com/learn/courses/Azure/
  2. Η κεντρική σελίδα του Azure: http://www.windowsazure.com
  3. Διαχείρηση των Project σας (Hosted Service, Data Storage, SQL Azure, …): https://windows.azure.com/

Download

Share/Bookmark
Posted by jupiter | 1 comment(s)

WCF Presentation at ImagineCup 2010 Training

Σήμερα έκανα ένα training για το Windows Communication Foundation στα πλαίσια του ImagineCup 2010 Training στο Microsoft Innovation Centre. Είδαμε αρκετά πράγματα, απ’το πώς να φτιάξουμε ένα πρώτο service μόνο σε κώδικα μέχρι πιο ενδιαφέροντα σενάρια. Ανεβάζω την παρουσίαση και όλα τα demos της, καθώς επίσης και κάποια demos που δεν πρόλαβα να δείξω ή θα δείξω αύριο!

Για όσους θέλουν να διαβάσουν κάποια περισσότερα πράγματα για το WCF μπορούν να δουν τα παρακάτω links:

Η συνέχεια αύριο με μερικά ακόμα demos και εισαγωγή στο Windows Azure και στο SQL Azure.

zip

Download

Share/Bookmark
Posted by jupiter | 4 comment(s)

TechEd, Days 4-5

This is my second and last post for TechEd Europe 2009. We returned to Athens yesterday after a 6 fantastic days in Berlin. So, what did we do the last couple of days..? On Thursday i attended, amongst others, two very interesting sessions:

  1. Dynamic in Microsoft Visual C# 4.0: The Why's and How's by Alex Turner
    Alex Turner presented how we can easily interact with dynamic objects (originating from COM, HTML DOM, Python, etc) in C# 4.0 which is a static language. The new dynamic type introduced in the latest version of C# makes things simpler for the programmer. Here is a nice blog post I stumbled upon: http://geekswithblogs.net/sdorman/archive/2008/11/16/c-4.0-dynamic-programming.aspx
  2. Pushing the limits of Windows by Mark Russinovich
    In this session, Mark Russinovich, one of the few (about 20) Technical Fellows in Microsoft, explained  the limits of Windows for the number of threads, processes, handles, etc. We saw, on live demos, how Windows react when they are running out of resources. You can read a series of posts about this topic on his blog: http://blogs.technet.com/markrussinovich/archive/2009/09/29/3283844.aspx 

 PB121738 PB111735

Friday was the last day of the conference, with sessions ending early. After a session in the morning i headed to the Hands-on labs where i got a brief introduction in F# language. We spent the rest of the sightseeing in Berlin. We didn’t have much free time the other days so we had to rush to see everything in one evening. We visited the Bradenburg Gate, Postdamer Platz, Holocaust Memorial, Checkpoint Charlie, Alexander Platz, the Parliament and saw part of the Wall that is still standing.

 PB121736PB131761 PB131807 PB141826

As i have already said many times, TechEd was a fantastic experience. Not only we attended many technical sessions, but we also met many people from other countries and had lots of fun. We played countless songs on Guitar Hero with other MSPs, drove Formula 1 simulator, saw interesting things by various companies on the Exhibition Hall and much more. Once again, thanks Microsoft Hellas for giving me the chance to be a part of this :)

Share/Bookmark
Posted by jupiter | with no comments
Filed under:

TechEd Days 0-3

Hello from Berlin! The 3rd day of the TechEd conference is over and we are back to our hotel, so it’s the best time to blog about this great event! We arrived at Berlin on Sunday and we headed directly to Messe Berlin to register! It turned out to be a wise choice since the queue for registration on Monday was too long, some people waited for 2 hours.

The whole place is enormous. There are about 15 halls where breakout sessions take place, some interactive theatres, exhibition halls, the Technical Learning Centre, Hands-on Labs, some lounges, the lunch halls and the Keynote hall! It’s very exciting to be here, watching all those interesting sessions, meeting developers from all around Europe…

DSC00103 PB091681

During the first day i attended the following 3 sessions and the Keynote:

  1. ADO.NET Entity Framework in VS10 and .NET 4,
  2. What’s new in SQL Azure and
  3. Developer General Session

On day two, we had our first MSP meeting with some very interesting sessions including XNA for Zune HD. During the meeting I met quite a few other MSPs from Italy, Switzerland, Norway, Spain and many other countries. It was nice chatting with them about their activity as MSPs, how they organize campus events, etc… After the meeting was over we went for a dinner with about 35 other MSPs which was really fun :)

PB101695PB101696 PB101728

Today, i attended 3 more great sessions:

  1. First one was about building a multi-touch application with Silverlight 3,
  2. Building applications with Windows Azure and SQL Azure and
  3. Windows API Codepack

and after those we had an MSP night out. We headed for a burger and beers and then for a wonderful walk on some underground Berlin sites! We saw an old underground brewery, some World War 2 bunkers and a place where some German missiles actually assembled! I still haven’t saved today’s photos to my laptop but i will do it soon. You can see more photos on my facebook photo album.

Once again, TechEd is awesome, i really enjoy being here. If you ever have the chance to attend a TechEd, don’t miss it :D

Share/Bookmark
Posted by jupiter | 1 comment(s)
Filed under:

Resources in Silverlight

A friend just asked me how he could add and read a txt file in his Silverlight application. So, I’m just posting the solution i gave him. I’ll add an image and a text file to the demo below.

First of all add the files you want to your project. Then go to the Properties and set the Build Action to “Resource” as show in the image:

Properties

And then you can use the code below to access your files:

Happy coding :)

Share/Bookmark
Posted by jupiter | 1 comment(s)
Filed under:

Imagine Cup 2010 – Training από Microsoft Hellas

Ο διαγωνισμός Imagine Cup έχει πλέον ξεκινήσει για τα καλά. Φέτος τα τελικά θα γίνουν στην Πολωνία. Η Microsoft Hellas διοργανώνει και φέτος το καθιερωμένο πλέον training για κάποιους επιλεγμένους φοιτητές! Έχοντας συμμετάσχει στα 2 προηγούμενα σας προτείνω ανεπιφύλακτα να δηλώσετε συμμετοχή! Θα μάθετε πολλά καινούρια πράγματα, όχι μόνο για development καθώς θα γίνουν και κάποια sessions για soft skills, δημιουργία business plan, κλπ. Φέτος θα είμαι και πάλι εκεί αλλά απ’την πλευρά του trainer, όποτε σας περιμένω να τα πούμε από κοντά :) Για περισσότερες πληροφορίες για το training και πως μπορείτε να δηλώσετε συμμετοχή δείτε στο αντίστοιχο thread στο forum μας! Είναι μεγάλη ευκαιρία να εκπαιδευτείτε σε ορισμένα θέματα δωρεάν, καθώς ένα αντίστοιχο training θα κόστιζε αρκετά χρήματα, εκμεταλευτείτε το ;)

Share/Bookmark
Posted by jupiter | with no comments
Filed under: ,

Windows 7 is here!

windows-7-logo

With just 3 days left before Windows 7 hit the shelves, i decided to make (yet another) post with all the great features, the available versions and their differences and much more. The first version of Windows 7, originally codenamed Blackcomb and later renamed to Vienna, officially available to public was Beta which was released on January 10th, 2009 (MSDN and TechNet subscribers got it a bit earlier as usual). A Release Candidate of Windows 7 Ultimate followed on May 5th, 2009, with a multilingual UI pack available for download via Windows Update on May 26th. RC was the last version officially available, before Windows 7 was Released to Manufacturing (RTM) on July 24th, 2009.  MSDN, TechNet and MSDNAA subscribers are able to download the final version since August 2009. After this brief timeline of Windows 7, let’s see some of the most interesting features (in no particular order).

Features

1. Taskbar and Jump Menus

Probably one of the best things in Windows 7 is the new and improved Taskbar. The taskbar in general is visible to users at almost all times and provides the means to perform many tasks, so the more powerful and user-friendly it is, the better. The new Taskbar was created with that in mind i guess, since it’s much better than the one that we were used to. An first sight you may not notice much of a difference but it is significantly improved. First of all, you will see that items on the taskbar now appear with just their icon without the text, saving up space for more applications. Also the icons are by default larger so that you can easily identify each item and same instances of an application are grouped together. One of the coolest new features is the preview of an application; you can see it by hovering your mouse over the icon. You can also move your mouse over the thumbnail itself to preview the application in full size, which is pretty useful if you have many instances of a program running. You can also pin any programs you like on your taskbar to access them even faster. Another great addition is the Jump Menus. You can right click on the items in taskbar and the Jump Menu will appear. There are many things you can do from the jump menu depending on the application, for example you can start InPrivate browsing for Internet Explorer or change your status on Windows Live Messenger. Last but not least, there is a permanent button to switch to desktop on the right end of the taskbar.

2. Aero Effects – Snap, Shake

Imagine that you have a dozen windows open and you just want to focus your attention on the excel sheet you are currently working on. Minimizing all the other windows is just a matter of “shaking” the window you are interested. You also have the ability to snap a window in the left or right half of the screen by just moving it. See the following video to better understand what i’m talking about.

3. Libraries

Windows 7 can group similar content, like Movies, Music, Documents and Pictures into “special folders” called Libraries. For example, you can add the folders of your Downloaded music, the CD-ripped music and your own written songs folder into the Music Library and access them from one place altogether. No matter where in disc (or discs) your files are stored you can view all of the same type in the corresponding Library. The files are physically located in their original location but you just can see them all in the same place. There are 4 libraries (the ones at the start of the paragraph) created by default, but you can create your own and add whatever folders you want. With the power of Home Group (see #5 below), sharing of the libraries is easier than ever.

libraries

4. Device Stage

Device Stage, a new addition to Windows, provides a centralized location to interact with various devices attached to the computer, such as MP3 players, digital cameras, mobile phones and many more. It supports devices connected to USB, Bluetooth or network. Device Stage is customized by the manufacturers and provides easy access to the device’s main functions. For example if you connect a mobile phone you may have options to import pictures taken, browse the phone’s file system, start a custom application from the manufacturer. Since i don’t own any device stage-compliant device, i borrowed a screenshot from Windows Team Blog. You can see a Device Stage in action in the video on Windows 7 website.

device-stage

5. Home Group

It took Windows many versions to get an easy way to share files across computers but now it’s here! You can create a Home Group (or join an existing one) and immediately start sharing files/folders or libraries. You can also select who to share it with and password protect it if privacy is an issue. Creating a Home Group option is only available in Home Premium, Professional, Ultimate and Enterprise editions, but you can still join an existing from Home Basic and Starter editions (see further down in this post for various editions and their features).

6. Touch support and Multi-Touch

One-finger touch support was built-in windows for a long time, but now you can do much more. Windows 7 supports multi touch and there are several gestures for common mouse actions. You can easily zoom, scroll up or down, navigate, rotate, etc using just your fingers (well, if you have a device that supports multi-touch). It’s really fun and changes the way you interact wit the computer! Windows Taskbar and Start Menu have larger icons so it’s really easy to navigate using fingers. Also Jump Menus (see #1) automatically adjust their size depending on the input method (mouse, touch).

7. One-click Connect to Network

With Windows 7, it takes just two clicks to connect to a WiFi (or other) network. Just click on the network icon on the tray and you get a list with all available networks!

wifi-oneclick

8. Customized notification tray

You now have the power to select not only which items will be shown on your tray (you can do that in Vista), but also if which ones are allowed to show notifications. So, no more annoying balloons popping up if you don’t want to!

notification-tray

9. More options on User Account Control

The unmanageable UAC of Windows Vista belongs to the past. Microsoft has heard people’s complaints about the UAC and decided to add more options so each user can customize when a warning message should appear. Most Vista users used to completely turn it off, to get rid of the notifications each time they wanted to do the simplest thing. Now, you can just tune UAC to relax the restrictions and still enjoy the security it offers.

uac

10. Calculator, WordPad, Paint

All these 3 applications are finally revamped with many new features and redesigned UI. Paint and WordPad now have ribbons instead old-style menus and calculator includes things like unit conversion. Here are a couple of screenshots.

calculator

paint

11. Gadgets – No more Sidebar

Microsoft decided to remove the Sidebar which was introduced in Windows Vista. The gadgets can now be added anywhere on your desktop where you can now resize them to your needs! You can read more about the gadgets on this MSDN article.

gadgets

12. “Play To”

This new feature will be quite welcomed by multimedia enthusiasts and those who own a Media Center PC. You can play your music on other PC’s or devices in your network by just right clicking on the file and selecting Play To. There is a large number of devices that support this feature including PCs running Windows 7 and Xbox360. You can find a list of more devices here.

13. Search in folders

A cool new addition on Windows 7 is the ability to search for a file inside the current folder of Windows Explorer. The search panel is there since Vista but it would search the entire computer instead. Now, you can also set filters like Size or Date Modified easily.

search-folder

14. Speed and Hardware Requirements

Windows Vista users complained a lot that the OS was slow and unresponsive with long boot times. It seems that all these issues are resolved in Windows 7. The latest version of Windows feels like XP in terms of speed and responsiveness, and also uses less resources like RAM. Windows 7 can run smoothly even on low-end PCs, unlike Windows Vista. The Starter edition is specifically designed for entry level PCs and Netbooks.

15. Windows XP Mode

This feature concerns mostly businesses who have applications running on Windows XP. Using Virtual PC behind the scenes, users can now run older Windows XP programs in Windows 7 without any problems. So they can still keep using their software without any modifications and benefit from all the great stuff in Windows 7!

16. Hotkeys

On Windows 7 you can do many many things from just your keyboard. I stumbled upon this list of hotkeys on Brandon Paddock’s blog:

hotkeys

 

Editions

Windows 7 will be available in 6 editions, but only the three of them will be sold in retail stores. Those are Home Premium, Professional and Ultimate. Below is a table with some of the main features that each edition will / won’t include. This list is not complete, you can view the “Compare Editions” page on Windows 7 website or the relevant wikipedia article.

Features

home-premium

Home Premium 

professional

Professional

ultimate

Ultimate

Aero

x

x

x

Mobility Center

x

x

x

Media Center

x

x

x

Multi-Touch

x

x

x

Home Group

x

x

x

XP Mode


x

x

Presentation Mode


x

x

Backup on network


x

x

BitLocker



x

Multilingual Pack



x

Boot from Virtual HD



x

The other three editions are: Starter, Home Basic and Enterprise.

Windows 7 Starter

The Starter edition includes a subset of the features available in full versions and is targeted for Netbooks. It will be available through OEM licensing and there won’t be an 64-bit version, just 32-bit one. Also some more limitations apply. For more information about Starter edition you can read this blog post.

Windows 7 Home Basic

The Home Basic edition will only be available to some emerging markets. This edition will include more features than the Starter edition but it will still lack some that are available in Home Premium, such as Media Center, Multi-Touch support and some Aero options.

Windows 7 Enterprise

Available only through Volume licensing, this edition will contain the same features as Windows 7 Ultimate that is available to individual users.

 

Availability

As stated above, starting October 22nd Windows 7 will available for purchase in retail stores . Students who study in departments that have MSDNAA (MSDN Academic Alliance) subscription, can already get their copy for free; the Professional edition (x86 and x64) is available for download since August. Same applies for those who have an MSDN subscription. If you are a student and you are not sure whether your school has MSDNAA, visit this site: http://msdn.microsoft.com/en-us/academic/dd861350.aspx.

 

Useful links

Share/Bookmark
Posted by jupiter | 5 comment(s)
Filed under:

Creating an Image Processing Library with C# – Part 1

Part 2

Some time ago, i was experimenting with Image Processing. So I created a small library of image processing functions / filters, which i decided tom make publicly available. Most of the filters i have written, are convolution ones like Gaussian Smoothing, Sharpen, Motion Blur etc… The implementation of this library is by no means the most efficient or fast and the primary purpose of this post is to help people understand what happens behind the scenes when they use a Blurring filter in Photoshop for example. Most probably this will be a series of 2 or 3 posts during the next couple of weeks.

Convolution

OK, lets get started. The most important part of these filters is the convolution operation. I will not get into much mathematical details about convolution, just what is important to show you how it works. To perform convolution on a pixel of the image we require a convolution kernel (i’ll use the term Filter Matrix for the rest of the post), which typically is a 3x3, 5x5 or 7x7 matrix. Convolution is basically a weighted summation. The value of the output pixel is determined by the values of that and some surrounding pixels. Each of these pixels contribute to the output value with a weight, specified in the Filter Matrix. You will understand it better if you take a look at the example in the image below.

For the rest of the post, i’ll be writing points as [y,x] instead of [x,y] just because that’s the way the matrices are declared/used in the library. So, if we apply the convolution operation on a pixel (y,x) of the input image I, the resulting pixel in the output image O will be:

Convolution1

where L is the Filter Matrix size,

ConvolutionSequals 
and F is the Filter Matrix. The filter F should be normalized (sum of all weights equals 1), otherwise we need to divide O[y,x] with a devisor D so that the pixels are in the 0-255 zone. D is usually the sum of all coefficients in the Filter Matrix. Some filters, like edge-detections ones, have a coefficients’ sum of 0. In those cases we need to avoid division by D = 0. Finally, sometimes we also add a constant value (Offset) to the result O[y,x] so that the output pixel is in [0, 255] range. We will see a couple of those filters in the next post.

So, in general, we can say that the formula for computing a pixel at (y,x) is:

Convolution2

As you see, the value of the output pixel at (y,x) is not only affected by the corresponding pixel in the original image, but from the ones near it as well. Each of the pixels has a weight that is specified in F. If we apply this process to every pixel, we get the filtered image. So, the only thing that changes for various filters is the Filter Matrix.

Here is an example of an image, while we apply a Sharpening filter to pixel at (4,2). The pixel has a value of 53 and you can see the values of the surrounding pixels. The value of output pixel O[4,2], according to the formula above, is:

ConvolutionExample 

MatrixFilter

Someone might ask, what values we use when computing the convolution for pixels on the edges. If we tried to apply the above filter for pixel at (1,0), we would try to get the value I[-1,0], I[-1,1], I[-1,2] that would lead to an error in our potential program. There are a few solutions used for pixels on edges:

  1. Use the same value as the first neighbor pixel in image.
  2. Use a value of 0 (black).
  3. Don’t apply the convolution to pixels on the edges. If the Filter Matrix has a width/height of 3 for example, you can apply the filter in image starting from [1,1] – That’s what i’ve implemented in this library.

First lets declare the Filter Matrix class (named ConvMatrix) that is used by the function that applies the filter:

The Offset, is the constant value that sometimes needs to be added as we discussed earlier. Factor is the normalizing factor (the divisor) and as you see it’s by default computed to the sum of the Filter Matrix coefficients.

We have a default size of 5x5 for the matrix, without any Offset and Factor = 1. Filter matrices of size 1x1, 3x3, 5x5 and 7x7 are allowed in the implementation but that can easily change. The default matrix, leaves the image unchanged. So, lets see a first implementation of the function that performs convolution to every pixel in the image. It uses the functions Bitmap.GetPixel() and Bitmap.SetPixel() of the System.Drawing namespace.

As you can see, we apply the filter to the pixels in the range ([s,s], [Height-s, Width-s]) to avoid the problem I described earlier. Notice that we use the convolution operation 3 times on each pixel, one for each color channel (R, G, B). On the last part, before setting the new value of the pixel, we divide with the normalizing factor (Factor), add the constant value (Offset) and filter out any values outside the (0, 255) range.

You now have almost all the code you need to test a filter on an image. We just need to declare a function for a filter (let’s use the sharpening filter of the example):

Here are a couple of sample images where you can see the results of filtering:

Sharpening Filter

 Sharpen Filter

Emboss Filter

Emboss Filter

 

The SafeImageConvolution() function works fine but it’s really slow because of the calls to GetPixel() and SetPixel().  On the next post, we will see a much faster implementation of the Convolution function, using pointers to iterate through the image. Also, i’ll post the Filter Matrices for the rest of the filters (smooth, emboss, edge-detection, motion blur, etc) and upload the complete source code and a sample WinForms project that uses the library. In the meantime you can see this library in action here: http://mzervos.xelixis.net/ImageFilters/

Share/Bookmark
Posted by jupiter | 4 comment(s)

Surface SDK on Vista/Windows 7 - x64

Yesterday i got a copy of the Surface SDK and tried to install it on my machine, only to find out that it doesn’t support 64-bit systems. Fortunately, i found a solution to this problem on another blog and i post the links here for more people to see:

  1. Surface SDK 1.0
  2. Surface SDK 1.0 with SP1

Here is a screenshot of my first Surface application :) An RSS feed reader for http://www.studentguru.gr/blogs :

SGfeeds

 

Update: If you download the Surface SDK from the MSDNAA, you most probably will get a file ‘en_surface_sdk_1_0_x86.exe’. When you run this .exe file, the .msi file you need to patch, gets extracted on your %temp% folder. So basically, go to Start->Run, type "%temp%" and the temp folder will come up. Delete all files or Sort them by date and then run the en_surface_sdk_1_0_x86.exe file. You will see a file named "22353a6.msi" (at least that was the name on my machine), that's the file you need ;) Copy it to another folder, and patch it using the guide.

Share/Bookmark
Posted by jupiter | 4 comment(s)
Filed under:
More Posts Next page »