blImageOperators — Overloaded operators and functions to use blImage as a generic matrix in c++

Introduction

blImage makes working with images in OpenCV a snap.  Automatic garbage collection, automatic type conversion and 100% compatibility with opencv algorithms, all that accomplished with the help of boost::shared_ptr and template meta-programming. Now, to expand its usefulness and applicability, in this post I present a collection of overloaded operators and a few functions that allow us to use blImage as a generic matrix, keeping in mind code readability and ease of use.

As an example, the following snippet shows just how easy it is to use blImage in a generic math program:

int main(int argc, char *argv[])
{
    // Create a matrix M1 from a 2d array
    double arr[3][3] = {{0.3,-3.1,1},{10.1,-9.5,2},{11,4,-2}};
    blImage<double> M1 = arr;

    // Calculate the inverse of M1
    // (Notice the automatic type conversion)
    blImage<float> M2 = inv(M1);

    // Multiply the two matrices
    blImage<double> M3 = M1*inv(M1);

    // Calculate the transpose
    blImage<double> M4 = Transpose(M1);

    // Output to the console
    cout < < "M1 = " << M1 << "\n\n";
    cout << "M2 = " << M1 << "\n\n";
    cout << "M3 = " << M3 << "\n\n";
    cout << "M4 = " << M4 << "\n\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output follows:

Dependencies

  1. blImage
    1. Used as the generic matrix
  2. cvTranspose
    1. Calculate the transpose of a matrix
  3. cvAdd
    1. Add two matrices
  4. cvSub
    1. Subtract two matrices
  5. cvScale
    1. Scale a matrix by a scalar value
  6. cvGEMM
    1. Matrix multiplication
  7. cvPseudoInverse
    1. To calculate the inverse or pseudo-inverse of a matrix
  8. cvAbsDiff
    1. Calculate the absolute difference between two matrices

The code

blImageOperations is a collection of overloaded operators and functions needed to use blImage as a generic matrix. They are collected in a file called blImageOperations.hpp, which uses inline documentation.

blImageOperations.hpp (Click to see code…)

Usage

This collection of overloaded operators and functions makes blImage a full blown generic matrix with linear algebra functionality, and easy code readability. It is actually incredible how a few functions can turn using opencv images into a snap. As an example, this following snippet shows just how powerful and convenient it is to use blImage:

int main(int argc, char *argv[])
{
    // Create a matrix M1 from a 2d array
    double arr[3][3] = {{0.3,-3.1,1},{10.1,-9.5,2},{11,4,-2}};
    blImage<double> M1 = arr;

    // Calculate the transpose of a matrix
    // (Notice the automatic type conversion)
    blImage<float> M2 = Transpose(M1);

    // Calculate the inverse of a matrix
    // (Again automatic type conversion)
    blImage<float> M3 = inv(M1);

    // Multiply two matrices
    // (Automatic type conversion here also)
    blImage<double> M4 = M1*M4;

    // Divide two matrices (multiply by inverse)
    blImage<double> M5 = M1/M1;

    // Output to the console
    cout << "M1 = " << M1 << "\n\n";
    cout << "M2 = " << M2 << "\n\n";
    cout << "M3 = " << M3 << "\n\n";
    cout << "M4 = " << M4 << "\n\n";
    cout << "M5 = " << M5 << "\n\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output is:

Downloads

I have put all the files into a zip file which can be downloaded here. All you have to do is extract it somewhere, let’s say in a directory called blImageAPI, and then include the blImageAPI.hpp file as follows:

#include "blImageAPI.hpp"
using namespace blImageAPI;

Note: Everything is declared in the namespace blImageAPI, and such you would use it as: blImageAPI::blImage
[download#1]

Updates

  1. Dec/29/2010 — I have taken the “AugmentTwoMatricesColumnWise” function out of this file and made a collection of all my “augmenting” functions in the post: blImageTiling.

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

Leave a Reply

Your email address will not be published. Required fields are marked *