Example: User-Defined Filter Kernel


This example shows a "class script" that runs from a Mira image window—that is, from a Mira class. This type of script uses the data from that class. Here, the script creates a simple filter kernel and applies it to an image set (i.e., an image stack) displayed in a window. The script could call a function to select one of the images, but instead we simply process all images in the window. This script creates the filter kernel as a CMatrix object then applies it to all the images.

Key Concepts

bullet.gif    Running a script on previously displayed images in an Image Window.

bullet.gif    Creating and working with a CMatrix object.

bullet.gif    Working with a CRectangle object.

bullet.gif    Working with a CImageView object, updating it after changes and adjusting its image cursor.

bullet.gif    Applying a kernel filter to an image set.

bullet.gif    Deleting objects from memory.

Overview

The first action of the script is to connect itself to the Mira Image Window that is the top-most window. The script does this using the Attach method of the CImageView class; this creates a new CImageView object which is used to access the Image window's contents. A CImageView is just a script object that references a Mira Image Display window. Next, the script creates a 5x5 filter kernel as a CMatrix object. Then the script uses the CImage:FiltKernel method inside a for loop to filter to each in the CImageView object. Inside the loop, a CRect (rectangle) object constrains the filter to the central 20% of each image, which may be different since the images do not necessarily have the same dimensions. After processing all images, the script updates the Mira Image window to reflect changes to its image data and then it positions the Image Cursor to outline the region that was filtered. Finally, the script does some housekeeping to clean up the objects it created.

Implementation

This script is provided in the file [Mira]\Scripts\Samples\ImageFilterKernelTest.lua. To execute this script, a Mira Image Window must have the focus. Move the Image Window to the top of the stack and then run this script.

 

 

V = CImageView:Attach()

-- use the top-most image window

Assert(V,"Invalid image window")

-- exit if not a valid image window

 

 

-- setup a filter matrix

 

F = new_matrix( 5, 5, 0 )

-- make a 5 x 5 matrix with all 0 values,

 

-- new() returns a CMatrix

F:Set( 1, 1, 1 )

-- set element (1,1) to 1.0

F:Set( 1, 5, 1 )

-- set element

F:Set( 5, 1, 1 )

-- set element

F:Set( 5, 5, 1 )

-- set element

F:Normalize()      

-- normalize the filter to unit weight

 

 

R = new_rect()

-- create a rectangle for use in the loop

 

 

-- loop over all images

-- create a rectangle object

for i=1,V:Count() do

-- for all images in the image window

  I = V:GetImage(i)

-- create a CImage from image i

  w = I:Cols() ; h = I:Rows()

-- get the image dimensions

  R:Set(0.4*w,0.6*w,0.4*h,0.6*h)

-- rectangle = central 20% of image

  I:FiltKernel(F,5,5,R)

-- apply the filter

end

-- update all displayed images

-- update with the Image View window

 

V:UpdateAll()

-- redraw image window after changes

V:SetCursorRect( R )

-- place the cursor on the filtered area

-- clean up memory

 

I:delete() ; V:delete()

-- delete CImage, CImageView

F:delete() ; R:delete()

-- delete CMatrix, CRect

Exit("script finished")

-- optional Exit with optional message

 

 

After executing the script, the Mira Image Window might look like the one below. You can see that the Image Cursor was moved to outline the central 20% of the image that was filtered.

Improving the Script

There are several ways to extend this script:

  1. Allow the script to process only the top image or the entire image set using the status of the Image Window "process image set" flag. This is polled using CImageView:GetProcessSet.

  2. The 20% central region being filtered is hard-coded into the script. Use one of the dialog functions, such as GetNumber or GetString to input the rectangle limits while the script is running.

  3. This example creates the filter kernel inside the script. In a more general version, the script might display a list of filters stored in various files, from which the user would pick which filter to apply. This is done in another example; see Loading Filter Kernels from a File.

Related Topics

Examples


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.