Using Norm in an ROI
In this example, the program zooms the source image and then
averages the image's pixel values.
To do this, the program computes the L1 norm
(that is, the sum of absolute values of pixels in the ROI)
and divides it by the number of pixels in the ROI.
Pixels in the destination image's ROI are set to this average value using the
iplSet()
function.
This average-and-set operation is performed in a loop, in which
the ROI is gradually shifted. After the loop is completed,
each pixel value in the destination image is equal to the average of pixel
values in the corresponding region of the input image.
To run the example from the command line, type:
> roi -t3 -s<roisize> [-z<zoom factor>] [-i =<L|C>]
(-i
specifies the interpolation mode).
The source:
int testRoiNorm( int tsize, int zoom, bool linear ) { const rawSize = 300; /// raw size of image const int numof = rawSize / tsize; /// number of tiles in one dimension const int tarea = tsize * tsize, size = tsize * numof; IplImage *dst=NULL, *src=NULL; __try { dst = iplCreateImageJaehne( IPL_DEPTH_8U, size, size ); src = iplCloneImage( dst ); if( !src || !dst ) return -1; /// make source image by using Zoom function and show it iplZoom( dst, src, zoom,1, zoom,1, linear ? IPL_INTER_LINEAR : IPL_INTER_CUBIC ); sprintf( msg, " Zoom( Jaehne, %d, %s )", zoom, (linear ? "LINEAR":"CUBIC") ); ipView( src, msg, !is_modal ); /// the images will use ROI for operations, /// the source image uses the ROI to calculate L1 norm, /// the destination image uses the ROI to set value IplROI roi = { 0, 0,0, tsize,tsize }; src->roi = dst->roi = &roi; /// for each ROI for( int y=0; y<numof; ++y ) { roi.yOffset = tsize * y; for( int x=0; x<numof; ++x ) { roi.xOffset = tsize * x; /// get mean value from source int value = (int)( iplNorm( src, NULL, IPL_L1 ) / tarea ); /// put this value to destination iplSet( dst, value ); } } /// show result sprintf( msg, " Filled by norm of ROIs %dx%d", tsize, tsize ); ipView( dst, msg , is_modal ); } __finally { /// preserve automatic variable and free memory src->roi = dst->roi = NULL; iplDeallocate( src, IPL_IMAGE_ALL ); iplDeallocate( dst, IPL_IMAGE_ALL ); } return 0; }
* Legal Information © 1998-2000, Intel Corporation