Computer Applications Reviewer Variable name can be combination of
letter and number
Matlab
Matlab is case sensitive (myvar is
a mathematical and graphical software different from Myvar or MYVAR, etc).
package with numerical, graphical and Use variable name that makes sense.
programming capabilities
>>who (shows defined variables)
extremely powerful higher level
programming language with built-in >>whos (shows more information on the
tools defined variable)
shorter name for “Matrix Laboratory”
>>clear (clears out all variables)
Hardware Requirements of Matlab
>>clear XXXX (clear only variable named XXXX)
Matlab 2020a
>>clear XXX1 XXX2 ... (clears variable named
latest version XXX1, XXX2,...)
with Intel or AMD processors with at
>>format short (display default 4 decimal
least four logical cores,
places)
4 GB of RAM (Random Access Memory)
3Gb of harddisk space2 >>format long (display 15 decimal places)
MATLAB 2012b >>format loose (default line spacing in the
command window)
To support older computers with lower
specifications >>format compact
>> (referred to as the prompt and can be found ... (3 dots) = continuation operator used for
in the command window) very long expression to continue on the enxt
line
>> demo (will show examples and help in
aseparate window) Random number
>> help XXXX (will show help for the function >>rand (generates a random number in the
XXXX) open interval (0,1))
>> quit >>round(rand*10) (generates a random
number in the open interval (0,10)
>>exit (quit or exit will exit Matlab)
Character- denoted by a singke quote.
Matlab Windows:
ex. ‘a’
command window
current folder window String- A series of Characters
workspace windows
command history window ex. ‘hello world’
How to store data (values) in Matlab The For Loop
Variables- stores value or data necessary to repeat statement/s in a
script/function.
>> variablename = value or expression
General Form of For Loop:
Semicolon- the output will not be displayed
ans- default variable name
arrow up- scroll to previously used commands
arrow left- modify the expression
Naming Variables:
Variable name should start with a letter
Nested For Loops-the action of a loop is commands are interpreted rather than
another loop complied.
Matlab Scripts knows as Matlab Code
Files (M-flies, with a file extension
of .m) consists of a sequence of Matlab
instructions.
Scripts can be executed (run) by
entering the name of the script (the M-
file without the .mextension)
Two ways of viewing a script
While Loops
1. opening the M-file in the editor
conditional loop 2. use the type command.
to repeat action when ahead of time it >>type scriptname
is not known how many times the
action will be repeated. Running/Executing a Script
General form of while loop >>scriptname
while condition Input Function
action prompts the user to neter the necessary
inputs.
end
>>variablename(‘Your input instruction or
Multiple Conditions in a while Loop parameter statement here’)
If the input is a string, “s” must be
added as the second argument,
>>variablename(‘Enter a character: ‘, ‘s’)
Algorithms- sequence of steps, sequential lines Note: Space is considered a character.
of codes needed to perform a certain operation
Output Statements- displays the results of the
or solve a particular problem.
command/expressions
Basic Algorithm consist of 3 basic steps:
disp - display the output of an expression or
1. Get the input/s command.
2. calculate the reslt/s
3. Display the results
Computer Program – a sequence of
instructions, written in a particular language
written to accomplish a task.
Execute/run a program – the computer follows
fprintf function – used for formatted output
the instruction (lines of codes) in the given
display. It uses place holders, new line character
sequence.
and many more.
Original lines of instructions are
Place holders:
referred to as source code
executable program output of the %d integer (decimal integer)
compiler is referred to as the object
%f float real numbers)
code,
line by line %c character (one character only)
Matlab Scripts %s string of characters
New line Character (\n)
-the next output will be moved to the next line elseif condition 3
action 3
end
The Switch statement -can be used when an
expression is tested to see whether it is equal to
one of several possible values
More Types of User-defined Functions
Functions that returns more than one
value
If Statements- chooses whether another
statement or group of statements is executed
or not.
if condition
action
end
Condition – a relational expression that is
conceptually or logically true or false.
action statement – will be executed if the
condition is true
Matlab Program Organization
If-Else Statements -the if statement chooses
Modular Programs- solution is broken
whether or not an action is executed.
down into muddles each implemented
if condition as a function.
action 1 Subfunctions
else Primary function –the function calling a
subfunction
action2
Both primary and subfunction are
end stored in the same code file
action 1 is executed if true Applications of Matlab
action 2 is executed if condition is false Images as matrices
Nested if-else statements -used to choose from Image Processing Toolbox convention is to
more than two options display 0 as black and 1 as white.
the elseif statement- another way of nesting if- Reading images
else statement to choose from more than two
options
if condition1
action 1
elseif condition2
imread(‘filename’)
action2
>> f = imread(‘baga.jpg’) binary images- black and white
indexed images- grayscale
>>imshow(f) %shows the image
RGB images- 3 matrices that overlaps
>>imshow(f, [low high])
Converting between image types
ex.1
>> imshow(f, [100 200])
this means that all values 0-99 are replaced by
100 and all values 201-255 are replaced by 200
ex. 2 displays both images
>>imshow(f), figure, imshow(f,[50 200])
This example shows how to automatically
detect circles or circular objects in an image
shows the original img shows the processed img andvisualize the detected circles.
ex. 3 Step 1: Load Image
>>imshow(f, [ ]) rgb = imread('coloredChips.png');
imshow(rgb)
the brackets sets the lowest and highest value
in the matrix Step 2: Determine Radius Range for Searching
Circles
>>pixval %specifies the value of each
pixel Find the appropriate radius range of the circles
using the drawline function.
>>size(f) %shows pixel length and width
d = drawline;
>>whos(f) %shows additional info
To save the file:
>>imwrite(f, ‘filename’)
>>imwrite(f, ‘filename.jpg’,’quality’,q)
whereas ‘q’ sets the quality of the image, 100
being the highest quality.
Data Classes
The length of the line ROI is the diameter of the
chip.
pos = d.Position;
diffPos = diff(pos);
diameter = hypot(diffPos(1),diffPos(2))
diameter = 45.3448
Step 3: Initial Attempt to Find Circles
gray_image = rgb2gray(rgb);
imshow(gray_image)
parameter 'ObjectPolarity'- set to bright or
Image types dark
intensity images- grayscale
parameter 'Sensitivity'- higher 'Sensitivity' low value (closer to 0) is more
permissive and includes even the
value sets the detection threshold lower and
weaker edges
leads to detecting more circles.
Step 4: Increase Detection Sensitivity
Step 5: Draw the Circles on the Image
function viscircles-to draw circles on the image.
Step 11: Draw 'Dark' and 'Bright' Circles
Together
Step 6: Use the Second Method (Two-stage) for
Finding Circles
Step 7: Why are Some Circles Still Getting
Missed?
The yellow chips do not have strong contrast
with the background.
imshow(gray_image)
Step 8: Find 'Bright' Circles in the Image
Step 9: Draw 'Bright' Circles with Different
Color
-Draw the bright circles in a different color, by
changing the 'Color' parameter in viscircles.
Step 10: Lower the Value of 'EdgeThreshold'
uses only the edge pixels in the image
controls how high the gradient value at
a pixel has to be before it is considered
an edge pixel
A high value (closer to 1) for this
parameter will allow only
the strong edges (higher gradient
values) to be included