1 of 82

CS111 – Fundamentals of CS�Lecture 9�Representing Images

Mohammad El-Ramly, PhD

Fall 2021-2022

http://www.acadox.com/class/63592

6EQYX4

2 of 82

دعاء

ربنا اغفر لنا و لإخواننا الذين سبقونا بالإيمان و لا تجعل فى قلوبنا غلا للذين آمنوا

2

3 of 82

3

4 of 82

Lecture Outline

  1. Representing Textual Data

* ASCII

* UNICODE

  1. Representing Images
  2. Representing Sound
  3. Image Compression

4

5 of 82

Reading for this lecture

  1. Computer Science: An Overview

Section 1.4, and 1.9

2. External links and sources to help

you understand the materials.

5

6 of 82

دعاء

ربنا آتنا فى الدنيا حسنة و فى الآخرة حسنة و قنا عذاب النار

6

7 of 82

كيف تنجح فى الكلية و فى حياتك

أولا: نم مبكرا عقب العشاء

1- وجعلنا الليل لباسا وجعلنا النهار معاشا

2- اللهم بارك لأمتي في بكورها

3- نام بكير فيق بكير شوف الصحة كيف بتصير

7

8 of 82

كيف تنجح فى الكلية و فى حياتك

ثانيا: إن الصلاة كانت على المؤمنين كتابا موقوتا

  1. وأقيموا الصلاة وآتوا الزكاة واركعوا مع الراكعين
  2. أرأيتم لو أن نهراً بباب أحدكم يغتسل منه كل يوم خمس مرات هل يبقى من درنه شيء ؟ قالوا: لا يبقى من درنه شيء. قال: (فذلك مثل الصلوات الخمس يمحو الله بهن الخطايا)
  3. أول ما يحاسب عليه العبد الصلاة

  • ؟؟؟؟؟؟

8

9 of 82

كيف تنجح فى الكلية و فى حياتك

ثالثا: أمك ثم أمك ثم أمك ثم أبوك

  1. ما من عمل يعجل الله ثوابه فى الدنيا مع ما يدخر لصاحبه من عظيم ثواب الآخرة مثل بر الوالدين وما من عمل يعجل الله عقابه فى الدنيا مع ما يدخر لصاحبه من عظيم عقابه فى الآخرة مثل عقوق الوالدين
  2. قال رسول الله - صلى الله عليه وسلم -: بابان معجلان عقوبتهما في الدنيا البغي والعقوق
  3. وعن عبد الله بن مسعود ـ رضي الله عنه ـ قال: " سألت رسول الله ـ صلى الله عليه وسلم ـ: أي العمل أحب إلى الله تعالى؟ " وفي رواية: " أي الأعمال أقرب إلى الجنة؟ قال: الصلاة على وقتها. قيل: ثم أي؟ قال: بر الوالدين. قيل: ثم أي؟ قال: الجهاد في سبيل الله "

9

10 of 82

كيف تنجح فى الكلية و فى حياتك

رابعا: حدد هدفك

  1. إذا لم تعرف أين تريد الذهاب فلن تصل أبدا
  2. إذا لم تشرع فى السير فى الطريق فلن تصل أبدا
  3. إذا لم تعمل لتحقيق هدفك فسيستخدمك الآخرون لتحقيق أهدافهم

10

11 of 82

كيف تنجح فى الكلية و فى حياتك

خامسا: اصنع الفرصة

  1. معظم الفرص لا تأتى و لكن تصنع
  2. كن مستعدا لاقتناص الفرصة حين تأتى و إلا ضاعت
  3. إذا لم تأتك الفرصة فاصنع فرصتك بنفسك
  4. إذا دخلت فى الزحام فتميز

https://www.youtube.com/watch?v=k5M5Fz1oLvo

goo.gl/DeZvQ2

��

11

12 of 82

كيف تنجح فى الكلية و فى حياتك

سادسا: المثابرة المثابرة المثابرة

كن نملة

  1. الأنجح ليسوا هم الأذكى و لا الأفضل فرصا بل هم الأكثر مثابرة
  2. تبنى عقلية النجاح Growth mindset
  3. https://www.ted.com/talks/carol_dweck_the_power_of_believing_that_you_can_improve?language=ar

  • ؟؟؟؟؟؟

12

13 of 82

How can we represent Images?

  • How to store images

like this?

1-13

https://mostafanageeb.com/

https://www.youtube.com/watch?v=OHZyrFlpMVE

14 of 82

How can we represent Images?

14

  • We can break it into pixels
  • The simplest form is black/white
  • We can represent a pixel by one bit

1 0

15 of 82

2. Image representation

  • An image can be divided into many tiny squares, called pixels.
  • Each pixel has a particular colour.
  • The quality of the picture depends on 2 factors:
    • the density of pixels.
    • The number of bits representing colours.
  • The resolution of an image is the pixels density.
  • The higher the resolution the more information the image contains.
    • 600 x 800 is better than 400 x 600

15

16 of 82

2. Image representation

  • Black and white images
  • Gray images
  • Colored images

  • Bitmap images
  • Vector images

16

17 of 82

B&W - Gray – Colored Pictures

17

18 of 82

Black & White Bitmap Images

19 of 82

000000001111111111111111111111111111111111111111111111111111111111111

111111110001111111111111111111111111111111111111111111111111111111001

111000000000011111111111111111111111111111111111111111111111111111001

110000000000001111111100000111111111111111111111111111111111111111111

111100000000011111111000001111111111111111111111111111111111111111111

111111111111111111110000001111111111111111000001111111111111111111111

111111111111111111110000001111111111111111000000111111111111111111111

111111111111111111110000011111111111111111000000011111111111111111111

111100000001111111110000011111111111111111100001111111111111111111111

111110000000011111110000011111111111111111111111111111111111101111111

111100000000001111110000001111111111111111111111111111111100000001111

111100000000001111110000001111111111111111111111111111111100000001111

111100011100001111110000001111111111111111111111111111111100000000111

111000011110000011110000001111111000011111111100001111111000111000001

111000011111000011110000001111110000011111111000001111111000111000001

110000011110000011110000001111110000011111111100000111110000111100001

110000001111000001111000001111111000001111111100000011110000111100001

110000000000000011111000011111111100001111111110000011110000000000001

111000000000000011111000011111111110000111111110000011110000000000001

110000000000000111111000111111111110000111111111000011100000000000001

111100000000000111110001111111111110000111111111000011111000000000001

111111111111111111110001111111111110000111111111000011111100000000001

111111111111111111111111111111111000001111111110000111111111111100001

111111111111111111111111111111111000011111111000000111111111111000001

111111111111111111111111101111100000011111100000001111111111100000011

111111111111111111111111100000000000110000000000011111100000000000111

111111111111111111111111110000000001111100000000111111110000000001111

111111111111111111111111110000000011111110000001111111111100000001111

111111111111111111111111111111000111111111000111111111111110000111111

Black & White Bitmap Images

20 of 82

Gray Bitmap Images

20

  • Each individual pixel (pi(x)cture element) in a graphic stored as a binary number
    • Pixel: A small area with associated coordinate location
    • Example: each point below is represented by a 4-bit code corresponding to 1 of 16 shades of gray

21 of 82

Example: 2-bit per pixel

  • 4=22 choices
    • 11 (off, off)=white
    • 10 (off, on)=light grey
    • 01 (on, off)=dark grey
    • 00 (on, on)=black

21

1

1

=

(white)

0

1

=

(dark grey)

0

1

=

(light grey)

0

0

=

(black)

22 of 82

Example: 4-bit per pixel

  • 16 =24 choices
    • 0000 ██
    • 0001 ██
    • 0010 ██
    • 0011 ██
    • 0100 ██
    • 0101 ██
    • 0110 ██
    • 0111 ██
    • 1000 ██

22

    • 1001 ██
    • 1010 ██
    • 1011 ██
    • 1100 ██
    • 1101 ██
    • 1110 ██
    • 1111 ██

23 of 82

Colour representation

  • Colours are represented with a sequence of bits.

  • 256 colours – how many bits?
    • Hint for calculating
      • To figure out how many bits are needed to represent a range of values, figure out the smallest power of 2 that is equal to or bigger than the size of the range.
      • That is, find x for 2 x => 256

  • 24-bit colour – how many possible colors?
    • Hints
      • 16 million possible colours

23

24 of 82

24-bits -- the True colour

    • 24-bit color is often referred to as the true colour.
    • Any real-life shade, detected by the naked eye, will be among the 16 million possible colours.

24

25 of 82

Methods Representing Images

  1. Bit map techniques
    • Pixel: short for “picture element”
    • RGB
    • Luminance (L) and chrominance (C)
      • (Brightness) L = R + G + B,
      • BC = L – B & RC = L - R
  2. Vector techniques
    • Scalable
    • TrueType fonts & PostScript Lumi Chrom Image

= Gray Image

26 of 82

26

27 of 82

RGB

  • RGB uses additive color mixing, because it describes what kind of light needs to be emitted to produce a given color.
  • Light is added together to create form from out of the darkness.
  • RGB stores individual values for red, green and blue.
  • (r,g,b)=>(0,0,0) black, (1,1,1) white [ranges 0 to 1]

28 of 82

28

29 of 82

Bitmap Images

  • Bitmap images (also called raster images) are made with pixels (picture element), which look like rectangles.
  • All the pixels, when combined for visual images, are called continuous tone images (contones).

30 of 82

Bitmap Images

  • Bitmap images are resolution dependent, and this must be taken into consideration when producing images of different size and quality.

31 of 82

Storing Bitmap: (1) Direct Coding

  • Storage space for each pixel to code the color
  • Use 3 bytes per pixel (1 for R, 1 for G and 1 for B) [Industry standard]
  • 256 different intensity level for each color

BYTE 1 R

BYTE 2 G

BYTE 3 B

Color Name

0

0

0

Black

0

0

1

Blue

0

1

0

Green

1

0

0

Red

Can we use less than 3 pixels?

32 of 82

Storing Bitmap: (2) Lookup table

  • Pixel values do not code colors directly
  • Refer to a table of color values
  • A table with 256 colors with RGB values

0

r

g

b

1

11111111

11111111

11111111

2

00100000

1000000

00100000

3

255

33 of 82

Storing Bitmap: (2) Lookup table

  • If there are less than 256 colours in the image then this bitmap will be the same quality as a 24 bit bitmap but it can be stored with one third the data.
  • Interesting colouring and animation effects can be achieved by simply modifying the palette, this immediately changes the appearance of the bitmap.

34 of 82

Bitmap Images

  • Resolution is the density of the pixel grid. It is the number of pixels in an image and is referred to as dpi or dots per inch. Resolution is based on the number of pixels in an image, which is determined, by its width and height of the pixels.
  • Example: Image size
  • = width in pixels x height in pixels

35 of 82

Bitmap Images

A pixel is the smallest display

element that makes up the images

seen on televisions and computer

monitors.

36 of 82

Advantages of Bitmap Images

  1. Bitmap images are easily converted to different formats.
  2. Bitmap images are easier to import into different software applications.

37 of 82

Advantages of Bitmap Images

  1. Bitmap images produce a variety of continuous tone images.
  2. Bitmap images are better suited for most high quality renderings and web page graphics.

38 of 82

Disadvantages of Bitmap Images.

  1. Bitmap images produce larger files sizes.
  2. Bitmap images have restrictions in regards to alterations and modifications such as scale, image distortion, and format conversion.

39 of 82

Bitmap Images

39

40 of 82

Vector Images

  • Vector images (also called outline images) are images made with lines, text, curves and shapes.

41 of 82

Advantages of Vector Images

  1. Vector graphics are resolution independent.
    • They can be output to the highest quality at any scale.
  2. Vector graphic images normally have much smaller file sizes than raster-based bitmaps.

42 of 82

Advantages of Vector Images

  1. Vector images are not limited to rectangular shapes like bitmaps.
  2. An image can be enlarged or reduced without affecting the quality of the image.
  3. There is no background unless it is placed behind the image as a layer

43 of 82

Advantages of Vector Images

  1. Vector images have the appearance of artistic form such as cartoons.
  2. Vector images can be easily converted to bitmap images.
  3. Lines and curves are easily defined and will always be smooth and retain their continuity.

44 of 82

Disadvantages of Vector Images

  1. The main disadvantage is they are not photorealistic.
  2. Vector images are usually filled with solid or gradient colors but lack in depth and appearance in the values and colors of a true continuous tone image.

45 of 82

45

46 of 82

3. Representing Sound

  • Sound signals are analog
  • Computer can store only digital signals
    • Ones 11111 and zeros 000000

47 of 82

Convert Analog to Digital

  • Analog

  • Digital

48 of 82

Analog-to-Digital-to-Analog

49 of 82

History

  • It wasn’t until 1988 the first sound card came to the market.
    • Introduced by Creative Labs
    • Created ability to have multiple channels playing

50 of 82

Characteristics of Sound

  • Amplitude شدة الصوت
  • Wavelength (w)
  • Frequency ( ) التردد

  • Hearing: [20Hz – 20KHz]
  • Speech: [200Hz – 8KHz]

51 of 82

Two Ways to Represent Sound

  • Sampling techniques
    • Used for high quality recordings
    • Records actual audio
  • MIDI
    • Used in music synthesizers
    • Records “musical score”

52 of 82

(1) Sampling�Digital Representation of Audio

  • Must convert wave form to digital
    • sample
    • quantize

53 of 82

The sound wave represented by the sequence 0, 1.5, 2.0, 1.5, 2.0, 3.0, 4.0, 3.0, 0

1-53

54 of 82

Sampling

  • Sampling is a method used to digitise sound waves.
  • A sample is the measurement of the amplitude at a point in time.
  • The quality of the sound depends on:
    • The sampling rate, the faster the better
    • The size of the word used to represent a sample.

54

55 of 82

Nyquist Theorem

For lossless digitization, the sampling rate should be at least twice the maximum frequency response.

  • In mathematical terms:

fs > 2*fm

  • where fs is sampling frequency and fm is the maximum frequency in the signal

56 of 82

Quantization

57 of 82

3-bit Quantization

A 3-bit binary (base 2) number has 23 = 8 values.

0

1

2

3

4

5

6

7

Amplitude

Time — measure amp. at each tick of sample clock

58 of 82

4-bit Quantization

A 4-bit binary number has 24 = 16 values.

0

2

4

6

8

10

12

14

Amplitude

Time — measure amp. at each tick of sample clock

59 of 82

Digitizing Sound

59

Zoomed Low Frequency Signal

Capture amplitude at these points

Lose all variation between data points

60 of 82

Digitizing Sound

60

Zoomed Low Frequency Signal

Capture amplitude at these points

Lose all variation between data points

61 of 82

The Digital Audio Stream

It’s just a series of sample numbers, to be interpreted as instantaneous amplitudes: one for every tick of the sample clock.

Previous example:

11 13 15 13 10 9 6 1 4 9 15 11 13 9

This is what appears in a sound file, along with a header that indicates the sampling rate, bit depth and other things.

62 of 82

16-bit Sample Word Length

A 16-bit integer can represent 216, or 65,536, values (amplitude points).

We typically use signed 16-bit integers, and center the 65,536 values around 0.

32,767

-32,768

0

63 of 82

Audio File Size

CD characteristics…

- Sampling rate:

44,100 samples per second (44.1 kHz)

How big is a 5-minute CD-quality sound file?

- Sample word length:

16 bits (i.e., 2 bytes) per sample

64 of 82

64

65 of 82

4. Data Compression (Section 1.9)

  • Compression: the process of coding that will effectively reduce the total number of bits needed to represent certain information.

65

66 of 82

4. Data Compression (Section 1.9)

  • If the compression and decompression processes induce no information loss, then the compression scheme is lossless; otherwise, it is lossy.

  • B0 – number of bits before compression
  • B1 – number of bits after compression

66

67 of 82

4. Data Compression (Section 1.9)

  • Lossy versus lossless
  • Run-length encoding (lossless)
  • Dictionary encoding (Includes adaptive dictionary encoding such as LZW encoding.) (lossless)

67

68 of 82

Run Length Encoding Example

4, 11

4, 9, 2, 1

4, 9, 2, 1

4, 11

4, 9

4, 9

5, 7

0, 17

1, 15

4.1

68

69 of 82

Run Length Encoding Example

4, 11

4, 9, 2, 1

4, 9, 2, 1

4, 11

4, 9

4, 9

5, 7

0, 17

1, 15

4.1

69

70 of 82

Run Length Encoding Example

4.1

70

71 of 82

Dictionary Encoding

  • Dictionary encoding can be used by word processors to compress text documents because the dictionaries already contained in these processors for the purpose of spell checking make excellent compression dictionaries.
  • In particular, an entire word can be encoded as a single reference to this dictionary rather than as a sequence of individual characters encoded using a system such as UTF-8.

4.4

71

72 of 82

Dictionary Encoding

  • A typical dictionary in a word processor contains approximately 25,000
  • entries, which means an individual entry can be identified by an integer in the range of 0 to 24,999.
  • This means that a particular entry in the dictionary can be identified by a pattern of only 15 bits. In contrast, if the word being referenced could be 4 or 8 bytes long

4.4

72

73 of 82

Compressing Images

  • GIF: Good for cartoons
  • JPEG: Good for photographs
  • TIFF: Good for image archiving

73

74 of 82

What are the basic graphic file formats?

Which file formats should you use and why?

How does the format affect the file size?

How does the format handle compression?

74

75 of 82

TIFF

Tagged Image File Format

  • Widely used cross platform file format also designed for printing.
  • A bitmap image format.
  • TIFF supports lossless LZW compression which also makes it a good archive format for Photoshop documents.

75

76 of 82

GIF

Graphics Interchange Format

  • Industry standard graphic format for on-screen viewing through the Internet and Web. Not meant to be used for printing.
  • The best format for all images except scanned photographic images (use JPEG for these).
  • GIF supports lossless LZW compression.

76

77 of 82

JPEG

Joint Photographic Experts Group

  • Preferred format for scanned photographic images for use over the internet or Web. Not meant for printing.
  • Not good for images with a lot of solid color, vector drawings, type, or line art.
  • JPEG compression is lossy! Save and archive the original before converting to JPEG.

77

78 of 82

JPEG

78

79 of 82

“Lossy” compression types:

  • JPEG

Original

Compressed

9:1

“averaging”

79

80 of 82

Compressing Audio and Video

  • MPEG
    • High definition television broadcast
    • Video conferencing
  • MP3
    • Temporal masking
    • Frequency masking

http://computer.howstuffworks.com/mp3.htm

80

81 of 82

Uncompressed

  • These are WAV (windows) or AIFF (mac) files that are considered ‘lossless’ files.
    • Roughly 10MB per minute
  • The initial recording of sound is usually done in this format before compression.
  • Audio CDs (not MP3 CDs) are uncompressed sound files.
    • It’s why you can only get 80 minutes vs 700 MB (roughly 700 minutes of mp3)

82 of 82

Homework …..

  • Read sections 1.4 and 1.9
  • Must read this link http://paulbourke.net/dataformats/bitmaps/

82