Top Banner
Lab 3 Data classes & flow control 1 م ي عظ ل د ا ب ع مان ي م/ ا
19
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab 3 Matlab

Lab 3

Data classes & flow control

العظيم/ 1 عبد ايمان م

Page 2: Lab 3 Matlab

2

Data classesAll numeric computations in Matlab are done using double

quantities frequently .• double : double-precision, floating-point numbers in the

approximate range -10308 to 10308

8 bytes per element• uint8: unsigned 8- bit integers

1 byte per elementRange 0 to 255

• logical:1 byte per element

) ( ) ( ابيض لون واحد أو اسود لون صفر قيمة يأخذ• char: characters 2 byte per element

double او uint8 :ان الصور ذات تدرج رمادي يكون مالحظه نوعها اما

Logical ) يكون نوعهاbinary ما الصورة ذات اللون ابيض أ ) واسود

العظيم/ عبد ايمان م

Page 3: Lab 3 Matlab

3

Image Types

• The toolbox supports four types of images:– Intensity images– Binary images– Indexed images– RGB images

Page 4: Lab 3 Matlab

4

Intensity Image

Binary Image

Page 5: Lab 3 Matlab

5

RGB Image

Indexed Image

Page 6: Lab 3 Matlab

6

Intensity Images

• Is a data matrix whose values have been scaled to represent intensities.

• When intensity image is of class uint8 they have integer values in the range [0,255].

• If the image is of class double the values are floating-point numbers.

Page 7: Lab 3 Matlab

7

Binary Images

• Is a logical array of 0s and 1s.• It stores an image as matrix but can only color

a pixel black or white.• It assign a 0 for black and 1 for white.• A numeric array is converted to binary array

using function logical which results in an array with logical 1s in locations where the input array had nonzero values, and logical 0s in places where the input array contained 0s.

Page 8: Lab 3 Matlab

العظيم/ عبد ايمان م 8

Indexed Images

An indexed image does not explicitly contain any color information. Its pixel values represent indicesمؤشرات into a color Look-Up Table (LUT). Colors are applied by using these indices to look up the corresponding RGB triplet in the LUT. In some cases, the pixel values of an indexed image reflect the relative intensity of each pixel.

RGB Image Interleaving

An RGB (red, green, blue) image is a three-dimensional byte array that explicitly stores a color value for each pixel. RGB image arrays are made up of width, height, and three channels of color information. Scanned photographs are commonly stored as RGB images.

The color information is stored in three sections of a third dimension of the image. These sections are known as color channels, color bands, or color layers. One channel represents the amount of red in the image (the red channel), one channel represents the amount of green in the image (the green channel), and one channel represents the amount of blue in the image (the blue channel).

Page 9: Lab 3 Matlab

9

Converting between data classes and image types

• Converting between data classes: Why need to convert data classes? because Numerical computation in matlab should be done

on double.

Converting between data classes is straightforward. The general syntax is:

B=data_class_name(A)Where :

data_class_name is one of data classes supported by matlab.ex: suppose A is an array of class uint8.B=double(A);

Page 10: Lab 3 Matlab

10

Notes: ex: Suppose C is an array of class double.

D=uint8(C)

Note that: If an array of class double has values outside the range [0,255], matlab uint8 function as in the last example converts to 0 all values less than 0, and converts to 255 all values that are greater than 255, numbers in between are converted to integers by discarding their fractional parts.

• So, proper scaling of double array so that its element are in range [0,255] is necessary before converting it to uint8.

Page 11: Lab 3 Matlab

11

The following table describe Functions that perform the scaling necessary to convert between image classes and types:

NameName Convert input toConvert input to Valid input image data Valid input image data classesclasses

im2uint8im2uint8 uint8uint8 Logical, uint8, Logical, uint8, uint16 ,doubleuint16 ,double

mat2graymat2gray Double in range Double in range [0,1][0,1]

DoubleDouble

im2doublim2doublee

doubledouble Logical, uint8, Logical, uint8, uint16 ,doubleuint16 ,double

im2bwim2bw logicallogical uint8, uint8, uint16 ,doubleuint16 ,double

rgb2grayrgb2gray Double,uint8Double,uint8 Double,uint8Double,uint8

im2uint1im2uint166

uint16uint16 Logical, uint8, Logical, uint8, uint16 ,doubleuint16 ,double

Converting between image classes and types

Page 12: Lab 3 Matlab

12

Example: Consider the following 2x2 image F of class double:

>> F = [-0.5 0.5 ; 0.75 1.5]F = -0.5000 0.5000 0.7500 1.5000

>> uint8(F)ans = 0 1 1 2

>> im2uint8(F)ans =

0 128 191 255

Im2uint8 sets to 0 all values in the input that are less than 0, sets to 255 all values in the input that are greater than 1, and multiplies all other values by 255. rounding the results of the multiplication to the nearest integer.

Remember the Difference between im2uint8 and uint8

Page 13: Lab 3 Matlab

13

mat2gray:Convert array of class double to array of class double scaled to range

[0,1] الحدود فى إال رقم أى بها ليس يصبح يعنى

(i. e) Convert matrix to intensity image. I = MAT2GRAY(A,[AMIN AMAX]) converts the matrix A to the intensity

image I. The returned matrix I contains values in the range 0.0 (black) to 1.0 (full intensity or white). AMIN and AMAX are the values in A that correspond to 0.0 and 1.0 in

I. Values less than AMIN become 0.0, and values greater than AMAX become 1.0. I = MAT2GRAY(A) sets the values of AMIN and AMAX to the minimum

and maximum values in A.

The input array A and the output image I are of class double.Note:Use mat2gray to convert double to double in range [0,1]

Page 14: Lab 3 Matlab

العظيم/ عبد ايمان م 14

im2double:Convert an input to class double in range [0,1] if input is class uint8 or logical. If input is double output is equal to input.

Consider the class uint8 image>> h= uint8([25 50; 128 200]);>>g=im2double(h)

The conversion simply done by dividing each value of the input array by 255

Ex:>>H = uint8 ( [255 50 ; 128 0] );>>G = im2double ( H ) G = 1.0000 0.1961 0.5020 0

Page 15: Lab 3 Matlab

15

G = im2bw ( F,T )• Produce binary (logical) image G from

intensity image F• Output has values of 0 for all pixels less than T.

And 1 for all other.• If T is omitted default value is 0.5• If input is uint8 it is divided by 255 then

convert like double• If the input is a logical array the output is

……………….

im2bw:

Page 16: Lab 3 Matlab

16

Example:

Consider the following double image F >>F = [ 1 2; 3 4];

>>G = mat2gray ( F )G = 0 0.3333

0.6667 1.0000

>>gb = im2bw ( G, 0.6)gb = 0 0

1 1>>gbd=islogical(gb)gbd =

1

Page 17: Lab 3 Matlab

العظيم/ عبد ايمان م 17

لتحويل الصورة من صوره ملونه الى صوره ثمذات تدرج رمادي

الى صوره التدرج الرمادى تحويل الصورة من (1أبيض و أسود )طريقة

Example1:% reading imageimg1=imread('city.jpg');% convert to grayimg2=rgb2gray(img1);%save image after convertImwrite(img2,’city2.jpg’);% convert to binaryimg3=im2bw(img2);figure(1),subplot(1,3,1),imshow(img1),title('The RGB image');figure(1),subplot(1,3,2),imshow(img2),title('The gray image');figure(1),subplot(1,3,3),imshow(img3),title('The binary image');

Page 18: Lab 3 Matlab

العظيم/ عبد ايمان م 18

لتحويل الصورة من صوره ذات تدرج رمادي الى (2صوره ذات اللون ابيض واسود طريقة )

Example2: img1=imread(‘sd.jpg');

[m,n]=size(img1);

for i=1:m for j=1:n if img1(i,j)>128 bim(i,j)=1; else bim(i,j)=0; end endEnd

figure(1),subplot(1,2,1),imshow(img1),title('the gray image');figure(1),subplot(1,2,2),imshow(bim),title('the binary image');

Page 19: Lab 3 Matlab

أ أشهد وبحمدك اللهم ال ن سبحانكإليك وأتوب أستغفرك أنت إال إله

المحاضرة المحاضرة نهاية نهاية