Wednesday, April 25, 2012

Image Comparison using Image Magick freeware.

In my previous blog post, I have demonstrated the quickest way to identify whether 2 images are identical or different using C#. In this blog post, I will show you how to check exactly the visual difference between 2 different images using Image Magick compare tool.

ImageMagick is a software suite to create, edit, compose, or convert bitmap images. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. Here, I am going to demonstrate Compare utility of Image Magick.

Step 1: As a first step install Image Magick software which you can find in below link.

This will install the required compare.exe tool in below location
C:\Program Files\ImageMagick-x.x.x-xxx

Step 2: As a next step take 2 images which you want to compare. For example I have taken 2 bitmap images in which one has a text “Hello” and another one with “Hello World”.

Hello.bmp – Standard




HelloWorld.bmp – Image for Comparison




Step 3: Open command prompt and then type the command following below syntax






Command line Image Comparison using Compare.exe
Syntax:
Compare.exe –metric type InputImageFullPath1 InputImageFullPath2 DifferenceImageFullPath

Example:
C:\Program Files\ImageMagick-6.7.3-Q16>compare.exe -metric AE "D:\ImageComparisonTest\Hello.bmp" "D:\ImageComparisonTest\HelloWorld.bmp" "D:\ImageComparisonTest\DiffHelloWorld.bmp"

Where,
-metric {type} - measures the differences between images with this metric.
Ex: –metric AE - Here AE represents absolute error. This will report the standard error with the number of pixel counts that were actually masked in the difference output.
InputImageFullPath1 – Provide full path for the standard image.
InputImageFullPath2 – provide full path for the image which you want to compare with the standard image
DifferenceImageFullPath – Provide full path to save the difference output image.

Difference output:
7653 - Pixels have been masked in generating below difference image.
DiffHelloWorld.bmp



 
Here in the output image the difference is masked with red color.

This utility will be very much helpful in batch processing for comparing large set of images. For more information on the usage of compare utility refer below link
http://www.imagemagick.org/Usage/compare/

Sunday, April 22, 2012

Image Comparison – What is the quick way to identify whether 2 images are identical or different?


In this blog post, i will show you the quickest way to identify whether 2 images are identical or different using C#.

As a first step in determining whether 2 images are identical or not, we use the file size information for basic comparison. However what happens when 2 different images have same file size. In this case you cannot determine the difference using file size.

Following are several approaches in comparing the images.
  1. Comparing hash of the image
  2. Comparing byte by byte
  3. Comparing pixel by pixel etc..,
In the above approaches, 1st one comparing the hash of the images is the fastest technique to determine whether the images are different.

Image Hash Comparison:

Step 1: Find the hash of the given image
Using any hashing technique supported in C#, compute the hash of the image by providing image bytes as argument. This will create a 32 bytes hash.

Code Snippet:
HMACSHA1 hmacSha1 = new HMACSHA1();
byte[] imageHash = hmacSha1.ComputeHash(ImageBytes);

Note:  Here ComputeHash() method will always generate 32 bytes of hash irrespective of the size of the image.

Step 2: Compare the calculated 32 bytes hash (byte by byte) and if any byte differs you can stop the comparison and return that the images are not equal.

Code Snippet:
private bool IsImageEqual(byte[] imageHash1, byte[] imageHash2)
{
bool isImageEqual = true;
//Compare the hash values
for (int j = 0; j < imageHash1.Length && j < imageHash2.Length; j++)
{
if (imageHash1[j] != imageHash2[j])
{
isImageEqual = false;
break;
}
}
return isImageEqual;
}

Saturday, April 21, 2012

Recursive vs. For Loop, Which is better


Checked with factorial program and sequential sum.

In this blog post, I just wanted to post my observation on recursive and for loop program. I just started creating a factorial program using both recursive and for loop technique. I am just inquisitive to know which one is better.

In the initial observation both recursive and for loop produced results in almost identical time. 65 is the maximum integer value for which factorial is calculated properly. Then I did further power testing creating for loop to find out factorial for each integer starting 0 – 65 using both recursive and for loop technique. To my surprise I found out recursive did better than for loop by a few milliseconds.

Test case 1: Factorial
for (uint i = 0; i < 65; i++)
{
UInt64 factPower = FactForLoop(i);
}
for (uint i = 0; i < 65; i++)
{
UInt64 factPower = FactRecursive(i);
}

private UInt64 FactRecursive(UInt64 number)
{
if (number > 0)
fact = number * FactRecursive(number - 1);
return fact;
}
private UInt64 FactForLoop(UInt64 number)
{
for(uint i =1;i<=number;i++)
fact*=i;
return fact;
}

Then I tried sequential sum (i.e) for any given integer (n), the program does is creating a sum of integers starting 0 to n (0+1+2+..+n). In this case, the performance of both technique is identical and at some cases for loop fared better than the recursive technique opposite to what I saw earlier in factorial program.

Test Case 2: Sequential sum
//sum for loop
UInt64 result = SumForLoop(Convert.ToUInt64(txtBoxNumber.Text));
//sum recursive
UInt64 result = SumRecursive(Convert.ToUInt64(txtBoxNumber.Text));

private UInt64 SumRecursive(UInt64 number)
{
if (number > 0)
sum = number + SumRecursive(number - 1);
return sum;
}
private UInt64 SumForLoop(UInt64 number)
{
for (uint i = 1; i <= number; i++)
sum += i;
return sum;
}


Additional observation is that as we all know that the recursive call is being achieved by maintaining stack, it has its limits. At large number of stack calls (recursive calls), it may end up in stack overflow exception.

From my observation, you can go for recursive only if the number of stack calls is not huge otherwise stick to for loop. If you do find any other test results or different observation kindly let me know in the comments.

Friday, April 20, 2012

GIDS.Net –Where Microsoft Focuses Next?

This blog post will provide general details on things which I newly learnt @ the great Indian developer summit held at Bangalore on 4/17/2012.

I attended the following sessions

· Windows reimagined by Harish Vaidyanathan and Rajasekharan Vengalil – focused session on Windows 8.

· Put Web identity to work in your application by Mike Benkovich – session on using access control services

· How frameworks can kill your projects & patterns to prevent getting killed by Sander Hoogendoorn – session on what to do and what not to on implementing the chosen frameworks.

· The Windows 8 platform for metro style Apps by Ramaprasanna Chellamuthu – session on how to leverage metro style apps.

· SQL Server 2012 for Developers by Amit Bansal – session on new features in SQL Server 2012.

· Building Metro Style Apps with XAML: What .Net developers need to Know by Ramaprasanna Chellamuthu – Introductory session on how to create metro style Apps with familiar XAML features and concepts.

· Windows 8 and Cloud Computing – Notification & Identity by Mike Benkovich – session on notifications in metro style apps using Azure.

Based on these sessions, it is obvious that the Microsoft focusses on Windows 8 Metro style apps, Cloud services (Azure and SQL Azure) and SQL Server 2012.

Windows reimagined by Harish Vaidyanathan and Rajasekharan Vengalil

Both Harish and Rajasekharan demonstrated the new Windows 8 operating system and the market opportunity for developers. IMO, It’s never been this better, Fluid UI Start menu - which is clean and unique other than that the rest is Windows 7. Windows 8 is a switchable OS common to both tablet and PC. Metro style app menu is a next gen user interface which has live tiles for clear cut notifications on what happens in the app at the background. Ex. Contacts app - live tiles can display the instant updates of images of your social contacts as like in Windows phone 7.

Put Web identity to work in your application by Mike Benkovich

Mike demonstrated on how to use third party access control service providers like Facebook, Yahoo, and live ID into your cloud application. All you need for that is an active Azure subscription, next add an ACS service namespace, and then add a secure token service (STS) reference to your application.

How frameworks can kill your projects & patterns to prevent getting killed by Sander Hoogendoorn

Sander demonstrated on how to implement code independent of framework choices. He presented models of layered architectures, and looks at applying bridge patterns, managers-providers, dependency injection, descriptors and layer super-types followed by bad code examples using blocks from MS enterprise library, NHibernate, Log4Net and the entity framework.

The Windows 8 platform for metro style Apps by Ramaprasanna Chellamuthu

Ramaprasanna demonstrated on how to leverage metro style apps development. This is an introductory session over the architecture of building windows 8 platform. He introduced the platform design tenets, programming language choices and the integration points with the operating system and across metro style apps.

SQL Server 2012 for Developers by Amit Bansal

Amit demonstrated the new features of SQL server 2012 with demo codes. Some of the new features help the developer to code less and some with additional performance improvements. Nevertheless session is a walk through over the capabilities of SQL Server 2012.

Building Metro Style Apps with XAML: What .Net developers need to Know by Ramaprasanna Chellamuthu

Again Ramaprasanna took a session on Metro style Apps. This time he focused on how to create App with XAML skills learnt with .Net. He covered the features and concepts that are new for Windows 8.

Windows 8 and Cloud Computing – Notification & Identity by Mike Benkovich

Mike demonstrated how to deliver notifications in metro style apps (notification enabled services) using Windows Azure. He talked about live tiles and how to interact with users through the use of toast, tile, badge and raw notifications. The process involves is that the client app developer registers the app with windows push notification service (WNS) , then requests a channel to be notified on. It sends the channel to its cloud partner who persist the channel and then uses that to send notifications through WNS to the client. This result in a very rich interactive experience.

You can download windows azure toolkit for windows 8 in below link.

http://watwindows8.codeplex.com/

You can find the details on the session in below link.

http://www.developermarch.com/developersummit/schedule.html