Image processing is really a task for C/C++ based tool. There has been a vast ammount of work done on the topic and it gets into tertiary level math. If you wanted to do it in Perl/CGI your quickest path would be to get hold of a C/C++ library and make an XS wrapper for it.
Here is a useful site with source code. The main library it uses lives here and is available for Win32 or Linux with versions that compile with VCC, BCC, and GCC. You will find a wealth of info on Google searching for strings like 'image compare similarity C++'