sysmod lab2-toftenes wilhelmsen

12

Upload: aldinei-aragao

Post on 20-Jul-2016

234 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SYSMOD Lab2-Toftenes Wilhelmsen

System Modeling with Simulink

LABORATORY 2

Jesper Toftenes, Christian Wilhelmsen

April 27, 2012

Page 2: SYSMOD Lab2-Toftenes Wilhelmsen

Laboratory Goals

In the laboratory 1 you got familiarized with a system modelling tool 'Simulink' in MATLAB. You learnedto model dynamic systems and to model (systems expressed by) mathematical equations by using thevarious block sets in Simulink. In the second lab you will learn modelling machine vision systems byusing `Image and Video Processing' (IVP) Blockset. Simulink's IVP Blockset o�ers a variety of graphicaltools for image acquisition; processing and analysis. You will also learn importing the image data fromMATLAB workspace to Simulink model and exporting it back in the workspace after certain processingin Simulink.

After this lab you should be able to use:

i. IVP tools for modelling Image Processing and Machine Vision Systems.ii. Using User-de�ned function blocks in order to utilize powerful Matlab functions in the SimulinkModel.iii. Using Simulink callback functions and building data paths from and to the Matlab workspace.

-Muhammad Amir Yousaf

Page 3: SYSMOD Lab2-Toftenes Wilhelmsen

TASK

ONE

1.1 Task description

Invert input image into its negative.Build a Simulink model similar to the block diagram given below.

Figure 1.1: Given block diagram

1.2 Execution

The given diagram in �gure 1.1 was edited in to the Simulink model in �gure 1.2 on the following page.The model consist of 4 unique blocks: Image from �le, Color Space Conversion, MATLAB function andTo Video Display. The �rst block imports an image from a location in the �le system. This sends the R(red), G (green) and B (blue) values to the next block. The Color Space Conversion block converts the3 vector values in to intensity values in one single vector, which is passed on to the MATLAB function.The MATLAB function does only subtract the imported value u from 255, and is shown in listing 2.2on page 5.

To understand why the image is inverted through this function block, we have to understand that eachpixel value spans from 0-255 intensity grade. In a greyscale/monochrome image, this only correspondsto the intensity of white or black. The higher the value, the more brighter the image. So, to get atotally white image, where all pixels will have the value 255, we subtract this from 255 and get 0, hence,we have the inverted value of the pixel.

The result of this conversion is shown in �gure 1.3.

2

Page 4: SYSMOD Lab2-Toftenes Wilhelmsen

1.2. EXECUTION TASK 1.

function

To Video

DisplayImage

Original

To Video

DisplayImage

MATLAB Function

u y

fcn

Image From File

L2T1I1.jpg

R

G

B

Color Space

Conversion1

R'G'B' to

intensity

R'

G'

B'

I'I'I'

uint8 [526x528]

uint8 [526x528]

uint8 [526x528]

[526x528]

[526x528]

uint8 [526x528] uint8 [526x528]

Figure 1.2: Simulink model for task

Figure 1.3: Inverted and original image side by side

3

Page 5: SYSMOD Lab2-Toftenes Wilhelmsen

TASK

TWO

2.1 Task description

In the second task, a Simulink model will import image data from MATLAB workspace, will invert theimported image data using the same inversion block as in task 1 and then export it back to workspace.

Use the callback function to read the image from �le, do some pre-processing and load the imagevariable in Matlab workspace.

2.2 Execution

Start script The pre-processing script start_script.m is shown in listing 2.1 on the following page. Thescript is made portable by letting the user of the script change the value of the variable image_importif he or she wants to use another image, independent of image size. The values of image_length andimage_width will handle this automatically, and the only thing the user has to change is the numberof samples used in the Simulink model �le (�gure 2.1). According to the comment on line 38 in listing2.1, the number of samples has to be (image_width × image_length)− 1 to export an array with thecorrect dimensions.

Since we only are working with monochrome images, we only need one of the colors. the 3-dimensional array which the image consists of, is cut down to only two dimension on line 26. Thisis then shaped in to a 1-dimensional array, where the number of rows is decided by the image size. Sincewe are exporting this array to Simulink, we have to add timestamps to each variable as shown in thecode, and typecast the array from unsigned 8-bit integer to a double (double-precision �oating-point).

Simulink model The Simulink model �le consists of three blocks: FromWorkspace, MATLAB functionand To Workspace as shown in �gure 2.1. The �rst block imports the variable timestamped_image

and sends it to the second block, which contains the code snippet shown in 2.2 on the following page,which inverts the monochrome data, pixel by pixel. The output from this block is then sent to the thirdblock, which generates and exports a variable inverted_1column_image to MATLAB workspace to behandled by stop_script.m.

inverted image

inverted_1column_image

MATLAB Function

u y

fcnFrom

Workspace

timestamped_image

Figure 2.1: Simulink model task 2

4

Page 6: SYSMOD Lab2-Toftenes Wilhelmsen

2.2. EXECUTION TASK 2.

1 %%%%%%%%%%%%%%% Lab 2 Task 2 : S t a r t s c r i p t %%%%%%%%%%%%%%%%

3 % This s c r i p t impo r t s an image .% I t c o n v e r t s t h i s image and add t imestamps to the image

5 % be f o r e i t i s s e n t to a s imu l i n k s chemat i c which r e t u r n% an i n v e r t e d v e r s i o n o f the image .

7 % The image i s c onve r t ed back to a g r a p h i c a l v i ewab l e% format i n the s c r i p t : s t o p_s c r i p t .m

9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

11

% Change the name f o r use w i th o th e r images13 image_import = ( ' L2T3I1 . j pg ' ) ;

% Read i n the image to v a r i a b l e ' o r i g i n a l '15 o r i g i n a l = imread ( image_import ) ;

% Put s i z e o f o r i g i n a l i n v a r i a b l e ' u '17 u=s i z e ( o r i g i n a l ) ;

% Element 1 and 2 i s l e n g t h and width o f the image19 image_length = u ( 1 ) ;

image_width = u ( 2 ) ;21 image_s ize = ( image_length ∗ image_width ) ;

23 % Each p i x e l has R , G and B (3 d imen s i o n a l ) , t h i s removes 2% of the d imens i on s ( c o l o r s ) so we on l y hand l e one c o l o r

25 % in our monochrome imagesone_dim_or ig ina l=o r i g i n a l ( : , : , 1 ) ;

27 % Reshape the 64 x64 image i n t o a one−column ar ray , 64∗64=4096reshaped_image = re shape ( one_dim_or ig ina l , 1 , image_s ize ) ;

29 % Transpose the a r r a y from 1x4096 to 4096 x1 ( one l ong column )reshaped_image = reshaped_image ' ;

31 %Typecast reshaped_image to doub l ereshaped_image = doub le ( reshaped_image ) ;

33 % Create a r r a y w i th 4096 t imestampst imestamps =[1: image_s ize ] ' ;

35 % Create a t imestamped image a r r a ytimestamped_image=[ t imestamps reshaped_image ] ;

37

% run s imu l i n k wi th ( image_width∗ image_length )−1 samples b e f o r e runn ing39 % stop s c r i p t

Listing 2.1: start_script.m

1 f u n c t i o n y = fcn ( u )y = 255−u ;

Listing 2.2: Matlab function in simulink model

5

Page 7: SYSMOD Lab2-Toftenes Wilhelmsen

2.2. EXECUTION TASK 2.

Stop script After the Simulink model has executed, the stop script in listing 2.3 starts with manipula-tion of the inverted_1column_image array. Since this is a monochrome image, we copy this variablein to a 3-column array as shown on line 12. We then reshape it in to a 64x64x3 sized array beforeconverting it back to unsigned 8 bit (as the original was before converting to double).

The array inverted_image_6464 can now be plotted as an image. For reference we print out theoriginal side by side with the inverted image in a subplot as shown in �gure 2.2 on the following page.

%%%%%%%%%%%%%%% Lab 2 Task 2 : Stop s c r i p t %%%%%%%%%%%%%%%2 %% This s c r i p t t a k e s the v a r i a b l e inverted_1column_image

4 % gene r a t ed by s imu l i n k a f t e r impo r t i n g image data from% s t a r t_ s c r i p t .m. I n the end i t w i l l p r i n t a g r a p h i c a l

6 % view o f both the o r i g i n a l s ou r c e image and the i n v e r t e d% image inverted_image_6464

8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

10

% Reshape the i n v e r t e d one column image i n t o a 4096 x3 a r r a y12 i nve r ted_image_3co l=[ inverted_1column_image . . .

inverted_1column_image inverted_1column_image ] ;14 % Reshape i n to 64 x64 a r r a y

inverted_image_6464=re shape ( inverted_image_3co l , . . .16 image_length , image_width , 3 ) ;

% Typecast u i n t 818 inverted_image_6464=u i n t 8 ( inverted_image_6464 ) ;

20 % Create a 1x2 s ubp l o t and p r i n t image on p o s i t i o n 1s ubp l o t ( 1 , 2 , 1 ) ;

22 image ( inverted_image_6464 ) ;% I n s e r t t e x t above image

24 t e x t (0 ,−10 , ' I n v e r t e d image ' ) ;% Place next image on p o s i t i o n 2 i n s ubp l o t

26 s u bp l o t ( 1 , 2 , 2 ) ;image ( o r i g i n a l ) ;

28 % I n s e r t t e x t above imaget e x t (0 ,−10 , ' O r i g i n a l image ' ) ;

30 % Pr i n t message to u s e rmys t r i ng=[ 'No more work to do , e v e r y t h i n g went OK. See f i g u r e 1 ' . . .

32 ' f o r images ' , ' Program f i n i s h e d ' ] ;msgbox ( myst r ing , ' F i n i s h e d . . . ' ) ;

Listing 2.3: stop_script.m

6

Page 8: SYSMOD Lab2-Toftenes Wilhelmsen

2.2. EXECUTION TASK 2.

Inverted image

100 200 300 400

50

100

150

200

250

300

350

Original image

100 200 300 400

50

100

150

200

250

300

350

Figure 2.2: Original and negative image

7

Page 9: SYSMOD Lab2-Toftenes Wilhelmsen

TASK

THREE

3.1 Task description

Download the Simulink model �le from the course web page:The model reads an image �le and cam mark any region as Region of Interest(RoI) with a rectanglewhich can be positioned with sliding gain buttons.

1. Read image L2T3I1.jpg and by sliding the gain buttons select a region as shown in �gure(withgreen rectangle). See 3.2 for the area we used during the test

2. While making this model I made a minor mistake that is preventing the moving rectangle to reachat the shown position. Find it out and correct it. Hint: See the data types used in related part ofthe model.

3. Plot the histogram for the selected region using histogram block in IVP.

4. Read another image L2T3I2.jpg and plot histogram for same region.

5. Find out SNR for both images.

3.2 Execution

1. To load the image we must remember to change the path to our own workspace in matlab, sothat the similink model block can �nd our image. The upper limits to the sliding block is alsocon�gured for another image. We must recon�gure the image width/length into the correspondingsliding buttons. For our image we use 360 x 480.

2. The error using the sliding buttons is simply a data-type error. The output from the data conversionblock to the large image is using uint8. This limits the Region of Interest to maximum 255 pixelwidth and length. Since our image is 360x480 we must increase this to uint16. This shouldprovide us with a large enough number for any normal images. We can change the datatype bydouble-clicking on the data conversion block and changing the output data type.

3. From simulink we can plot the histogram. See �gure 3.1

4. The original marked image is shown in �gure 3.2

5. To �nd the SNR for both images, we use a shortcut and say that signal and noise values areapproximately the average of the signal, close to 0, and the average of the noise. We did a FFTwith the Spectrum Scope block in simulink. This gives us a FFT of the two images. This givesus a SNR as follows

8

Page 10: SYSMOD Lab2-Toftenes Wilhelmsen

3.2. EXECUTION TASK 3.

Figure 3.1: Histogram of the two pictures

Figure 3.2: The area marked during the test

9

Page 11: SYSMOD Lab2-Toftenes Wilhelmsen

3.2. EXECUTION TASK 3.

-25 -20 -15 -10 -5 0 5 10 15 20 25

-40

-30

-20

-10

0

10

20

30

40

Frame: 2 Frequency (Hz)

Mag

nitu

de-s

quar

ed, d

B

Figure 3.3: FFT of the sample image

SNR(L2T3I1.jpg) =45dB

15dB= 3 (3.1)

SNR(L2T3I2.jpg) =45dB

10dB= 4.5 (3.2)

10

Page 12: SYSMOD Lab2-Toftenes Wilhelmsen

CONCLUSION

This exercise has given us better insight on manipulating images with the use of MATLAB and Simulink.The use of di�erent data types, reshaping of image vectors and seeing how an image is built up hasbeen very useful in future work with digital machine vision and modeling of such systems.

With close to no experience on the �eld, this exercise proved to be a nice way on getting betterknowledge on the matter of digitally manipulating image data.

11