Horus Doc || User Guide || Introduction   Installation   Getting Started  

An example of a binary pixel operation: Adding images

Addition of images is implemented by the global C++ function HxAdd:

/*
 *  Copyright (c) 2000, University of Amsterdam, The Netherlands.
 *  All rights reserved.
 *
 *  Author(s):
 *  Marc Navarro            (mnavarro@wins.uva.nl)
 *  Dennis Koelma           (koelma@wins.uva.nl)
 */

#include "HxAdd.h"
#include "HxPixMax.h"
#include "HxUnaryMax.h"

HxImageRep
HxAdd(HxImageRep im1, HxImageRep im2)
{
    HxString fname("HxAdd");

    if (im1.isNull())
    {
        HxGlobalError::instance()->reportError(fname, im1.name(), "null image", HxGlobalError::HX_GE_INVALID);
        return HxImageRep();
    }
    if (im2.isNull())
    {
        HxGlobalError::instance()->reportError(fname, im2.name(), "null image", HxGlobalError::HX_GE_INVALID);
        return HxImageRep();
    }

    if (im1.dimensionality() != im2.dimensionality())
    {
        HxGlobalError::instance()->reportError(fname, "unequal image dimensionalities", HxGlobalError::HX_GE_UNEQUAL_IMAGES);
        return HxImageRep();
    }
    if (im1.pixelDimensionality() != im2.pixelDimensionality())
    {
        HxGlobalError::instance()->reportError(fname, "unequal pixel dimensionalities", HxGlobalError::HX_GE_UNEQUAL_IMAGES);
        return HxImageRep();
    }

    if (im1.sizes().x() != im2.sizes().x())
    {
        HxGlobalError::instance()->reportError(fname, "unequal image widths", HxGlobalError::HX_GE_UNEQUAL_IMAGES);
        return HxImageRep();
    }
    if (im1.sizes().y() != im2.sizes().y())
    {
        HxGlobalError::instance()->reportError(fname, "unequal image heights", HxGlobalError::HX_GE_UNEQUAL_IMAGES);
        return HxImageRep();
    }
    if (im1.dimensionality() > 2)
    {
        if (im1.sizes().z() != im2.sizes().z())
        {
            HxGlobalError::instance()->reportError(fname, "unequal image depths", HxGlobalError::HX_GE_UNEQUAL_IMAGES);
            return HxImageRep();
        }
    }

    // in case of byte, unsigned: generate warnings in case of potentially dangerous
    // situations.
    // Check if image is byte.

    if (((im1.signature().pixelType() == INT_VALUE) && 
         (im1.signature().pixelPrecision() == 8)) ||
        ((im2.signature().pixelType() == INT_VALUE) && 
         (im2.signature().pixelPrecision() == 8)))
    {
        if ((im1.pixelDimensionality() == 1) && (im1.pixelDimensionality() == 1))
        {
            if ((HxPixMax(im1).HxScalarIntValue() + 
                 HxPixMax(im2).HxScalarIntValue()) > HxScalarInt(255))
            {
                HxGlobalError::instance()->reportWarning(fname, 
                    im1.name()+HxString(" ")+im2.name(), 
                    "possible overflow due to byte precision", 
                    HxGlobalError::HX_GW_OVERFLOW);
            }
        }
        else if ((HxPixMax(HxUnaryMax(im1)).HxScalarIntValue() + 
                  HxPixMax(HxUnaryMax(im2)).HxScalarIntValue()) > HxScalarInt(255))
        {
                HxGlobalError::instance()->reportWarning(fname, 
                    im1.name()+HxString(" ")+im2.name(), 
                    "possible overflow due to byte precision", 
                    HxGlobalError::HX_GW_OVERFLOW);
        }
    } 

    return im1.binaryPixOp(im2, "add");
}

As shown in the code, HxAdd is just a wrapper around the generic binary pixel operation. For an example of how to use a global function in a C++ application see A first application.

MidApp

HxAdd can also be used via the menu and dialogue interface:

Note that at some points in the image overflow occurs due to the default display model. This is caused by the fact that an image representation (i.e. an instance of HxImageRep) does not know what the pixel values mean so all the default display model does is to map the pixel values directly onto the RGB values used in Java. You can change the display model of the image representation by right clicking on the image, selecting 'setDisplayMode' from the 'Run' menu and choosing a more appropriate display model, e.g. 'Stretch'. You can also set a default display model via 'Viewer/Default Display Mode'.

We can also execute the generic binary pixel operation directly:

IDL script

Addition of images is demonstrated in demoAdd.cs

# Demo: shows how to add Horus images

println("Enter name of first image, e.g. c:/images/trui.tif");
name = getline();

println("Read image from disk");
a = OPS.HxMakeFromFile(name);

println("Enter name of second image, e.g. c:/images/cermet.tif");
name = getline();

println("Read image from disk");
b = OPS.HxMakeFromFile(name);

println("Now add the images using the global function HxAdd");
c = OPS.HxAdd(a, b);

println("Now add the images using the generic function");
d = a.binaryPixOp(b, "add", CTOR.emptyTagList());

and executed with exec("x:/HxSamples/Scripts/demoAdd.cs");.

Matlab

Addition of images is demonstrated in demoAdd.m

%demoAdd     Demo: shows how to add Horus images

echo on
clc

name = input('Enter name of first image, e.g. c:/images/trui.tif\n', 's');

% Read image from disk
a = OPS.HxMakeFromFile(name);

% Display the image
hxShow(a);

name = input('Enter name of second image, e.g. c:/images/cermet.tif\n', 's');

% Read image from disk
b = OPS.HxMakeFromFile(name);

% And display it
hxShow(b);

pause % Press any key to continue

% Now add the images using the global function HxAdd
c = OPS.HxAdd(a, b);

% And display it
hxShow(c);

pause % Press any key to continue

% Now add the images using the generic function
d = a.binaryPixOp(b, 'add', CTOR.emptyTagList);

% And display it
hxShow(c);


and executed with demoAdd.


Go to the next section or return to the index.


Generated on Mon Jan 27 15:12:12 2003 for UserGuide by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001