Swordrock's Blog

Just another WordPress.com site

Matlab 4 Matrices


Last 2 lessons  , i make an introduction to plotting and function building . They are living in the heart of Matlab  . I tell them first because next lessons i will work on lots of functions , including plotting functions . So we should understand how a function is written , how it works , and plotting style .

Today i will give you some arithmetic functions , mostly about matrix artihmetic .


You will see functions Magic , Sort , Abs and help elfun firstly .

Then i will share matrix functions like Zeros , Ones , Rand and Randn

You will see Concenation operation with martices and learn how to get bigger matrices by using small ones . Deleting rows or columns in a matrix . Taking transpose of a matrix , determinant (det  )value of a matrix and finally inverse (inv) of a matrix .

1)Magic function

Magic is a built in function which creates N-by-N matrix , from 1 to N^2

>> magic (4)

ans =

16     2     3    13

5    11    10     8

9     7     6    12

4    14    15     1

2)Sort function

Sort is also a matlab function which sorts the elements , unsurprisinglyJ

>> sort ([3+4i,4+3i])

ans =

4.0000 + 3.0000i   3.0000 + 4.0000i

We use i to refer complex numbers , it is sorted that way because of the angle values :

>> angle (3+4i)

ans =

0.9273

>> angle (4+3i)

ans =

0.6435

We can use “help elfun “ command to see other mathematic functions :

Elementary math functions.

<<help elfun

—————————-

Trigonometric.

sin         – Sine.

sind        – Sine of argument in degrees.

sinh        – Hyperbolic sine.

asin        – Inverse sine.

asind       – Inverse sine, result in degrees.

asinh       – Inverse hyperbolic sine.

cos         – Cosine.

……. (it is going on like that)

You can also type help specfun and help elmat for more specific functions  .

———————————————-

3)Abs

abs function gives the absolute value of the input .

>> abs (5+12i)

ans =

13

Matrices

There are also lots of matlab functions that generates matrices ;

1)Zeros : generates all 0 matrix

2)Ones : generates all 1 matrix


3)Rand : uniformly distributed random elements

4)Randn : normally distributed random elements

it is working that way .

Concatenation

Concatenation takes small matrices and outputs a bigger one .

Let’s save a matrix a and get a bigger matrix b by concenation :

Deleting Columns and Rows

You can also delete rows and columns by using empty brackets , matlab will automatically erase these rows or columns , lets do it on matrix b:

<<b (:,3:5) = []

This command will erase column 3,4 and 5 , command also tells matlab erasing row includes.

Transpose of Matrix

That is so easy on matlb if you want the transpose  of a matrix ,

a =

16     3     2     1

2     5     9    88

55    16    22    74

>> a’

ans =

16     2    55

3     5    16

2     9    22

1    88    74

Determinant of matrix

#You can take determinant of a square matrix by det function . But if you matrix is not a square , you will take that error :

>> det (a’)

??? Error using ==> det

Matrix must be square.

Inverse of matrix


# you can take the inverse of matrix by using inv function

inv (a)

a =

4018000000000000   c026000000000000   c014000000000000
c018000000000000   3ff0000000000000   4014000000000000
4018000000000000   c034000000000000   c008000000000000

>> format short
>> inv (a)

ans =

-0.8083   -0.5583    0.4167
-0.1000   -0.1000         0
-0.9500   -0.4500    0.5000

For a summary of this lesson , first we learn magic sort and abs functions’ usage . Then enter some matrix specific functions like zeros ones rand and randn .

After them , i wrote Concenation process for matrix  , determinant value and how to take inverse of a function .

If you get confused by mathematic of that lesson,  there are some useful links for matrices and linear algebra , Take Care :

http://ocw.mit.edu/courses/mathematics/18-06-linear-algebra-spring-2010/index.htm

http://tutorial.math.lamar.edu/Classes/LinAlg/LinAlg.aspx

http://mathforum.org/linear/linear.html

Reklamlar

Eylül 28, 2010 Posted by | Matlab | , , , , | Yorum bırakın

image processing


An introduction to image processing

Hi today i will introduce a funny and excellent area  : digital image processing .

#Extensive experimental work + software simulation-testing with lots of images .

#This area is an artificial intelligence area  .

#Digital image processing is an algorithm work on an image that you can take meaningful outputs .

There are lots of application of image processing today , like ;

—–

optical sorting , optical character recognition


face recognition


remote sensing


object recognition


biomedical image processing


—–

and lots of other types .

There is 3 types of this process , low , mid and high level process.

Low level process involves noise reducing , contrast enhancement and image sharpenings (primitive operations)

Mid level involves segmentation (partioning an image into regions or objects) , description of these objects to get the needed objects , classification (recognition) of individual objects . Mid level takes an image as an input and its outputs are edges , contours , identified objects  .

High level involves making decisions and sensing the object .

Reading Images

1) imread

In matlab, we use imread function to do this :

imread(‘filename’)

if we dont use a semi colon then we get this :

So we prefer to read a file in a vaiable and using a semicolon;

<<f=imread (‘0001.jpg’);


Now variable f has image array .

We can use imread function by using directory name :

<<f = imread(‘D:\myimages\holiday2010.jpg’);

2)size

We also have a function size , that gives row and column dimensions of an image  :

>> size (f)

ans =

480   342     3

3)whos

whos function used to ge information about image array and it is used like that :

>> whos f

Name        Size                Bytes  Class    Attributes

f         480x342x3            492480  uint8

Displaying images


1) imshow (f,G)

imshow function is used to display an image .

f is an image array and G is the number of intensity levels used to display it , i we dont use G , it defaults 256 levels .

>> imshow (f,[]) % sets variable low to minimum value of array f and high to  its maximum value

If we use imshow again to display another image , matlab replaces it with new one and close the existing image .

But you can use imshow o open an image at another window  like that :

<<imshow (f) , figure , imshow (g)

Displays both images .

Writing Images


1) imwrite

Images are written to disk by using imwrite  , imwrite takes 2 arguments.


%imwrite (f,’filename’) % general usage of imwrite

%imwrite (f,’0001.jpg’) % we write image ‘f’  to current directory .

İf filename contains no path info , then matlab will write the file to current directory .

2)iminfo

İmfinfo displays that information of image to us :

—————————————————————

>> imfinfo 0001.jpg

ans =

Filename: ‘0001.jpg’

FileModDate: ’16-Eyl-2010 22:59:31′

FileSize: 10989

Format: ‘jpg’

FormatVersion: ”

Width: 342

Height: 480

BitDepth: 24

ColorType: ‘truecolor’

FormatSignature: ”

NumberOfSamples: 3

CodingMethod: ‘Huffman’

CodingProcess: ‘Sequential’

Comment: {}

————————————————————–

You can also save this information into a variable .

İnf = imfinfo (‘0001.jpg’);

Then you can use inf for calculations like that :

—–

İnf = imfinfo (‘0001.jpg’);

İmage_bytes = K.Width * K.Height * K.BitDepth/8;

Compressed_bytes = K.FileSize ;

Compression_ratio = image_bytes / compressed_bytes

——

Working with imwrite can get bigger like that :

>> f = imread (‘0001.jpg’);

>> imwrite (f,’kristen.tif’,’compression’,’none’,’resolution’,[300 300])

>> imshow (‘kristen.tif’)

Then we get this result :

As it is seen , we control image resolution and compression by commands  .

Summary

Today we have learned  what is image processing , what is its applications , using ;

imread ,  size   , whos , imshow ,  imwrite and iminfo functions .


Some Useful Links for image processing

Matlab documentation : http://www.mathworks.com/help/techdoc/index.html

Matlab demos : http://www.mathworks.com/products/matlab/demos.jsp

Pixel colors : http://blogs.mathworks.com/steve/2006/04/07/all-about-pixel-colors-wrapping-up/

Fuzzy image processing  : http://pami.uwaterloo.ca/tizhoosh/fip.htm

Books list : http://www.imageprocessingplace.com/root_files_V3/publications.htm

See you at lesson-2 .

Eylül 19, 2010 Posted by | Image Processing | , , , , | Yorum bırakın