Importing and Exporting Graphics MathCad Help

Reading and writing graphics files

Mathcad includes functions that allow you to read in a grayscale or color image as data. Once you have read an image into a Mathcad matrix, you can view it in Mathcad’s picture operator or apply image processing techniques or other mathematical manipulations. And once you finish processing a matrix, you may save the data to disk as an image for later use in Mathcad or in graphics applications.

While the functions described in this section are designed specifically to read and write image files, they share many of the general behaviors of the functions for reading and writing ASCII data described in Chapter 19, “Data Management.”

Reading, viewing, and writing BMP files

To import a bitmap (BMP format) file as data into Mathcad, use one of these functions:

You must use all uppercase letters to type these function names; alternatively, choose Function from the Insert menu and double-click on the function name in the scrolling list.

The argument you supply to READBMP or READRGB is a string expression–or a variable to which a string is assigned-as described in “Arguments to file access functions” on page 448. The string can correspond to either:  the name of a bitmap (BMP format) file in the working directory of the Mathcad worksheet you’re currently working on; or a full or relative path to a bitmap file located elsewhere on a local or network file system.

Each function returns a matrix of numbers used to represent the image. For example, to create a matrix M of numbers corresponding to the image in the color image file data2 . bmp, type either

M: =READBMP(“data2 .bmp”) or M: =READRGB(“data2 .bmp”)

In the first case M will be a grayscale representation of the image; in the second, M will consist of three submatrices corresponding to the red, green, and blue components of the color image.

As a second example, to read the grayscale image blur. bmpin the \DATA directory into the matrix B, pass the following string expression to READBMP

B: =READBMP(“c: \DATA\blur.bmp”)

“Variables and Constants,” for more information about creating string expressions. To divide the matrix for a color image into its red, green, and blue components, use the formulas shown in Figure 28-1 that use the submatrix function. In this example the color bitmap file monalisa. bmpis read into a grayscale matrix gray as well as the packed RGB matrix packed and then converted into three submatrices called red, green, and blue.

Reading in a color image as both grayscale and packed RGB

Reading in a color image as both grayscale and packed RGB

Once you have read an image file into Mathcad, you can use the picture operator to view it. First click in a blank part of your worksheet and then:

• Choose Picture from the Insert menu.
• Type the name of the matrix in the placeholder at the bottom left of the picture operator
• Click outside the picture region to see the image.

Importing and Exporting Graphics

Importing and Exporting Graphics

To display an image in color, you must provide the picture operator with the names of three matrices containing the red, green and blue color values of the image. Otherwise, the displayed image will be in grayscale. For example, to display the image, you would choose Picture from the Insert menu and type red, green, blue into the placeholder:

If you want to display only the red components of the image that was used for  you still must include the green and blue submatrices, but they both should contain only zeros. Keep in mind that the colors of the image that you ultimately see may be distorted to the extent that you don’t display 256 colors on your monitor.

If you have been working with submatrices for a color file, you can recombine them by defining an augment3(X, Y, Z) function:

augment3(X, y, Z) : = augment(X, augment(y, Z»

X, Y, and Z are the names of the submatrices and should have the same number of rows and columns. The resulting matrix will then have the same number of rows as X but three times the number of columns.

Once you finish processing a matrix, you may want to save it to disk for later use. To do this, use one of the following functions:

You can use Mathcad’ s augment function (or the augment3 function defined above) to combine submatrices at the time you are saving a file to your disk. For example, the following saves the sub matrices that were created in Figure 28-1 as one 24-bit color image file called nucolor. brnp:

WRITERGB(“nucolor.brnp”):=augrnent(red,augrnent(green,blue»

WRITEBMP and WRITERGB each take a string expression as described in “Arguments to file access functions” on page 448. The string can correspond to either:
• The name of a bitmap (BMP format) file in the working directory of the Mathcad worksheet you’re currently working on; or
• A full or relative path to a bitmap file located elsewhere on a local or network file system.

Posted on November 20, 2015 in Importing and Exporting Graphics

Share the Story

Back to Top
Share This