Video and Image Processing Blockset 2 User’s Guide
How to Contact The MathWorks Web Newsgroup www.mathworks.com/contact_TS.html Technical Support www.mathworks.com comp.soft-sys.matlab suggest@mathworks.com bugs@mathworks.com doc@mathworks.com service@mathworks.com info@mathworks.com Product enhancement suggestions Bug reports Documentation error reports Order status, license renewals, passcodes Sales, pricing, and general information 508-647-7000 (Phone) 508-647-7001 (Fax) The MathWorks, Inc.
Revision History July 2004 October 2004 March 2005 September 2005 November 2005 March 2006 September 2006 March 2007 First printing Second printing Online only Online only Online only Online only Online only Online only New for Version 1.0 (Release 14) Revised for Version 1.0.1 (Release 14SP1) Revised for Version 1.1 (Release 14SP2) Revised for Version 1.2 (Release 14SP3) Revised for Version 2.0 (Release 14SP3+) Revised for Version 2.1 (Release 2006a) Revised for Version 2.
Contents Getting Started 1 What Is Video and Image Processing Blockset? . . . . . . . 1-2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing Video and Image Processing Blockset . . . . . . . . . Required Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Related Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 1-3 1-4 1-4 Product Demos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Importing and Exporting Video 2 Working with AVI Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Importing and Viewing AVI Files . . . . . . . . . . . . . . . . . . . . . Exporting to AVI Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Annotating AVI Files with Video Frame Numbers . . . . . . . Annotating AVI Files at Two Separate Locations . . . . . . . . Saving Portions of an AVI File to Separate Files . . . . . . . .
Chroma Resampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19 Geometric Transformation 5 Interpolation Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nearest Neighbor Interpolation . . . . . . . . . . . . . . . . . . . . . . Bilinear Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bicubic Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 5-2 5-3 5-4 Rotating an Image . . . . . . . . . . . . . . . . . . . . .
Image Enhancement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sharpening and Blurring an Image . . . . . . . . . . . . . . . . . . . Removing Salt and Pepper Noise from Images . . . . . . . . . . Removing Periodic Noise from Video . . . . . . . . . . . . . . . . . . Adjusting the Contrast in Intensity Images . . . . . . . . . . . . Adjusting the Contrast in Color Images . . . . . . . . . . . . . . . 7-27 7-27 7-35 7-41 7-48 7-53 Pixel Statistics . . . . . . . . . . . . . . . . . . . . .
1 Getting Started Video and Image Processing Blockset is a tool for processing images and video in the Simulink® environment. This chapter provides an introduction to Video and Image Processing Blockset, its product requirements, and its documentation. What Is Video and Image Processing Blockset? (p. 1-2) Learn more about Video and Image Processing Blockset and its components. Installation (p.
1 Getting Started What Is Video and Image Processing Blockset? Video and Image Processing Blockset is a tool used for the rapid design, prototyping, graphical simulation, and efficient code generation of video processing algorithms. Video and Image Processing Blockset blocks can import streaming video into the Simulink environment and perform two-dimensional filtering, geometric and frequency transforms, block processing, motion estimation, edge detection and other signal processing algorithms.
Installation Installation This section describes how to install Video and Image Processing Blockset and its documentation. It also reviews the other MathWorks products you must install to run Video and Image Processing Blockset.
1 Getting Started 2 Click Download products. 3 Log in to the system using your MathWorks Account e-mail and password. If you do not have a MathWorks Account, you can create one from this Web page. 4 Select your platform and the products you want to install. 5 Follow the instructions on the Download and Install screen, which describe how to download the product(s) and the installer. 6 Double-click the Installer.exe file to run the installer. 7 When prompted, enter your Personal License Password.
Product Demos Product Demos Video and Image Processing Blockset has a number of demo models that solve real-world problems. Begin viewing Video and Image Processing Blockset demos by using the MATLAB Help browser. For additional demo models, navigate to the MathWorks and MATLAB Central Web sites.
1 Getting Started Demos on the Web The MathWorks Web site contains demos that show you how to use Video and Image Processing Blockset. You can find these demos at http://www.mathworks.com/products/viprocessing/demos.jsp. You can run these demos without having MATLAB or Video and Image Processing Blockset installed on your system.
Working with the Documentation Working with the Documentation Video and Image Processing Blockset documentation includes the Video and Image Processing Blockset User’s Guide and the Video and Image Processing Blockset Reference. You can access this documentation using the MATLAB Help browser or on the MathWorks Web site.
1 Getting Started Documentation on the Web You can also view the documentation from the MathWorks Web site. The documentation available on these Web pages is for the latest release, regardless of whether the release was distributed on a DVD or as a Web download: 1 Navigate to the Video and Image Processing Blockset Product page at http://www.mathworks.com/products/viprocessing/. 2 On the right side of the page, click the Documentation link.
Working with the Documentation • MATLAB, to write scripts and functions with M-code, and to use functions with the command-line interface • Simulink, to create simple models as block diagrams and simulate those models What Chapter Should I Read? Follow the procedures in this guide to become familiar with the blockset’s functionality.
1 Getting Started • Read Chapter 8, “Example Applications” to learn how to track the motion of an object in a video stream. Also, learn more about motion compensation and image compression. For a description of each block’s operation, parameters, and characteristics, see the Block Reference in the Video and Image Processing Blockset documentation on the Web at http://www.mathworks.com/products/viprocessing/ or in the Help browser.
Key Blockset Concepts Key Blockset Concepts In this section, you learn how the Video and Image Processing Blockset blocks interpret input matrices and arrays. Images are real-valued ordered sets of color or intensity data. The blocks interpret input matrices as images, where each element of the matrix corresponds to a single pixel in the displayed image. Video data is a series of images over time. All blocks in Video and Image Processing Blockset can process images or video data.
1 Getting Started Image Types Images can be binary, intensity (grayscale), or RGB. Binary Images Binary images are represented by a Boolean matrix of 0s and 1s, which correspond to black and white pixels, respectively. For more information, see “Binary Images” in the Image Processing Toolbox documentation. Intensity Images Intensity images are represented by a matrix of intensity values. While intensity images are not stored with colormaps, you can use a gray colormap to display them.
Key Blockset Concepts Defining Intensity and Color The values in a binary, intensity, or RGB image can be different data types. The data type of the image values determines which values correspond to black and white as well as the absence or saturation of color. The following table summarizes the interpretation of the upper and lower bound of each data type. To view the data types of the signals at each port, from the Format menu, point to Port/Signal Displays, and select Port Data Types.
1 Getting Started Color Image Processing Video and Image Processing Blockset enables you to work with color images and video signals as multidimensional arrays. For example, the following model passes a color image from a source block to a sink block using a 384-by-512-by-3 array.
Key Blockset Concepts You can choose to process the image as a multidimensional array by setting the Image signal parameter to One multidimensional signal in the Image From File block dialog box.
1 Getting Started The blocks that support multidimensional arrays meet at least one of the following criteria: • They have the Image signal parameter on their block mask. • They have a note in their block reference pages that says, “This block supports intensity and color images on its ports.” • Their input and output ports are labeled “Image”. You can also choose to work with the individual color planes of images or video signals.
Key Blockset Concepts 1-17
1 Getting Started To process the individual color planes of an image or video signal, set the Image signal parameter to Separate color signals in both the Image From File and Video Viewer block dialog boxes. Note The capacity to input and output separate color planes might be obsoleted in a future version of Video and Image Processing Blockset.
Key Blockset Concepts If you are working with a block that only outputs multidimensional arrays, you can use the Selector block to separate the color planes. For an example of this process, see “Measuring an Angle Between Lines” on page 7-17. If you are working with a block that only accepts multidimensional arrays, you can use the Matrix Concatenation block to create a multidimensional array. For an example of this process, see “Finding the Histogram of an Image” on page 7-59.
1 Getting Started For pixel coordinates, the first component r (the row) increases downward, while the second component c (the column) increases to the right. Pixel coordinates are integer values and range from 0 to the length of the row or column. The pixel coordinates used in Video and Image Processing Blockset are zero based, while the pixel coordinates used by Image Processing Toolbox and MATLAB are one based.
Key Blockset Concepts positions. Second, the spatial coordinates of the center point of any pixel are identical to the pixel coordinates for that pixel. However, the pixel coordinate system is discrete, while the spatial coordinate system is continuous. This means that, in pixel coordinates, the upper-left corner of an image is (0,0), while in spatial coordinates, this location is (-0.5,-0.5).
1 Getting Started The Solver, while in scheduler mode, uses a block’s sample time to determine when the code behind each block is executed. For example, if the sample time of a Video From Workspace block is 0.05, the Solver executes the code behind this block, and every other block with this sample time, once every 0.05 second. Video Duration and Simulation Time The duration of the simulation is controlled by the Stop time parameter — not the input video.
Key Blockset Concepts Normal and Accelerator Mode The Simulink Accelerator removes much of the computational overhead required by Simulink models. It works by replacing blocks that are designed to handle any possible configuration in Simulink with compiled versions customized to your particular model’s configuration. Through this method, the Accelerator can achieve substantial improvements in performance for larger Simulink models. The performance gains are tied to the size and complexity of your model.
1 Getting Started For more information, see “Simulink Accelerator” in the Simulink documentation.
Key Blockset Concepts Optimizing Your Implementation Optimizing your implementation is a crucial step toward real-time video processing. The following tips can help improve the performance of your model: • Minimize the number of blocks in your model. • Process only the regions of interest to reduce the input data size. • Use efficient algorithms or the simplest version of an algorithm that achieves the desired result. • Use efficient block parameter settings.
1 Getting Started Developing Your Models Use the following general process guidelines to develop real-time video processing models to run on embedded targets. By optimizing the model at each step, you improve its final performance. 1 Create the initial model and optimize the implementation algorithm. Use floating-point data types so that the model runs faster in simulation mode. If you are working with a floating-point processor, go to step 3.
Key Blockset Concepts For more information about Real-Time Workshop optimization parameters, see “Generated Source Files and File Dependencies” in the Real-Time Workshop documentation. If you want to run these executables on a Windows machine where Video and Image Processing Blockset is not installed, copy vip_rt.dll from the machine where Video and Image Processing Blockset is installed to a directory on the system path of the other machine. The library vip_rt.
1 Getting Started Block Data Type Support The following table shows what data types are accepted on the main input data port of each Video and Image Processing Blockset block, unless otherwise noted. If the block is a source, the table shows what data types are accepted on the main output data port of each source block. • If the Double, Single, Boolean, and/or Custom Data Types columns are populated by an x, the block supports those data types.
Block Data Type Support Base Integer Fixed-Point x s, u s, u x x s, u s, u Blob Analysis x (Output) x (Output) s (Output) s, u (Output) Block Matching x x s, u s, u Block Processing The blocks inside the subsystem dictate the data types supported by this block.
1 Getting Started Base Integer Fixed-Point s, u s, u s, u s, u x s, u s, u x x s, u s, u Histogram Equalization x x s, u s, u Hough Lines x x s s (Word length less than or equal to 32) Hough Transform x (Output) x (Output) x u (Output) u (Output) Image Complement x x x s, u Image Data Type Conversion x x x s, u (Word length less than or equal to 16) s, u (Word length less than or equal to 16) Image From File x x x s, u s, u Image From Workspace x x x s, u
Block Data Type Support Block Double Single Boolean Base Integer Fixed-Point Projective Transformation x x x s, u s, u PSNR x x s, u s, u s, u Read Binary File Resize x x s, u s, u Rotate x x s, u s, u SAD x x s, u s, u Shear x x s, u s, u To Multimedia File This is a Signal Processing Blockset block.
1 Getting Started Image Credits This table lists the copyright owners of the images used in the Video and Image Processing Blockset documentation. 1-32 Image Source cameraman Copyright Massachusetts Institute of Technology. Used with permission. circuit Micrograph of 16-bit A/D converter circuit, courtesy of Steve Decker and Shujaat Nadeem, MIT, 1993. moon Copyright Michael Myers. Used with permission.
2 Importing and Exporting Video In this chapter, you learn how to bring video data into a model, display it on your monitor, and export it to an AVI or multimedia file. Working with AVI Files (p. 2-2) Use the From Multimedia File block to import video data into your Simulink model and the Write AVI File block to export video data to an AVI file Working with Multimedia Files (p.
2 Importing and Exporting Video Working with AVI Files Video and Image Processing Blockset enables you to work with video data within the Simulink environment. Before you can analyze or operate on your data, you must import it into your Simulink model. Blocks from the Sources library, such as the From Multimedia File block, can help you with this type of task.
Working with AVI Files 2 Locate an AVI file that you want to import into Simulink. If you do not have access to an AVI file, Video and Image Processing Blockset has sample AVI files you can use to complete this procedure. 3 Use the From Multimedia File block to import the AVI file into the model. Double-click the From Multimedia File block. • If you do not have your own AVI file, enter barcodes.avi for the File name parameter.
2 Importing and Exporting Video 6 Set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters. Set the parameters as follows: • Solver pane, Stop time = 20 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 7 Run your model. View your video in the Video Viewer window that automatically appears when you start your simulation.
Working with AVI Files Note The video that is displayed in the Video Viewer window runs as fast as Simulink processes the video frames. If you are on a Windows platform and you want to run the video at the frame rate that corresponds to the input sample time, use the To Video Display block. You have now imported and displayed video data in your Simulink model. In “Exporting to AVI Files” on page 2-6, you manipulate your video stream and export it to an AVI file.
2 Importing and Exporting Video audio associated with an AVI file, use the To Wave Device block in Signal Processing Blockset. Note The Video Viewer block is supported on all platforms, but it does not support code generation. If you are on a Windows platform, you can use the To Video Display block to display video data. This block supports code generation. For more information, see the To Video Display block reference page in the Video and Image Processing Blockset Reference.
Working with AVI Files Block Library Quantity Write AVI File Video and Image Processing Blockset > Sinks 1 Gain Simulink > Math Operations 2 3 Connect the blocks as shown in the following figure. You might need to resize some blocks to do so. You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 4 Use the Gain block to change the green values of the video stream.
2 Importing and Exporting Video 5 Use the Gain1 block to change the blue values of the video stream. Set the block parameters as follows: • Main pane, Gain = 1.5 • Signal Data Types pane, Output data type mode = Same as input 6 Use the Write AVI File block to export the video to an AVI file. Set the File name parameter to my_test_file.avi. 7 If you have not already done so, set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters.
Working with AVI Files You have now manipulated your video stream and exported it from a Simulink model to an AVI file. For more information, see the Write AVI File block reference page in the Video and Image Processing Blockset Reference. Annotating AVI Files with Video Frame Numbers You can use the Insert Text block to overlay text on video stream. In this example, you add a running count of the number of video frames to a video.
2 Importing and Exporting Video Block Library Quantity From Multimedia File Video and Image Processing Blockset > Sources 1 Insert Text Video and Image Processing Blockset > Text & Graphics 1 Video Viewer Video and Image Processing Blockset > Sinks 2 2 Position the blocks as shown in the following figure. 3 Use the From Multimedia File block to import the video into the Simulink model. Set the Image color space parameter to Intensity.
Working with AVI Files 5 Click-and-drag the Frame Counter block from the demo model into your model. This block counts the number of frames in an input video. 6 Use the Insert Text block to annotate the video stream with a running frame count.
2 Importing and Exporting Video 9 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = inf • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 10 Run the model. The original video appears in the Video Viewer window.
Working with AVI Files The annotated video appears in the Video Viewer1 window. You have now added descriptive text to a video stream. For more information, see the Insert Text block reference page in the Video and Image Processing Blockset Reference. For related information, see the Draw Shapes and Draw Markers block reference pages. Annotating AVI Files at Two Separate Locations You can use the Insert Text block to overlay text on a video stream at two separate locations in the video frame.
2 Importing and Exporting Video Block Library Quantity From Multimedia File Video and Image Processing Blockset > Sources 1 Insert Text Video and Image Processing Blockset > Text & Graphics 1 Constant Simulink > Sources 1 Video Viewer Video and Image Processing Blockset > Sinks 1 2 Position the blocks as shown in the following figure. 3 Use the From Multimedia File block to import the video stream into the Simulink model. Accept the default parameters.
Working with AVI Files By setting the Text parameter to 'Text position: Row %d and Column %d', you are asking the block to replace each conversion specification (%d) with a decimal input to the Variables port. You used the Location parameter to specify where to print each text string. In this case, the block places the top-left corner of the text box that surrounds the first text string 5 rows down and 10 rows over from the top left corner of the image.
2 Importing and Exporting Video 8 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = inf • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 9 Run the model. The annotated video appears in the Video Viewer window.
Working with AVI Files Alternatively, you can input two string values at the Variables port. 10 On the Insert Text block dialog box, set the Text parameter to'%s region of interest'. You are asking the block to replace the %s conversion specification with a string input to the Variables port. 11 Use the Constant block to specify the strings to substitute into the first and second text strings. Because the conversion specification is %s, the values must be 8-bit unsigned integer data types.
2 Importing and Exporting Video You have now added descriptive text to a video stream. For more information, see the Insert Text block reference page in the Video and Image Processing Blockset Reference. Saving Portions of an AVI File to Separate Files In this section, you use To Multimedia File and Enabled Subsystem blocks to save portions of one AVI file to three separate AVI files. 1 Create a new Simulink model, and add to it the blocks shown in the following table.
Working with AVI Files 3 Use the From Multimedia File block to import an AVI file into your model. Accept the default parameters. 4 Use the Insert Text block to annotate the video stream with the frame numbers. Set the parameters as follows: • Text = 'Frame %d' • Location = [10 10] • Color = [0 1 0] The block writes the frame number in green in the upper-left corner of the output video stream.
2 Importing and Exporting Video 5 Double-click each Enabled Subsystem block, and click-and-drag one of the To Multimedia File blocks into it. 6 Inside each Enabled Subsystem, connect the blocks so that your subsystem looks similar to the one in the following figure. 7 Use the To Multimedia File blocks to send the video stream to three separate AVI files. Set the block parameters as follows: • Output file name = output1.avi, output2.avi, and output3.
Working with AVI Files 8 Use the Counter block to count the number of video frames. You use this information to specify which frames are sent to which file. Set the block parameters as follows: • Count event = Free running • Initial count = 1 • Output = Count • Clear the Reset input check box. • Sample time = 1/30 • Count data type = uint16 9 Use the Compare to Constant block to send frames 1 to 9 to the first AVI file.
2 Importing and Exporting Video 10 Use the Compare to Constant1 and Compare to Constant2 blocks to send frames 10 to 19 to the second AVI file. Set the Compare to Constant1 block parameters as follows: • Operator = >= • Constant value = 10 Set the Compare to Constant2 block parameters as follows: • Operator = < • Constant value = 20 11 Use the Compare to Constant3 block to send frames 20 to 30 to the third AVI file.
Working with AVI Files 14 Set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters. Set the parameters as follows: • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 15 Run your model. The model saves the three output AVI files in your current directory.
2 Importing and Exporting Video 16 View the resulting files by typing the following commands at the MATLAB command prompt: mplay output1.avi mplay output2.avi mplay output3.avi Then, press the Play button on the MPlay GUI. You have now sent portions of an AVI file to three separate AVI files using an Enabled Subsystem block, a To Multimedia File block, and a trigger signal.
Working with Multimedia Files Working with Multimedia Files If you are working on a Windows platform, Video and Image Processing Blockset contains blocks that you can use to import and view multimedia files. These blocks include the From Multimedia File block, the To Multimedia File block, and the To Video Display block. These blocks perform best on platforms with DirectX Version 9.0 or later and Windows Media Version 9.0 or later. They also support code generation.
2 Importing and Exporting Video 2 Locate a multimedia file that you want to import into Simulink. If you do not have access to a multimedia file, Video and Image Processing Blockset has sample multimedia files you can use to complete this procedure. 3 Use the From Multimedia File block to import the multimedia file into the model. Double-click the From Multimedia File block: • If you do not have your own multimedia file, enter vipmen.avi for the File name parameter.
Working with Multimedia Files • Stop time = 20 • Type = Fixed-step • Solver = discrete (no continuous states) 7 Run your model. View your video in the To Video Display window that automatically appears when you start your simulation. This window closes as soon as the simulation stops. Note The video that is displayed in the To Video Display window runs at the frame rate that corresponds to the input sample time. To run the video as fast as Simulink processes the video frames, use the Video Viewer block.
2 Importing and Exporting Video 1 If the model you created in “Importing and Viewing Multimedia Files” on page 2-25 is not open on your desktop, you can open an equivalent model by typing doc_export2 at the MATLAB command prompt. 2 Click-and-drag the following blocks into your model. Block Library Quantity To Multimedia File Video and Image Processing Blockset > Sinks 1 Gain Simulink > Math Operations 3 3 Use the Gain blocks to increase the red, green, and blue values of the video stream.
Working with Multimedia Files 4 Use the To Multimedia File block to export the video to a multimedia file. Set the block parameters as follows: • Output file name = my_output.avi • Write = Video only • Image signal = Separate color signals 5 Connect the blocks as shown in the following figure. You might need to resize some blocks to do so.
2 Importing and Exporting Video You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 6 If you have not already done so, set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. On the Solver pane, set the parameters as follows: • Stop time = 20 • Type = Fixed-step • Solver = discrete (no continuous states) 7 Run your model.
Working with Multimedia Files You have now manipulated your video stream and exported it from a Simulink model to a multimedia file. For more information, see the To Multimedia File block reference page in the Video and Image Processing Blockset Reference. Working with Audio In this example, you use the From Multimedia File block to import a video stream into a Simulink model. You also use Signal Processing Blockset From Wave File block to import an audio stream into the model.
2 Importing and Exporting Video 3 Use the From Multimedia File block to import a multimedia file into the model. Accept the following default parameters.
Working with Multimedia Files The From Multimedia File block inherits its sample time from vipmen.avi. For video signals, the sample time is equivalent to the frame period. Because this file’s frame rate is 30 frames per second (fps) and the frame period is defined as 1/frame rate, the frame period of this block is 0.0333 seconds per frame. 4 Use the From Wave File block to import an audio file into the model.
2 Importing and Exporting Video The frame period of the audio signal must match the frame period of the video signals, which is 0.0333 seconds per frame. Since the frame period is also defined as the frame size divided by frequency, you can calculate the frame period of the audio signal by dividing the frame size of the audio signal (735 samples per frame) by the frequency (22050 samples per second) to get 0.0333 seconds per frame.
Working with Multimedia Files 6 Set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Configuration Parameters. On the Solver pane, set the parameters as follows: • Stop time = 10 • Type = Fixed-step • Solver = discrete (no continuous states) 7 Run your model. The model creates a multimedia file called output.avi in your current directory. 8 Play the multimedia file using a media player. The original video file now has an audio component to it.
2 2-36 Importing and Exporting Video
3 Working with MPlay The MPlay GUI enables you to view videos that are represented as variables in the MATLAB workspace. You can also use it to view video files or video signals in Simulink models. Viewing Videos from the MATLAB Workspace (p. 3-2) Use MPlay to view videos in the MATLAB workspace. Viewing Video Files (p. 3-6) Use MPlay to view videos stored in AVI files. Viewing Video Signals in Simulink (p. 3-8) Use MPlay to view video signals in Simulink models.
3 Working with MPlay Viewing Videos from the MATLAB Workspace The MPlay GUI enables you to view videos that are represented as variables in the MATLAB workspace, such as video data exported to the workspace by the Video To Workspace block. The following procedure shows you how to use the MPlay GUI to view such a video: 1 Define a variable that represents a video sequence in the MATLAB workspace. For example, to read an entire video into memory, type d = aviread('vipmen.
Viewing Videos from the MATLAB Workspace Note The MPlay GUI supports MATLAB variables that are in the movie structure array format. It also supports three-dimensional and four-dimensional arrays, which it interprets as intensity and RGB videos, respectively. You can use a function or any statement that can be evaluated for the MATLAB variable or expression parameter. 4 To resize the video to fill the GUI display area, click the Maintain fit to window button.
3 Working with MPlay 5 Experiment with using the MPlay GUI to play and interact with the video sequence. By default, the GUI assumes that the video data has a frame rate of 20 frames per second (fps). To change the frame rate to 30 fps, click Playback > Frame Rate. Enter 30 for the Desired playback rate parameter.
Viewing Videos from the MATLAB Workspace For more information about the MPlay GUI, see the mplay function reference page in the Video and Image Processing Blockset Reference.
3 Working with MPlay Viewing Video Files The MPlay GUI enables you to view videos from files without having to load all the video data into memory at once. The following procedure shows you how to use the MPlay GUI to load and view a video one frame at a time: 1 On the MPlay GUI, click . 2 Use the Connect to File dialog box to navigate to the multimedia file you want to view in the MPlay window. For example, navigate to $matlabroot\toolbox\vipblks\vipdemos\vipmen.avi. Click Open.
Viewing Video Files 3 Experiment with the MPlay GUI by using it to play and interact with the video stream. For more information about the MPlay GUI, see the mplay function reference page in the Video and Image Processing Blockset Reference.
3 Working with MPlay Viewing Video Signals in Simulink The MPlay GUI enables you to view video signals in Simulink models without adding blocks to your model. The following procedure shows you how to use the MPlay GUI to view a Simulink signal: 1 Open a Simulink model. At the MATLAB command prompt, type vipmplaytut 2 Open an MPlay GUI. 3 Run the model. 4 Select the signal line you want to view. For example, select the bus signal coming out of the Rotate block. 5 On the MPlay GUI, click .
Viewing Video Signals in Simulink Also, some new buttons appear above the video window. 6 Change to floating-scope mode by clicking the button. 7 Experiment with selecting different signals and viewing them in the MPlay window. You can also use multiple MPlay GUIs to display different Simulink signals. Note During code generation, Real-Time Workshop does not generate code for the MPlay GUI.
3 3-10 Working with MPlay
4 Conversions In this chapter, you learn how to convert an intensity image to a binary image and an RGB image to an intensity image. You also learn how to downsample the chroma components of an image. Intensity to Binary Conversion (p. 4-2) Learn how to convert an intensity image to a binary image. Color Space Conversion (p. 4-14) Learn how to convert color information between color spaces and to intensity values. Chroma Resampling (p.
4 Conversions Intensity to Binary Conversion Binary images contain Boolean pixel values that are either 0 or 1. Pixels with the value 0 are displayed as black; pixels with the value 1 are displayed as white. Intensity images contain pixel values that range between the minimum and maximum values supported by their data type. Intensity images can contain only 0s and 1s, but they are not binary images unless their data type is Boolean.
Intensity to Binary Conversion 3 Create a new Simulink model, and add to it the blocks shown in the following table. Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Video Viewer Video and Image Processing Blockset > Sinks 2 Relational Operator Simulink > Logic and Bit Operations 1 Constant Simulink > Sources 1 4 Position the blocks as shown in the following figure.
4 Conversions 5 Use the Image from Workspace block to import your image from the MATLAB workspace. Set the Value parameter toI 6 Use the Video Viewer1 block to view the original intensity image. Accept the default parameters. 7 Use the Constant block to define a threshold value for the Relational Operator block. Since the pixel values range from 0 to 255, set the Constant value parameter to 128. This value is image dependent.
Intensity to Binary Conversion 9 Use the Video Viewer block to view the binary image. Accept the default parameters. 10 Connect the blocks as shown in the following figure.
4 Conversions 11 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 12 Run your model. The original intensity image appears in the Video Viewer1 window. The binary image appears in the Video Viewer window.
Intensity to Binary Conversion Note A single threshold value was unable to effectively threshold this image due to its uneven lighting. For information on how to address this problem, see “Correcting for Nonuniform Illumination” on page 6-11. You have used the Relational Operator block to convert an intensity image to a binary image. For more information about this block, see the Relational Operator block reference page in the Simulink documentation.
4 Conversions 1 If the model you created in “Thresholding Intensity Images Using Relational Operators” on page 4-2 is not open on your desktop, you can open an equivalent model by typing doc_conversion at the MATLAB command prompt. 2 If you have not already done so, define an intensity image in the MATLAB workspace. At the MATLAB command prompt, type I= imread('rice.png'); I is a 256-by-256 matrix of 8-bit unsigned integer values that range from 0 to 255.
Intensity to Binary Conversion 5 Connect the blocks as shown in the following figure. 6 Use the Autothreshold block to perform a thresholding operation that converts your intensity image to a binary image. Select the Output threshold check box.
4 Conversions The block outputs the calculated threshold value at the Th port. 7 From the Signal Processing Blockset library, and then from the Signal Processing Sinks library, click-and-drag a Display block into the model. Connect it to the Th port.
Intensity to Binary Conversion 8 Double-click the Image From Workspace block. On the Data Types pane, set the Output data type parameter to double. 9 If you have not already done so, set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 10 Run the model.
4 Conversions The binary image appears in the Video Viewer window. In the model window, the Display block shows the threshold value, calculated by the Autothreshold block, that separated the rice grains from the background.
Intensity to Binary Conversion You have used the Autothreshold block to convert an intensity image to a binary image. For more information about this block, see the Autothreshold block reference page in the Video and Image Processing Blockset Reference. To open a demo model that uses this block, type vipstaples at the MATLAB command prompt.
4 Conversions Color Space Conversion The Color Space Conversion block enables you to convert color information from the R’G’B’ color space to the Y’CbCr color space and from the Y’CbCr color space to the R’G’B’ color space as specified by Recommendation ITU-R BT.601-5. This block can also be used to convert from the R’G’B’ color space to intensity. The prime notation indicates that the signals are gamma corrected.
Color Space Conversion 3 Create a new Simulink model, and add to it the blocks shown in the following table. Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Color Space Conversion Video and Image Processing Blockset > Conversions 1 Video Viewer Video and Image Processing Blockset > Sinks 1 4 Position the blocks as shown in the following figure.
4 Conversions Once you have assembled the blocks needed to convert a R’G’B’ image to an intensity image, you are ready to set your block parameters. To do this, double-click the blocks, modify the block parameter values, and click OK. 5 Use the Image from Workspace block to import your image from the MATLAB workspace. Set theValue parameter to I. 6 Use the Color Space Conversion block to convert the input values from the R’G’B’ color space to intensity.
Color Space Conversion 7 View the modified image using the Video Viewer block. Accept the default parameters. 8 Connect the blocks so that your model is similar to the following figure. 9 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu.
4 Conversions • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 10 Run your model. The image displayed in the Video Viewer window is the intensity version of the greens.jpg image. To view the image at its true size, right-click the window and select Set Display To True Size. In this topic, you used the Color Space Conversion block to convert color information from the R’G’B’ color space to intensity.
Chroma Resampling Chroma Resampling The Y’CbCr color space separates the luma (Y’) component of an image from the chroma (Cb and Cr) components. Luma and chroma, which are calculated using gamma corrected R, G, and B (R’, G’, B’) signals, are different quantities than the CIE chrominance and luminance. Because the human eye is more sensitive to changes in luma than to changes in chroma, you can reduce the bandwidth required for transmission or storage of a signal by removing some of the color information.
4 Conversions 3 Create a new Simulink model, and add to it the blocks shown in the following table.
Chroma Resampling The blocks to the left of and including the Chroma Resampling block represent the transmission portion of the model. The remaining blocks represent the receiving portion of the model. Once you have assembled these blocks, you are ready to set your block parameters. To do this, double-click the blocks, modify the block parameter values, and click OK. 5 Use the Image from Workspace block to import your image from the MATLAB workspace. Set the Value parameter to I.
4 Conversions The Image Pad block adds one column to the right of each plane of the array by repeating its border values. This padding minimizes the effect of the pixels outside the image on the processing of the image. Note When processing video streams, it is computationally expensive to pad every video frame. You should change the dimensions of the video stream before you process it with Video and Image Processing Blockset blocks.
Chroma Resampling – Index Option = Select all • 2 – Index Option = Select all • 3 – Index Option = Index vector (dialog) – Index = 1 Set the Selector1 block parameters as follows: • Number of input dimensions = 3 • 1 – Index Option = Select all • 2 – Index Option = Select all • 3 – Index Option = Index vector (dialog) – Index = 2 Set the Selector2 block parameters as follows: • Number of input dimensions = 3 • 1 – Index Option = Select all • 2 – Index Option = Select all • 3 – Index Op
4 Conversions a gamma corrected signal. Set the Image signal parameter to Separate color signals. 9 Use the Chroma Resampling block to downsample the chroma components of the image from the 4:4:4 format to the 4:2:2 format. Use the default parameters. The dimensions of the output of the Chroma Resampling block are smaller than the dimensions of the input. Therefore, the output signal requires less bandwidth for transmission.
Chroma Resampling 14 Configure Simulink to display signal dimensions next to each signal line. Click Format > Port/Signal Displays > Signal Dimensions. 15 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 16 Run the model.
4 Conversions 17 Examine the signal dimensions in your model. The Chroma Resampling block downsamples the Cb and Cr components of the image from 206-by-346 matrices to 206-by-173 matrices. These matrices require less bandwidth for transmission while still communicating the information necessary to recover the image after it is transmitted. You have used the Chroma Resampling block to downsample the Cb and Cr components of an image.
5 Geometric Transformation The Geometric Transformations library contains blocks that enable you to rotate, translate, shear, and resize images. Interpolation Overview (p. 5-2) Understand how blocks in the Geometric Transformations library interpolate values. Rotating an Image (p. 5-6) Use the Rotate block to continuously rotate an image. Resizing an Image (p. 5-13) Use the Resize block to reduce the size of an image. Cropping an Image (p.
5 Geometric Transformation Interpolation Overview Video and Image Processing Blockset contains blocks that perform geometric transformations. These blocks use interpolation to calculate the appropriate pixel values so that images appear rotated, translated, resized, or sheared. Note The examples in this section are illustrations of interpolation methods. The block algorithms are implemented in a slightly different way so that they are optimized for speed and memory.
Interpolation Overview 1 Zero pad the input matrix and translate it by 1.7 pixels to the right. Translated zero-padded matrix 0 1 0 2 1 3 2 0 3 0 0 0 4 0 5 4 6 5 0 6 0 0 0 7 0 8 7 9 8 0 9 0 0 1.7 pixels Original zero-padded matrix 2 Create the output matrix by replacing each input pixel value with the translated value nearest to it. The result is the following matrix: 0 0 1 2 3 0 0 4 5 6 0 0 7 8 9 Note You wanted to translate the image by 1.
5 Geometric Transformation 1 2 3 4 5 6 7 8 9 represents your input image. You want to translate this image 0.5 pixel in the positive horizontal direction using bilinear interpolation. The Translate block’s bilinear interpolation algorithm is illustrated by the following steps: 1 Zero pad the input matrix and translate it by 0.5 pixel to the right. Translated zero-padded matrix 0 1 1 2 2 3 3 0 0 0 4 4 5 5 6 6 0 0 0 7 7 8 8 9 9 0 0 Original zero-padded matrix 0.
Interpolation Overview 1 2 3 4 5 6 7 8 9 represents your input image. You want to translate this image 0.5 pixel in the positive horizontal direction using bicubic interpolation. The Translate block’s bicubic interpolation algorithm is illustrated by the following steps: 1 Zero pad the input matrix and translate it by 0.5 pixel to the right.
5 Geometric Transformation Rotating an Image You can use the Rotate block to rotate your image or video stream by a specified angle. In this example, you learn how to use the Rotate block to continuously rotate an image: 1 Define an RGB image in the MATLAB workspace. At the MATLAB command prompt, type I = checker_board; I is a 100-by-100-by-3 array of double-precision values. Each plane of the array represents the reg, green, or blue color values of the image.
Rotating an Image Block Library Quantity Rotate Video and Image Processing Blockset > Geometric Transformations 1 Video Viewer Video and Image Processing Blockset > Sinks 2 Gain Simulink > Math Operations 1 Display Signal Processing Blockset > Signal Processing Sinks 1 Counter Signal Processing Blockset > Signal Management > Switches and Counters 1 4 Position the blocks as shown in the following figure.
5 Geometric Transformation You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 5 Use the Image From Workspace block to import the RGB image from the MATLAB workspace. On the Main pane, set the Value parameter to I. 6 Use the Video Viewer block to display the original image. Accept the default parameters. The Video Viewer block automatically displays the original image in the Video Viewer window when you run the model.
Rotating an Image The Angle port appears on the block. You use this port to input a steadily increasing angle. Setting the Output size parameter to Expanded to fit rotated input image ensures that the block does not crop the output. 8 Use the Video Viewer1 block to display the rotating image. Accept the default parameters. 9 Use the Counter block to create a steadily increasing angle.
5 Geometric Transformation • Counter size = 16 bits • Output = Count • Clear the Reset input check box. • Sample time = 1/30 The Counter block counts upward until it reaches the maximum value that can be represented by 16 bits. Then, it starts again at zero. You can view its output value on the Display block while the simulation is running. You are using the Counter block from Signal Processing Blockset because its Count data type parameter enables you to specify the data type of its output.
Rotating an Image 12 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = inf • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 13 Run the model. The original image appears in the Video Viewer window. The rotating image appears in the Video Viewer1 window.
5 Geometric Transformation Note If you are on a Windows operating system, you can replace the Video Viewer block with the To Video Display block, which supports code generation.
Resizing an Image Resizing an Image You can use the Resize block to change the size of your image or video stream. In this example, you learn how to use the Resize block to reduce the size of an image: 1 Define an intensity image in the MATLAB workspace. At the MATLAB command prompt, type I = imread('moon.tif'); I is a 537-by-358 matrix of 8-bit unsigned integer values.
5 Geometric Transformation Image Courtesy of Michael Myers 5-14
Resizing an Image 3 Create a new Simulink model, and add to it the blocks shown in the following table. Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Resize Video and Image Processing Blockset > Geometric Transformations 1 Video Viewer Video and Image Processing Blockset > Sinks 2 4 Position the blocks as shown in the following figure. 5 Use the Image From Workspace block to import the intensity image from the MATLAB workspace.
5 Geometric Transformation The Video Viewer1 block automatically displays the original image in the Video Viewer1 window when you run the model. 7 Use the Resize block to shrink the image. Set the Resize factor in % parameter to 50. The Resize block shrinks the image to half its original size. 8 Use the Video Viewer block to display the modified image. Accept the default parameters. 9 Connect the blocks as shown in the following figure.
Resizing an Image 10 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 11 Run the model. The original image appears in the Video Viewer1 window. To view the image at its true size, right-click the window and select Set Display To True Size.
5 Geometric Transformation The reduced image appears in the Video Viewer window. Right-click the window and select Set Display To True Size. The smaller image is half the size of the original image.
Resizing an Image In this example, you used the Resize block to shrink an image. For more information about this block, see the Resize block reference page in the Video and Image Processing Blockset Reference. For more information about other geometric transformation blocks, see the Rotate, Shear, and Translate block reference pages.
5 Geometric Transformation Cropping an Image You can use the Selector block to crop your image or video stream. In this example, you learn how to use the Selector block to trim an image down to a particular region of interest: 1 Define an intensity image in the MATLAB workspace. At the MATLAB command prompt, type I = imread('coins.png'); I is a 246-by-300 matrix of 8-bit unsigned integer values.
Cropping an Image Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Video Viewer Video and Image Processing Blockset > Sinks 2 Selector Simulink > Signal Routing 1 4 Position the blocks as shown in the following figure. 5 Use the Image From Workspace block to import the intensity image from the MATLAB workspace. Set the Value parameter to I. 6 Use the Video Viewer1 block to display the original image. Accept the default parameters.
5 Geometric Transformation 7 Use the Selector block to crop the image.
Cropping an Image The Selector block starts at row 140 and column 200 of the image and outputs the next 70 rows and columns of the image. 8 Use the Video Viewer block to display the cropped image. The Video Viewer block automatically displays the modified image in the Video Viewer window when you run the model. 9 Connect the blocks as shown in the following figure.
5 Geometric Transformation 10 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 11 Run the model. The original image appears in the Video Viewer1 window. To view the image at its true size, right-click the window and select Set Display To True Size.
Cropping an Image The cropped image appears in the Video Viewer window. The following image is shown at its true size. In this example, you used the Selector block to crop an image. For more information about the Selector block, see the Simulink documentation. For information about the imcrop function, see the Image Processing Toolbox documentation.
5 5-26 Geometric Transformation
6 Morphological Operations Morphological image analysis can be used to perform image filtering, image segmentation, and measurement operations. Overview of Morphology (p. 6-2) Learn about morphological operations and which Video and Image Processing blocks can be used to perform them. Counting Objects in an Image (p. 6-3) Use the Opening and Label blocks to determine the number of spokes in a wheel. Correcting for Nonuniform Illumination (p.
6 Morphological Operations Overview of Morphology Morphology is the study of the shape and form of objects.
Counting Objects in an Image Counting Objects in an Image In this example, you import an intensity image of a wheel from the MATLAB workspace and convert it to binary. Then, using the Opening and Label blocks, you count the number of spokes in the wheel. You can use similar techniques to count objects in other intensity images. However, you might need to use additional morphological operators and different structuring elements: 1 Define an intensity image in the MATLAB workspace.
6 Morphological Operations The file testpat1.png is an intensity image of a wheel that contains 24 black spokes. 3 Create a new Simulink model, and add to it the blocks shown in the following table.
Counting Objects in an Image You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 5 Use the Image From Workspace block to import your image from the MATLAB workspace. Set the Value parameter to I. 6 Use the Constant block to define a threshold value for the Relational Operator block. Set the Constant value parameter to 200. 7 Use the Video Viewer1 block to view the original image. Accept the default parameters.
6 Morphological Operations The strel function creates a circular STREL object with a radius of 5 pixels. When working with the Opening block, pick a STREL object that fits within the objects you want to keep. It often takes experimentation to find the neighborhood or STREL object that best suits your application. 10 Use the Video Viewer block to view the opened image. Accept the default parameters. 11 Use the Label block to count the number of spokes in the input image.
Counting Objects in an Image 12 The Display block displays the number of spokes in the input image. Use the default parameters. 13 Connect the block as shown in the following figure.
6 Morphological Operations 14 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 15 Run the model. The original image appears in the Video Viewer1 window. To view the image at its true size, right-click the window and select Set Display To True Size.
Counting Objects in an Image As you can see in the preceding figure, the spokes are now separate white objects. In the model, the Display block correctly indicates that there are 24 distinct spokes. You have used the Opening and Label blocks to count the number of spokes in an image. For more information about these blocks, see the Opening and Label block reference pages in the Video and Image Processing Blockset Reference.
6 Morphological Operations To Workspace block in Simulink or the Signal to Workspace block in Signal Processing Blockset. For more information about STREL objects, see strel in the Image Processing Toolbox documentation.
Correcting for Nonuniform Illumination Correcting for Nonuniform Illumination Global threshold techniques, which are often the first step in object measurement, cannot be applied to unevenly illuminated images. To correct this problem, you can change the lighting conditions and take another picture, or you can use morphological operators to even out the lighting in the image.
6 Morphological Operations This image is darker at the bottom than at the top. You want to create a model to even out this lighting. 3 Create a new Simulink model, and add to it the blocks shown in the following table.
Correcting for Nonuniform Illumination Block Library Quantity Sum Simulink > Math Operations 2 Data Type Conversion Simulink > Signal Attributes 1 4 Position the blocks as shown in the following figure. Once you have assembled the blocks required to correct for uneven illumination, you need to set your block parameters. To do this, double-click the blocks, modify the block parameter values, and click OK. 5 Use the Image From Workspace block to import the intensity image into your model.
6 Morphological Operations The strel function creates a circular STREL object with a radius of 15 pixels. When working with the Opening block, pick a STREL object that fits within the objects you want to keep. It often takes experimentation to find the neighborhood or STREL object that best suits your application. 8 Use the Video Viewer1 block to view the background estimated by the Opening block. Accept the default parameters.
Correcting for Nonuniform Illumination 13 Use the second Sum block to lighten the image so that it has the same brightness as the original image. Set the block parameters as follows: • Icon shape = rectangular • List of signs = ++ 14 Use the Video Viewer3 block to view the corrected image. Accept the default parameters. 15 Connect the blocks as shown in the following figure. 16 Set the configuration parameters.
6 Morphological Operations The original image appears in the Video Viewer window. To view the image at its true size, right-click the window and select Set Display To True Size. The estimated background appears in the Video Viewer1 window. The following image is shown at its true size.
Correcting for Nonuniform Illumination The image without the estimated background appears in the Video Viewer2 window. The following image is shown at its true size. The preceding image is too dark. The Constant block provides an offset value that you used to brighten the image. The corrected image, which has even lighting, appears in the Video Viewer3 window. The following image is shown at its true size.
6 Morphological Operations In this section, you have used the Opening block to remove irregular illumination from an image. For more information about this block, see the Opening block reference page in the Video and Image Processing Blockset Reference. For related information, see the Top-hat block reference page. For more information about STREL objects, see the strel function in the Image Processing Toolbox documentation.
7 Analysis and Enhancement You can use Video and Image Processing Blockset blocks to learn more about the structure of images as well as to improve them. Feature Extraction (p. 7-2) Learn more about the content of images. Image Enhancement (p. 7-27) Understand how to improve image characteristics. Pixel Statistics (p. 7-59) Determine information about the data values that make up an image using blocks from the Statistics library.
7 Analysis and Enhancement Feature Extraction Feature extraction techniques return information about the structure of an image. For example, you can use them to find edges, locations, and attributes of objects.
Feature Extraction 3 Create a new Simulink model, and add to it the blocks shown in the following table.
7 Analysis and Enhancement 4 Place the blocks so that your model resembles the following figure. You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 5 Use the Image From Workspace block to import your image from the MATLAB workspace. Set the block parameters as follows: • Main pane, Value = I • Data Types pane, Output data type = double 6 Use the Edge Detection block to find the edges in the image.
Feature Extraction The Edge Detection block convolves the input matrix with the Sobel kernel to calculate the gradient components of the image that correspond to the horizontal and vertical edge responses. The block outputs these components at the Gh and Gv ports, respectively. Then it performs a thresholding operation on these gradient components to find the binary image, a matrix filled with 1s and 0s.
7 Analysis and Enhancement 9 Use the Subtract blocks to subtract the minimum values from each element of the Gv and Gh matrices. This process ensures that the minimum value of these matrices is 0. Accept the default parameters. 10 Use the Maximum blocks to find the maximum value of the new Gv and Gh matrices. Set the Mode parameters to Value. 11 Use the Divide blocks to divide each element of the Gv and Gh matrices by their maximum value.
Feature Extraction • Solver pane, Solver = discrete (no continuous states) 15 Run your model. The Video Viewer window displays the edges of the rice grains in white and the background in black. To view the image at its true size, right-click the window and select Set Display To True Size. The Video Viewer1 window displays the intensity image of the vertical gradient components of the image. You can see that the vertical edges of the rice grains are darker and more well defined than the horizontal edges.
7 Analysis and Enhancement The Video Viewer2 window displays the intensity image of the horizontal gradient components of the image. In this image, the horizontal edges of the rice grains are more well defined. The following image is shown at its true size. 16 Double-click the Edge Detection block and clear the Edge thinning check box.
Feature Extraction 17 Run your model again. Your model runs faster because the Edge Detection block is more efficient when you clear the Edge thinning check box. However, the edges of rice grains in the Video Viewer window are wider. You have now used the Edge Detection block to find the object boundaries in an image. For more information on this block, see the Edge Detection block reference page in the Video and Image Processing Blockset Reference.
7 Analysis and Enhancement imshow(I) Image Courtesy of Steve Decker and Shujaat Nadeem 3 Create a new Simulink model, and add to it the blocks shown in the following table.
Feature Extraction Block Library Quantity Hough Transform Video and Image Processing Blockset > Transforms 1 Find Local Maxima Video and Image Processing Blockset > Statistics 1 Selector Simulink > Signal Routing 2 Variable Selector Signal Processing Blockset > Signal Management > Indexing 2 Terminator Simulink > Sinks 1 Hough Lines Video and Image Processing Blockset > Transforms 1 Draw Shapes Video and Image Processing Blockset > Text & Graphics 1 Video Viewer Video and Image Pro
7 Analysis and Enhancement 5 Use the Image From Workspace block to import your image from the MATLAB workspace. Set the Value parameter to I. 6 Use the Edge Detection block to find the edges in the intensity image. This process improves the efficiency of the Hough Lines block as it reduces the image area over which the block searches for lines. The block also converts the image to a binary image, which is the required input for the Hough Transform block. Accept the default parameters.
Feature Extraction 9 Use the Find Local Maxima block to find the location of the maximum value in the Hough matrix. Set the block parameters as follows: • Maximum number of local maxima (N) = 1 • Select the Input is Hough matrix spanning full theta range check box. 10 Use the Selector blocks to separate the indices of the rho and theta values, which are output at the Idx port, that are associated with the maximum value in the Hough matrix.
7 Analysis and Enhancement • 1 – Index Option = Index vector (dialog) – Index = 0 • Input port size = 2 Set the Selector1 block parameters as follows: • Index mode = Zero-based • 1 – Index Option = Index vector (dialog) – Index = 1 • Input port size = 2 11 Use the Variable Selector blocks to index into the rho and theta vectors and determine the rho and theta values that correspond to the longest line in the original image.
Feature Extraction 16 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 17 Run your model. The Video Viewer window displays the edges found in the original image in white and the background in black.
7 Analysis and Enhancement The Video Viewer1 window displays the original image with a white line drawn over the longest line in the image. You have now used the Hough Transform, Find Local Maxima, and Hough Lines blocks to find the longest line in an image.
Feature Extraction these blocks, see the Hough Transform, Find Local Maxima, and Hough Lines block reference pages in the Video and Image Processing Blockset Reference. For additional examples of the techniques used in this section, see the Lane detection and tracking and Rotation correction demos. You can open these demos by typing vipdetectlane and viphough at the MATLAB command prompt.
7 Analysis and Enhancement Block Library Quantity Embedded MATLAB Function Simulink > User-Defined Functions 1 Draw Shapes Video and Image Processing Blockset > Text & Graphics 1 Display Simulink > Sinks 1 Video Viewer Video and Image Processing Blockset > Sinks 3 2 Position the blocks as shown in the following figure. 3 Use the Image From File block to import an image into the Simulink model. Set the parameters as follows: • File name = gantrycrane.
Feature Extraction Note In this example, you segment the image using a thresholding operation that performs best on the Cb channel of the Y’CbCr color space. 5 Use the Selector and Selector1 blocks to separate the Y’ (luminance) and Cb (chrominance) components from the main signal. The Selector block separates the Y’ component from the entire signal.
7 Analysis and Enhancement 6 Use the Submatrix and Submatrix1 blocks to crop the Y’ and Cb matrices to a particular region of interest (ROI). This ROI contains two beams that are at an angle to each other.
Feature Extraction 9 Use the Find Local Maxima block to find the peak values in the Hough matrix. These values represent potential lines in the input image. Set the parameters as follows: • Neighborhood size = [11 11] • Input is Hough matrix spanning full theta range = selected Because you are expecting two lines, leave the Maximum number of local maxima (N) parameter set to 2, and connect the Count port to the Terminator block.
7 Analysis and Enhancement • Starting row = Index • Starting row index = 2 • Ending row = Index • Ending row index = 2 The Idx port of the Find Local Maxima block outputs a matrix whose second row represents the zero-based indices of the theta values that correspond to the peaks in the Hough matrix. Now that you have these indices, you can use a Selector block to extract the corresponding theta values from the vector output of the Hough Transform block.
Feature Extraction 13 Use the Hough Lines block to find the Cartesian coordinates of lines that are described by rho and theta pairs. Set the Sine value computation method parameter to Trigonometric function. 14 Use the Draw Shapes block to draw the lines on the luminance portion of the ROI. Set the parameters as follows: • Shape = Lines • Border value = White 15 Use the Embedded MATLAB Function block to calculate the angle between the two lines.
7 Analysis and Enhancement 19 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 20 Run the model.
Feature Extraction The Video Viewer window displays the original image. The Video Viewer1 window displays the ROI where two beams intersect.
7 Analysis and Enhancement The Video Viewer2 window displays the ROI that has been annotated with two white lines. The Display block shows a value of 54, which is the angle in degrees between the two lines on the annotated ROI. You have now annotated an image with two lines and measured the angle between them. For additional information, see the Hough Transform, Find Local Maxima, Hough Lines, and Draw Shapes block reference pages in the Video and Image Processing Blockset Reference.
Image Enhancement Image Enhancement Image enhancement techniques improve images. You can use them to remove noise from images, increase the signal-to-noise ratio, make certain features easier to see by modifying the colors or intensities, or sharpen the image.
7 Analysis and Enhancement I is a 384-by-512-by-3 array of double-precision floating-point values. Each plane of this array represents the red, green, or blue color values of the image. 2 To view the image this array represents, at the MATLAB command prompt, type imshow(I) Now that you have defined your image, you can create your model. 3 Create a new Simulink model, and add to it the blocks shown in the following table.
Image Enhancement Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Color Space Conversion Video and Image Processing Blockset > Conversions 2 2-D FIR Filter Video and Image Processing Blockset > Filtering 1 Video Viewer Video and Image Processing Blockset > Sinks 1 4 Position the blocks as shown in the following figure. 5 Use the Image From Workspace block to import the R’G’B’ image from the MATLAB workspace.
7 Analysis and Enhancement 7 Use the 2-D FIR Filter block to filter the luma portion of the image.
Image Enhancement The fspecial('unsharp') command creates two-dimensional highpass filter coefficients suitable for correlation. This highpass filter sharpens the image by removing the low frequency noise in it. 8 Use the Color Space Conversion1 block to converts the color information from the Y’CbCr color space to the R’G’B’ color space.
7 Analysis and Enhancement 9 Use the Video Viewer block to automatically display the new, sharper image in the Video Viewer window when you run the model. Set the Image signal parameter to Separate color signals. 10 Connect the blocks as shown in the following figure.
Image Enhancement 11 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 12 Run the model. A sharper version of the original image appears in the Video Viewer window. To view the image at its true size, right-click the window and select Set Display To True Size.
7 Analysis and Enhancement 13 To blur the image, double-click the 2-D FIR Filter block. Set Coefficients parameter to fspecial('gaussian',[15 15],7) and then click OK. The fspecial('gaussian',[15 15],7) command creates two-dimensional Gaussian lowpass filter coefficients. This lowpass filter blurs the image by removing the high frequency noise in it. 14 Run the model. A blurred version of the original image appears in the Video Viewer window. The following image is shown at its true size.
Image Enhancement In this example, you used the Color Space Conversion and 2-D FIR Filter blocks to sharpen and blur an image. For more information on these blocks, see the Color Space Conversion and 2-D FIR Filter block reference pages in the Video and Image Processing Blockset Reference. For more information on the fspecial function, see the Image Processing Toolbox documentation.
7 Analysis and Enhancement I= double(imread('circles.png')); I= imnoise(I,'salt & pepper',0.02); I is a 256-by-256 matrix of 8-bit unsigned integer values. 2 To view the image this matrix represents, at the MATLAB command prompt, type imshow(I) The intensity image contains noise that you want your model to eliminate. 3 Create a new Simulink model, and add to it the blocks shown in the following table.
Image Enhancement Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Median Filter Video and Image Processing Blockset > Filtering 1 Video Viewer Video and Image Processing Blockset > Sinks 2 4 Place the blocks as shown in the following figure. Now that you have assembled the blocks required to remove the noise in your image, you need to set your block parameters. To do this, double-click the blocks, modify the block parameter values, and click OK.
7 Analysis and Enhancement The Median Filter block replaces the central value of the 3-by-3 neighborhood with the median value of the neighborhood. This process removes the noise in the image. 7 Use the Video Viewer blocks to display the original, noisy image and the modified image. Because these images are represented by 8-bit unsigned integers, a value of 0 corresponds to black and a value of 255 corresponds to white. Accept the default parameters. 8 Connect the blocks as shown in the following figure.
Image Enhancement 9 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 10 Run the model. The original noisy image appears in the Video Viewer window. To view the image at its true size, right-click the window and select Set Display To True Size.
7 Analysis and Enhancement The cleaner image appears in the Video Viewer1 window. The following image is shown at its true size. You have used the Median Filter block to remove noise from your image. For more information about this block, see the Median Filter block reference page in the Video and Image Processing Blockset Reference.
Image Enhancement Removing Periodic Noise from Video Periodic noise can be introduced into a video stream during acquisition or transmission due to electrical or electromechanical interference. In this example, you remove periodic noise from an intensity video using the 2-D FIR Filter block.
7 Analysis and Enhancement You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 5 Use the Read Binary File block to import a binary file into the model. Set the block parameters as follows: • File name = cat_video.
Image Enhancement 6 Use the Image Data Type Conversion block to convert the data type of the video to single-precision floating point. Accept the default parameter. 7 Use the Video Viewer block to view the original video. Accept the default parameters. 8 Use the Add block to add the noise video to the original video. Accept the default parameters. 9 Use the Video Viewer1 block to view the noisy video. Accept the default parameters. 10 Define the filter coefficients in the MATLAB workspace.
7 Analysis and Enhancement radians per sample. The coefficients were created using the Filter Design and Analysis Tool (FDATool) and the ftrans2 function. 11 Use the 2-D FIR Filter block to model a band-reject filter capable of removing the periodic noise from the video. Set the block parameters as follows: • Coefficients = h • Output size = Same as input port I • Padding options = Circular Choose a type of padding that minimizes the effect of the pixels outside the image on the processing of the image.
Image Enhancement 12 Use the Video Viewer2 block to view the approximation of the original video. Accept the default parameters. 13 Connect the block as shown in the following figure.
7 Analysis and Enhancement 14 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = inf • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 15 Run the model. The original video appears in the Video Viewer window. To view the video at its true size, right-click the window and select Set Display To True Size.
Image Enhancement The noisy video appears in the Video Viewer1 window. The following video is shown at its true size. The approximation of the original video appears in the Video Viewer2 window, and the artifacts of the processing appear near the edges of the video. The following video is shown at its true size. You have used the Read Binary File block to import a binary video into your model, the 2-D FIR Filter to remove periodic noise from this video, and the Video Viewer block to display the results.
7 Analysis and Enhancement blocks, see the Read Binary File, 2-D FIR Filter, and Video Viewer block reference pages in the Video and Image Processing Blockset Reference. For more information about the Filter Design and Analysis Tool (FDATool), see the Signal Processing Toolbox documentation. For information about the ftrans2 function, see the Image Processing Toolbox documentation.
Image Enhancement 3 Use the Image From File block to import the first image into the Simulink model. Set the File name parameter to pout.tif. 4 Use the Image From File1 block to import the second image into the Simulink model. Set the File name parameter to tire.tif. 5 Use the Contrast Adjustment block to modify the contrast in pout.tif. Set the Adjust pixel values from parameter to Range determined by saturating outlier pixels, as shown in the following figure.
7 Analysis and Enhancement This block adjusts the contrast of the image by linearly scaling the pixel values between user-specified upper and lower limits. 6 Use the Histogram Equalization block to modify the contrast in tire.tif. Accept the default parameters.
Image Enhancement This block enhances the contrast of images by transforming the values in an intensity image so that the histogram of the output image approximately matches a specified histogram. 7 Use the Video Viewer blocks to view the original and modified images. Accept the default parameters. 8 Connect the blocks as shown in the following figure. 9 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu.
7 Analysis and Enhancement • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 10 Run the model. The results appear in the Video Viewer windows.
Image Enhancement In this example, you used the Contrast Adjustment block to linearly scale the pixel values in pout.tif between new upper and lower limits. You used the Histogram Equalization block to transform the values in tire.tif so that the histogram of the output image approximately matches a uniform histogram. For more information, see the Contrast Adjustment and Histogram Equalization block reference pages in the Video and Image Processing Blockset Reference.
7 Analysis and Enhancement Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Color Space Conversion Video and Image Processing Blockset > Conversions 2 Constant Simulink > Sources 1 Divide Simulink > Math Operations 1 Histogram Equalization Video and Image Processing Blockset > Analysis & Enhancement 1 Product Simulink > Math Operations 1 Video Viewer Video and Image Processing Blockset > Sinks 2 3 Place the blocks so that your model resemb
Image Enhancement • Image signal = Separate color signals 5 Use the Color Space Conversion block to separate the luma information from the color information. Set the block parameters as follows: • Conversion = sR'G'B' to L*a*b* • Image signal = Separate color signals Because the range of the L* values is between 0 and 100, you must normalize them to between 0 and 1 before you pass them to the Histogram Equalization block, which expects floating point input in this range.
7 Analysis and Enhancement 9 Use the Product block to scale the values back to the 0 to 100 range. Accept the default parameters. 10 Use the Color Space Conversion1 block to convert the values back to the sR’G’B’ color space. Set the block parameters as follows: • Conversion = L*a*b* to sR'G'B' • Image signal = Separate color signals 11 Use the Video Viewer blocks to view the original and modified images. For each block, set the Image signal parameter to Separate color signals.
Image Enhancement As shown in the following figure, the model displays the original image in the Video Viewer1 window.
7 Analysis and Enhancement As the next figure shows, the model displays the enhanced contrast image in the Video Viewer window. In this example, you used the Histogram Equalization block to transform the values in a color image so that the histogram of the output image approximately matches a uniform histogram. For more information, see the Histogram Equalization block reference page in the Video and Image Processing Blockset Reference.
Pixel Statistics Pixel Statistics Video and Image Processing Blockset contains blocks that can provide information about the data values that make up an image. Blocks from the Statistics library, such as the 2-D Maximum and 2-D Autocorrelation blocks, can help you determine this information.
7 Analysis and Enhancement 3 Create a new Simulink model, and add to it the blocks shown in the following table.
Pixel Statistics Block Library Quantity Reshape Simulink > Math Operations 3 Matrix Concatenation Simulink > Math Operations 1 Vector Scope Signal Processing Blockset > Signal Processing Sinks 1 4 Place the blocks so that your model resembles the following figure. 5 Use the Image From Workspace block to import the RGB image from the MATLAB workspace.
7 Analysis and Enhancement 7 Use the Bus Creator block to combine the R, G, and B, signals into one signal so you can process it with one 2-D Histogram block. Set the Number of inputs parameter to 3. 8 Use the 2-D Histogram block to calculate the histogram of the R, G, and B values in the image. Accept the default parameters. The R, G, and B values input to the 2-D Histogram block are double-precision floating point and range between 0 and 1.
Pixel Statistics 10 Use the Reshape blocks to transform the row vectors output from the Bus Selector block into columns vectors. Set the Output dimensionality parameters to Column vector (2-D). 11 Use the Matrix Concatenation block to concatenate the R, G, and B column vectors into a single matrix so they can be displayed using the Vector Scope block. Set the Number of inputs parameter to 3. 12 Use the Vector Scope block to display the histograms of the R, G, and B values of the input image.
7 Analysis and Enhancement The Bus Selector block still needs to be connected. You cannot configure the parameters of this block until you connect an input signal to it. 14 Configure the Bus Selector block. Double-click the block. In the Signals in the bus pane, select signal3. Click Select to move signal3 to the Selected signals pane. Click OK. The Bus Selector block now has three output ports. 15 Connect the Bus Selector block to the Reshape blocks. 16 Set the configuration parameters.
Pixel Statistics The original image appears in the Video Viewer window. To view the image at its true size, right-click the window and select Set Display To True Size. 18 Right-click in the Vector Scope window and select Autoscale. The scaled histogram of the image appears in the Vector Scope window.
7 Analysis and Enhancement You have now used the 2-D Histogram block to calculate the histogram of the R, G, and B values in an RGB image. For more information about this block, see the 2-D Histogram block reference page in the Video and Image Processing Blockset Reference. To open a demo model that illustrates how to use this block to calculate the histogram of the R, G, and B values in an RGB video stream, type viphistogram at the MATLAB command prompt.
8 Example Applications Video and Image Processing Blockset blocks enable you to track objects in an image, stabilize a video stream, and compress images. Pattern Matching (p. 8-2) Learn how to track the motion of a sculpture in a video stream. Motion Compensation (p. 8-9) Explore the video compression and stabilization demo models. Image Compression (p. 8-11) Understand how to compress an image and view the result.
8 Example Applications Pattern Matching Pattern matching can be used to recognize and/or locate specific objects in an image. It can be accomplished using several techniques, one of which is correlation. Correlation provides a direct measure of the similarity between two images. Though sensitive to the scaling or rotation of objects, normalized correlation is robust to changes in lighting.
Pattern Matching Block Library Quantity Video Viewer Video and Image Processing Blockset > Sinks 1 Data Type Conversion Simulink > Signal Attributes 1 Constant Simulink > Sources 1 Mux Simulink > Signal Routing 1 2 Position the blocks as shown in the following figure. You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 3 Use the Read Binary File block to import a binary file into the model.
8 Example Applications 4 Use the Image Data Type Conversion block to convert the data type of the video to single-precision floating point. Accept the default parameter. 5 Use the Image From File block to import the image of the cat sculpture, which is the object you want to track. Set the block parameters as follows: • Main pane, File name = cat_target.
Pattern Matching Because you chose Valid for the Output size parameter, the block outputs only those parts of the correlation that are computed without the zero-padded edges of any input. 7 Use the Maximum block to find the index of the maximum value in each input matrix. Set the Mode parameter to Index. The block outputs the zero-based location of the maximum value as a two-element vector of 32-bit unsigned integers at the Idx port.
8 Example Applications 9 Use the Constant block to define the size of the image of the cat sculpture. Set the Constant value parameter to single([41 41]). 10 Use the Mux block to concatenate the location of the maximum value and the size of the image of the cat sculpture into a single vector. You use this vector to define a rectangular region of interest (ROI) that you pass to the Draw Shapes block.
Pattern Matching by single-precision floating-point values, a value of 0 corresponds to black and a value of 1 corresponds to white. 13 Connect the blocks as shown in the following figure. 14 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = inf • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 15 Run the simulation.
8 Example Applications As the video plays, you can watch the rectangular ROI follow the sculpture as it moves. In this example, you used the 2-D Correlation, 2-D Maximum, and Draw Shapes blocks to track the motion of an object in a video stream. For more information about these blocks, see the 2-D Correlation, Maximum, and Draw Shapes block reference pages in the Video and Image Processing Blockset Reference.
Motion Compensation Motion Compensation Motion compensation is a set of techniques that take advantage of redundancy in consecutive video frames. These techniques are used in video processing applications such as video compression and video stabilization. For both of these applications, motion compensation is a two-step process of detection and compensation. The detection step results in the specification of a motion vector that relates two consecutive video frames.
8 Example Applications This demo model detects motion by analyzing how much objects move between consecutive video frames. The model aligns two sequential video frames, subtracts them, and codes the residual. Video and Image Processing Blockset also contains a video stabilization demo model that you can open by typing vipstabilize at the MATLAB command prompt. The demo illustrates a motion stabilization technique based on the sum of absolute differences (SAD) method.
Image Compression Image Compression The examples in this section illustrate how to build a Simulink model that is capable of image compression. For image compression algorithms, the input image is divided into blocks and the two-dimensional DCT is computed for each block. The DCT coefficients are then quantized, coded, and transmitted. The receiver decodes the quantized DCT coefficients, computes the inverse two-dimensional DCT of each block, and then puts the blocks back together into a single image.
8 Example Applications Image Courtesy of MIT 3 Create a new Simulink model, and add to it the blocks shown in the following table. Block Library Quantity Image From Workspace Video and Image Processing Blockset > Sources 1 Block Processing Video and Image Processing Blockset > Utilities 2 Video Viewer Video and Image Processing Blockset > Sinks 2 4 Position the blocks as shown in the following figure.
Image Compression You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK. 5 Use the Image From Workspace block to import the intensity image into your model. Set the block parameters as follows: • Main pane, Value = I • Data Types pane, Output data type = double 6 Use the Video Viewer1 block to view the original intensity image. Accept the default parameters.
8 Example Applications The Block Processing block’s subsystem opens.
Image Compression You can drag blocks into this subsystem to process the submatrices. 8 Add the following blocks to your subsystem. Block Library Quantity 2-D DCT Video and Image Processing Blockset > Transforms 1 Selector Simulink > Signal Routing 1 9 Connect the blocks as shown in the following figure.
8 Example Applications 10 The 2-D DCT block takes the two-dimensional DCT of each submatrix. This process puts most of the energy in the image into the upper left corner of the resulting matrix. Use the default parameters. 11 Use the Selector block to extract the upper left corner of the submatrix.
Image Compression You are using the Selector block to compress the image by extracting the upper left corner of the submatrix, which contains the high energy image coefficients. You want to transmit only this portion of the submatrix because it requires less bandwidth than transmitting the entire submatrix. 12 Close the subsystem and the Block Processing dialog box. You have now configured the Block Processing and 2-D DCT blocks to compress an image for transmission.
8 Example Applications Viewing the Compressed Image In “Compressing an Image” on page 8-11, you compressed an image using the 2-D DCT and Selector blocks. Now, you can use the 2-D IDCT block to transform the image back to the time domain and view the result: 1 If you have not already done so, define an intensity image in the MATLAB workspace by typing I= imread('cameraman.tif'); I is a 256-by-256 matrix of 8-bit unsigned integer values.
Image Compression 4 Open the block’s subsystem by clicking Open Subsystem, and add the following blocks to it.
8 Example Applications 5 Connect the blocks as shown in the following figure. 6 Use the Image Pad block to zero pad the 4-by-4 submatrix back to its original 8-by-8 size.
Image Compression Because zeros are replacing the low energy transform coefficients, the output image is an approximation of the original image. 7 The 2-D IDCT block takes the inverse two-dimensional DCT of the submatrices. Accept the default parameters. 8 Close the subsystem and the Block Processing1 dialog box.
8 Example Applications 9 Use the Video Viewer block to view the compressed image. Accept the default parameters. 10 Connect the blocks as shown in the following figure. 11 Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows: • Solver pane, Stop time = 0 • Solver pane, Type = Fixed-step • Solver pane, Solver = discrete (no continuous states) 12 Run the model.
Image Compression The compressed image is displayed in the Video Viewer window. The compressed image is not as clear as the original image. However, it still contains many of its features. The following image is shown at its true size. In this example, you used the 2-D DCT, Image Pad 2-D IDCT, and Block Processing blocks to compress an image.
8 Example Applications reference pages in the Video and Image Processing Blockset Reference. For information on the Selector block, see the Simulink documentation. For more information on sharpening an image, see “Sharpening and Blurring an Image” on page 7-27.
Index A Index Accelerator mode 1-23 adding periodic noise to a signal 7-41 adjusting intensity image contrast 7-48 RGB image contrast 7-53 Adobe Acrobat Reader 1-8 algorithms bicubic interpolation 5-4 bilinear interpolation 5-3 nearest neighbor interpolation 5-2 angles rotation 5-6 annotating AVI files 2-9 arrays interpretation of 1-11 artifacts in an image 7-41 audio exporting to multimedia file 2-31 Autothreshold block to perform thresholding 4-7 AVI files annotating 2-9 cropping 2-18 exporting 2-6 impo
Index contrast increasing 2-27 controlling video duration 1-22 conventions column-major format 1-21 conversion color space 4-14 intensity to binary 4-2 R’G’B’ to intensity 4-14 correction of uneven lighting 6-11 correlation used in object tracking 8-2 counting objects 6-3 cropping AVI files 2-18 images 5-20 D data type support 1-28 data types 1-13 definition of intensity and color 1-13 demos in the Help browser 1-5 on MATLAB Central 1-6 on the Web 1-6 Periodic noise reduction 7-41 Video compression 8-9 Vi
Index lines in images 7-9 form of objects 6-2 frequency distribution of elements in an image 7-59 fspecial function 7-27 G gamma correction 4-14 geometric transformation 5-1 gradient components of images 7-2 H Help browser demos 1-5 documentation 1-7 histograms of images 7-59 I image compression 8-11 image credits 1-32 image data storage of 1-21 image rotation 5-6 image types 1-12 images binary 1-12 boundary artifacts 7-41 brightening 6-11 correcting for uneven lighting 6-11 counting objects in 6-3 crop
Index interpretation of matrices 1-11 irregular illumination 6-11 K key blockset concepts 1-11 knowledge user’s expected 1-8 L labeling objects 6-3 lightening images 6-11 location of lines 7-9 object edges 7-2 objects in an image 8-2 luma components applying highpass filter 7-27 applying lowpass filter 7-27 of images 4-19 luminance 4-19 morphology 6-1 opening 6-3 overview 6-2 STREL object 6-3 motion compensation 8-9 motion detection 8-9 multimedia files exporting 2-27 exporting audio and video 2-31 impo
Index interpolation 5-2 morphology 6-2 Video and Image Processing Blockset 1-2 P padding 7-41 pattern matching 8-2 performance improving 1-23 periodic noise removal 7-41 printing PDF documentation 1-8 processing in real time 1-24 product demos 1-5 products related 1-4 required 1-4 R R’B’G’ conversion to intensity 4-14 real-time processing 1-24 reception of an RGB image 4-19 reconstruction of images 8-11 reduction of image size 5-13 region of interest cropping to 5-20 visualizing 8-2 related products 1-4
Index summary of morphology 6-2 T techniques motion compensation 8-9 sum of absolute differences (SAD) 8-9 thresholding 6-11 thresholding operation 4-2 with uneven lighting 4-7 thresholding techniques 6-11 tracking of an object 8-2 transformation geometric 5-1 transmission of an RGB image 4-19 trimming images 5-20 true size 2-2 true-color images 1-12 tutorials 1-9 types of images 1-12 U uneven lighting correcting for 6-11 V vectors motion 8-9 video Index-6 adjusting display size 2-2 annotating AVI fil