0% found this document useful (0 votes)
3 views

matlab print

The document provides instructions on executing commands in MATLAB, including variable assignment, displaying results, and using built-in functions. It covers image processing basics such as importing, displaying, and manipulating images, as well as techniques for segmentation and classification. Additionally, it discusses the benefits of converting images to grayscale for processing efficiency and simplicity.

Uploaded by

lpriya0843
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

matlab print

The document provides instructions on executing commands in MATLAB, including variable assignment, displaying results, and using built-in functions. It covers image processing basics such as importing, displaying, and manipulating images, as well as techniques for segmentation and classification. Additionally, it discusses the benefits of converting images to grayscale for processing efficiency and simplicity.

Uploaded by

lpriya0843
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

COMMANDS

You can execute commands by typing them in the Command Window and pressing the Enter key.

Multiply the numbers 3 and 5 with the command 2*3

Unless you specify an output variable, MATLAB stores results in a variable named
ans
>> 7 + 3
ans =
10
Assign the result of 2*3 to a variable named m,

m = 2*3
The equal sign (=) in MATLAB is the assignment operator, meaning that the value of the expression on
the right of the equal sign is assigned to the variable on the left.

When you enter x = 3 + 4, MATLAB first evaluates 3 + 4 and then assigns the result (7) to the
variable x.

Enter the command m = m + 1 to see what happens.

The Workspace browser (on the right) shows all the variables you created.

Create a variable named y that has the value of m/2.

When you enter a command without a semicolon at the end, MATLAB displays the result.

>> x = 5 + 1
x =
6

Optionally, you can add a semicolon to the end of a command so that the result is not displayed. MATLAB
still executes the command, and you can see the variable in the Workspace browser.

>> x = 5 + 1;

Enter k = 8 - 2; including the semicolon at the end.

The result won't appear in the Command Window, but you can see the value of k in the Workspace
browser.
You can recall previous commands by pressing the Up arrow key ↑ on your keyboard. Note that the
Command Window must be the active window for this keystroke to work.

Press the Up arrow key to return to the command m = 3*5, and before pressing Enter, edit the
command to be m = 3*k.
When you enter just a variable name at the command prompt, MATLAB displays the current value of that
variable.

In Task 4, you calculated the value of y using the value of m. Was y recalculated when you
modified m in Task 6?

Type just the variable name y at the command prompt, and press Enter.
The value of y is unchanged because MATLAB does not rerun previous commands in the Command
Window.

To recalculate y with the modified value of m, repeat the command y = m/2.

Try it out: use the Up arrow key to recall the command y = m/2, then press Enter. To see the new value
of y, remember not to use a semicolon at the end of the command.

NAMING VARIABLES
You can name your MATLAB variables anything you'd like

MATLAB variables are also case sensitive.

Create a variable named A with the value -2.

Use A and a to calculate

(a+A)/2
. Store the result in a variable named meanAa

Empty the workspace using the clear command.

Clear the Command Window using the clc command.

MATLAB contains a wide variety of built-in functions, such as abs (absolute value)
and eig (eigenvalues).

>> a = sin(-5)
a =
0.9589

Calculate the sine of x by using the sin function. Assign the result to a variable named y.
Calculate the square root of -9 by using the sqrt function. Assign the result to a variable
named z.
IMAGE PROCESSING BASICS

Receipt Identification Workflow

Course Outline
Working with Images in MATLAB
Import, display, and manipulate color and grayscale images.

Segmenting an Image
Create binary images by thresholding pixel intensity values.

Pre- and Postprocessing Techniques


Improve image segmentation by using common pre- and postprocessing techniques.

Classification and Batch Processing


Develop a metric to classify an image, and apply that metric to a set of image files.

Importing Images into MATLAB

In all MATLAB image processing workflows, the first step is importing images into the MATLAB
workspace.

Once in the workspace, you can use MATLAB functions to display, process, and classify the images.
You can import an image into memory and assign it to a variable in the MATLAB workspace for later
manipulation, display, and export.

A = imread("imgFile.jpg");

Ending the line with a semicolon stops MATLAB from displaying the values stored in A, which is helpful
because images typically contain millions of values.
TASK
Load image IMG_001.jpg into memory and assign it to variable I.

Remember to use a semicolon (;) so the values in I are not displayed.

Image data stored in a variable A can be displayed using the imshow function.

imshow(A)

TASK
Display the image data stored in I.

To develop a robust classification algorithm, you'll need additional images to analyze.


TASK
Load IMG_002.jpg into the variable I2 and display it.

Remember to use a semicolon (;) when reading the image file.

ou often want to view multiple images. For example, you might want to compare an image and a modified
version of that image.

You can display two images together using the imshowpair function.

imshowpair(A1,A2,"montage")
The "montage" option places the images A1 and A2 side by side, with A1 on the left and A2 on the right.
TASK
Display the images stored in I and I2 as a montage.

While receipts contain mostly grayscale colors, receipt images are still stored with three color planes like
other color images. Like sunlight, which is composed of a full spectrum of colors, the color planes
combine to create bright white paper.

Read IMG_003.jpg into the variable I, and then display it using imshow.

Many image processing techniques are affected by image size. The images you'll be classifying all have
the same size: 600 pixels tall and 400 pixels wide.

You can find the size of an array by using the size function.

sz = size(A)

The size function returns a vector. The first element in the vector is the number of rows in the array,
which corresponds to the height of the image. The second element is the number of columns in the array,
which corresponds to the width of the image.
TASK
Find the size of I and store the result in sz.
You can access individual color planes of an image by indexing into the third dimension. For example,
you can extract the green (second) color plane using the value 2 as the third index.

Ig = I(:,:,2);

TASK

Extract the red color plane of the RGB image I and store it in R. Display R using imshow.

Most images use the unsigned 8-bit integer (uint8) data type, which stores integers from 0 to 255. Bright
or brightly colored images contain pixel intensity values near 255 in one or more color planes.

The red plane of this receipt image has some pretty bright areas. Do you think any elements of the red
plane reach the maximum value of 255?

You can find the largest value in an array using the max function.

Amax = max(A,[],"all")

Using the "all" option finds the maximum across all values in the array. The brackets are required; they
are placeholders for an unused input.
TASK
Find the largest pixel intensity value of the red plane R and store the result in Rmax.

Dark areas, like receipt text, contain values close to zero. Based on the image of the red plane from Task
3, do you think the red plane has any elements with a value of 0?

You can find the smallest value in an array using the min function.

Amin = min(A,[],"all")
TASK
Find the smallest pixel intensity value of the red plane and store the result in Rmin.

Many common tasks can be completed more quickly using functions in Image Processing Toolbox. For
example, to extract all three color planes of an image array, you can use imsplit instead of indexing
into each plane individually.

[R,G,B] = imsplit(A);

You can display all three color planes at once using montage.

montage({R,G,B})
Try using imsplit to extract the color planes of I. Display all three color planes using montage.

Why Convert to Grayscale?

 When loaded into memory, a grayscale image occupies a third of the space required for an RGB
image.
 Because a grayscale image has a third of the data, it requires less computational power to process
and can reduce computation time.
 A grayscale image is conceptually simpler than an RGB image, so developing an image processing
algorithm can be more straightforward when working with grayscale.

In receipt images, like the one shown below, little information is lost when converting to grayscale. The
essential characteristics, like the dark text and bright paper, are preserved.

On the other hand, if you wanted to classify the produce visible in the background, the color planes are
essential. After converting to grayscale, it's hard to see the broccoli, let alone classify it.

The algorithm that you’ll implement in this training is built around identifying text, so color is not
necessary. Converting the images to grayscale eliminates color and helps the algorithm focus on the
black-and-white patterns found in receipts.

You can convert an image to grayscale using the im2gray function.


Ags = im2gray(A);

TASK
Convert the RGB image I to a grayscale image and store the result in gs. Display gs.

You might also like