livecode.com
  • Home
  • Download
  • Developers
  • Academy
  • Community
  • Store
  • Support
  • About
LiveCode Lessons » How To - Step-By-Step Guides To Tasks In LiveCode » Vision: How do I Blur an Image?

Topics

  • Creating User Interfaces 22
    • Using Tab Buttons
    • How to create a rollover button with icon images
    • How to apply the new graphics effects to an object
    • How to make a stack with a window shape
    • How do I Send Messages During A Drag Operation?
    • How to show the progress of a download
    • How do I populate a field when an option menu is changed?
    • How do I scroll a card that is taller or wider than the window?
    • Creating Hypertext Links in Fields
    • How do I use the same controls on multiple cards?
    • Pinning Drag-and-drop to the mat: a primer
    • Detecting changes in a field
    • How do I use a Progress Bar?
    • How to manage and position objects when a stack is resized
    • How do I move between cards in my stack?
    • Play Sounds
    • How do I display a PDF in Rev?
    • How do I Create an ANSI Font Menu?
    • How to make a simple line graph
    • How to build an interactive plotter with a polygon
    • How To Create a Custom Cursor
    • How do I use Relayering?
  • Text 9
    • How Do I Use UTF-8 Text With Fields?
    • How do I Display Tab Delimited Text in a Table Field?
    • How can I search delimited data for a matching string?
    • How do I use Unicode in Rev?
    • How to count upper and lower case characters
    • Using the flagged property in a field
    • How do I style paragraphs in a field in LiveCode 5.5?
    • How do I display a table in a field?
    • How do I use hierarchical list support?
  • Working with files 9
    • How to list the files in a folder
    • How to read in data from an XML file
    • File Input/Output
    • How do I import a text file?
    • How Do I Process a Binary File?
    • How do I export a csv file?
    • How do I get the content of a URL on a web server into Revolution?
    • How do I rename the headers on a text file?
    • Uploading a file using FTP
  • The LiveCode Message Path 2
    • How to call a function or command in another object?
    • How to send messages to an object using the send command
  • Custom Properties 3
    • How do I save custom properties in a standalone application?
    • How do I define my own properties for an object?
    • Grouping custom properties in a custompropertyset
  • LiveCode tips 18
    • How to communicate with other applications using sockets
    • Opening a Stack from the Server
    • How do I Locate Scripts or Custom Properties in my Project?
    • How to relayer grouped controls
    • Setting toolTip and toolTipDelay
    • How do I get the path to common folders on my computer?
    • How can I get the path to the folder a stack file resides in?
    • Message box tips and tricks
    • What Are The Alternatives To Using Global Variables?
    • How to use date and time in Rev
    • How to create simple lists in LiveCode
    • How to create PDFs using LiveCode
    • How do I Create an HTML E-Mail?
    • How do I Attach a File to an E-Mail?
    • Dropping System Folders on Fields
    • How to Quickly Edit a Palette Stack in the IDE
    • Visual Basic-to-revTalk cheat sheet
    • How to create an application that opens when you drag a file onto it
  • LiveCode Arrays 9
    • How do I display an array in a table field?
    • How do I convert tab delimited data into an array?
    • How do I sort an array?
    • How do I add keys to or delete keys from an array?
    • How do I rename a key in an array?
    • How do I store an array in a text file and then retrieve it again?
    • How do I Search an Array?
    • How do I Display an Array in Human Readable Form?
    • How do I Store an Array Variable Inside of Another Array Variable?
  • Using Behaviors 3
    • How To Assign A Behavior
    • How to Override a Behavior
    • How To Create Field Placeholder Text Using Behaviors
  • LiveCode and Databases 5
    • How do I Create and set up a Database?
    • Converting a Database Cursor to a Data Grid Array
    • Connecting to a MySQL database
    • How to connect Database using ODBC
    • How to create and use an SQLite database
  • Creating an 8-puzzle game 3
    • How do I Create an 8-Puzzle Game?
    • How do I Tell the Computer to Solve the 8-Puzzle Game?
    • How do I Tell the Computer to Solve the 8-Puzzle Game Using A*?
  • Groups 0
  • Using Graphics 3
    • How do I Create Animated Overlays?
    • How do I add Motion to Animated Overlays?
    • How to create a scrolling group using the improved graphics architecture
  • Using Images 9
    • How to Create a Thumbnail Image
    • Vision: How do I Threshold an Image?
    • Vision: How do I Blur an Image?
    • Vision: How do I Edge Detect an Image?
    • How to proportionally resize an image
    • Vision: How do I Convert a Color Image to Grayscale?
    • How do you embed an image in a stack so a path reference is not needed?
    • Screenshot with Effects
    • How do you display an image by dragging it on to a stack?
  • LiveCode Plugins and Externals 5
    • How To Install 3rd Party Externals For Use in the IDE and Standalone Builder
    • How To Install a 3rd Party Plugin In the Revolution IDE
    • How to safely attach an external to your stack
    • Linking an OSX external bundle with a .dylib library
    • How to install custom user plugins
  • LiveCode revLets and the Web plugin 7
    • How to build a revlet
    • How do I communicate with the browser from a plugin?
    • How do I determine which permissions a revlet has?
    • How do I create a web-based revlet that references assets such as videos?
    • How do I pass data from a webpage to a plugin?
    • How do I Pass Variable Data from a Webpage to a Plugin?
    • How to build a revLet without a security prompt
  • Using dialogs 7
    • How do I Create a Color Picker Swatch?
    • How can I restrict an answer file dialog to just applications?
    • How Can I Pass Data To and Get Data From a Dialog Window?
    • How do I Create a Modal Dialog?
    • How to control where the answer file/folder dialog starts
    • How do I use the Project Browser?
    • How do I create a scalable UI using graphics?

Last Updated

Mar 23, 2011

Download Lesson PDF

Other Resources

  • Getting Started with LiveCode

  • Get Up and Running with LiveCode
  • Getting Started with LiveCode Development
  • LiveCode Concepts

  • Features, concepts and aspects of LiveCode
  • LiveCode Lessons

  • How To - Step-By-Step Guides To Tasks In LiveCode
  • How To - LiveCode Server Tasks
  • How To - LiveCode Mobile Tasks
  • How To - LiveCode Sample Scripts
  • How to - LiveCode Marketplace Products
  • How to Purchase and License LiveCode
  • Tutorials

  • Creating a Video Library Application
  • Data Grid

  • LiveCode Data Grid
  • Data Grid Tips & Tricks
  • Converting the Stock Program

Comments

0 for this lesson

  • Prev: Vision: How do I Threshold an Image?
  • Next: Vision: How do I Edge Detect an Image?

Vision: How do I Blur an Image?

This lesson describes how to blur an image, using a smoothing mask. Sample code and example screen captures are provided.

Introduction

The lesson continues along the theme of image processing and introduces an algorithm that can be used to smooth images in order to remove fine detail texture or noise. This is often useful when applying image processing algorithms that look at larger detail objects in an image.
The code in this lesson works with both, grayscale and color images, although the performance can be greatly improved, if you tailor the algorithm to work with grayscale images only.

Note: The examples shown here assume that you are familiar with lesson Vision: How do I Convert a Color Image to Grayscale?, and that you have a working LiveCode stack that implements the code shown there.

The smoothing process discussed here uses a configurable mask to perform operations on a number of neighboring pixels in an image. The generic image processing code is stored in the Stack Script, rather than a Button Script, making the architecture more hierarchical and relevant code segments easier to access from other buttons that may also want to use a mask for image processing.

Adding Stack Code Variables

Open the stack you created under lesson Vision: How do I Convert a Color Image to Grayscale? and add the following variables to the Stack Script:

local sXsize, sYsize, sImage

These variables hold the dimensions of the image to be processed and a copy of the image data itself.

Adding Stack Code Functions and Commands

First add the command convolve that sits at the upper most level and takes two parameters: the image to be processed and the mask that is be applied to the image.
This command loops through each pixel of the image and calls a function that applies the mask to a number of neighboring pixels.

command convolve xImage pMask
    local tImageTarget, tX, tY, tImagePosition, tConvolveValue
    # set up the global variables
    put the width of image xImage into sXsize
    put the height of image xImage into sYsize
    put the imageData of image xImage into sImage
    # take a copy of the image that is to be processed
    put the imageData of image xImage into tImageTarget
    # loop through every pixel of the image
    # and apply the mask to each color channel
    repeat with tY = 0 to sYsize - 1
        repeat with tX = 0 to sXsize - 1
            put (tX + tY * sXsize) * 4 into tImagePosition
            put applyMask (pMask, tX, tY, 2) into tConvolveValue
            put numToChar (tConvolveValue) into char (tImagePosition + 2) of tImageTarget
            # comment out the following line
            # if you are only going to process grayscale images
            put applyMask (pMask, tX, tY, 3) into tConvolveValue
            put numToChar (tConvolveValue) into char (tImagePosition + 3) of tImageTarget
            # comment out the following line
            # if you are only going to process grayscale images
            put applyMask (pMask, tX, tY, 4) into tConvolveValue
            put numToChar (tConvolveValue) into char (tImagePosition + 4) of tImageTarget
        end repeat
    end repeat
    set the imageData of image xImage to tImageTarget
    set the cHiddenImageData of image xImage to tImageTarget
end convolve

Add the function applyMask that applies the mask to a number of neighboring pixels. This function takes the mask, the image X and Y coordinates and the color channel as arguments.

function applyMask pMask pX pY pChannel
    local tResult, tImagePosition
    put 0 into tResult
    # loop through each grid position of the mask
    repeat for each key tKeyX in pMask
        repeat for each key tKeyY in pMask[tKeyX]
            # get the image position that lies under
            # a particular grid position of the mask
            put getImagePosition (pX+tKeyX, pY+tKeyY) into tImagePosition
            if tImagePosition is -1 then
                return 0
            else
                # multiply the weight of the grid position with the
                # respective image position and add that product to the result
                put pMask[tKeyX][tKeyY] * \
                    charToNum (char (tImagePosition + pChannel) of sImage) + \
                    tResult into tResult
            end if
        end repeat
    end repeat
    # some masks can generate negative results,
    # so ensure the result is positive
    return abs (tResult)
end applyMask

Add the function that returns an image index, calculated from X and Y coordinates.

function getImagePosition pX pY
    local tResult
    if (pX >= sXsize or pY >= sYsize or pX < 0 or pY < 0) then
        return -1
    else
        return (pX + pY * sXsize) * 4
    end if
end getImagePosition

Creating the Smoothing Button

Drag a button onto the stack. Then open the Property Inspector and change the name of the button to Smoothing. Next, open the Script Editor for this button and insert the following code:

on mouseUp
    local tMask
    # set up the 3x3 mask
    put 0.111 into tMask [1][1]
    put 0.111 into tMask [1][0]
    put 0.111 into tMask [1][-1]
    put 0.111 into tMask [0][1]
    put 0.111 into tMask[0][0]
    put 0.111 into tMask [0][-1]
    put 0.111 into tMask [-1][1]
    put 0.111 into tMask [-1][0]
    put 0.111 into tMask [-1][-1]
    # apply the mask
    convolve "Image", tMask
end mouseUp

This code sets up a smoothing mask in the shape of a 3x3 array with values of 0.111 in each array entry. This effectively allows each pixel that lies under the mask to contribute roughly 1/9 to the overall value of the pixel entry that is being updated.
The mask array is indexed from -1 to 1 along the X and Y coordinates, with the center grid position 0,0 representing the location at which the new pixel value is inserted.

Loading an Image

Your application may look something like the one shown in this example. After selecting the Load Image button you should get a file selection dialog that allows you to select an image from your file system. The image is then displayed in the Image Area.

Processing a Color Image

A color image is smoothed by selecting the Smoothing button.

Note: Smoothing color images can take a long time, as the smoothing mask is applied to each color channel of each pixel of the image.

Processing a Grayscale Image

The performance of the smoothing process can be improved by processing grayscale images. You can do this by using the Grayscale button before you use the Smoothing button.

Note: In order to get the performance increase, you have to comment out two lines of code in the Stack Script, as highlighted by comments under step: Adding Stack Code Functions and Commands.

  • Prev: Vision: How do I Threshold an Image?
  • Next: Vision: How do I Edge Detect an Image?

Comments (0)

Add your comment

E-Mail me when someone replies to this comment
Brought to you by RunRev Ltd, Registered in Scotland, No. SC200728