digital image representation...17/05/2010 compsci 373 - tutorial 4 pixel values data types...

38
17/05/2010 CompSci 373 - Tutorial 1 Digital Image Representation

Upload: others

Post on 28-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • 17/05/2010 CompSci 373 - Tutorial 1

    Digital Image Representation

  • 17/05/2010 CompSci 373 - Tutorial 2

    Defining an Image

    ● Image Grid– cols * rows

    ● Pixels– Contains a single

    code value

    (R,G,B) = (128, 128, 255)“light purple”

    rows

    cols

  • 17/05/2010 CompSci 373 - Tutorial 3

    Image Model

    ● A function – R is the rectangular image grid– V is the code value of a pixel (scalar or vector)– Maps an image grid to pixel values

    ● Thus can define a pixel uniquely with:

    f :RV

    x , y , f x , y

  • 17/05/2010 CompSci 373 - Tutorial 4

    Pixel Values

    ● Data Types (bit-depth)– Binary – Unsigned integers– Floating points

    ● Channels– 1 channel (scalar code value) = binary or grayscale image– 3 channel (vector code value) = colour image (e.g. RGB)– Operation on channels are independent

    Data type # of Values RangeBinary 2 [0 - 1]8-bit integer 256 [0 - 255]16-bit integer 65536 [0 - 65535]32-bit floating point

    “lots” [0.0 - 1.0]

    64-bit floating point

    “lots lots” [0.0 - 1.0]

  • 17/05/2010 CompSci 373 - Tutorial 5

    ● Colour– Represented by separate, independent channel values– Different representations (colour spaces) possible, e.g.

    ● RGB (or BGR ordering)● HSV (or HSL)● Lab● CMYK

    ● Resolution– Number of pixels

    ● 800 cols * 600 rows– Spatial - Number of pixels per measurement unit

    ● pixels (dots) per inch, pixels per mm

    Defining an Image

  • 17/05/2010 CompSci 373 - Tutorial 6

    RGB Colour

    R G B

  • 17/05/2010 CompSci 373 - Tutorial 7

    More Image Types

    ● Videos– A sequence of images

    ● 3D images– The image grid is extended to the 3rd coordinate– Time varying images (similar to videos)

    ● 4D videos– Time varying 3D images

  • 17/05/2010 CompSci 373 - Tutorial 8

    Example

    ● Defining an image– 1600 cols * 1200 rows– 16 bits per channel– 3 channels

    ● RGB colour model● Data stored in BGR order● (48 bits required per pixel)

    – Displayed at 72 dots per inch (dpi)● i.e. physical size = 22.22 in * 16.67 in

  • 17/05/2010 CompSci 373 - Tutorial 9

    Thresholding and Linear Mapping

  • 17/05/2010 CompSci 373 - Tutorial 10

    General Idea

    For every pixel's intensity as input, apply a (linear) function to generate an output intensity for that pixel

    for each pixel (x, y)output_value

    (x,y) = func ( input_value

    (x,y) )

  • 17/05/2010 CompSci 373 - Tutorial 11

    Threshold Functions

    ● Thresholding is like an if-else statement● Different types possible:

    – Threshold to binaryif (input > threshold) output = max // 255 for 8-bit images, or 1 for binaryelse output = 0

    – Threshold belowif (input < threshold) output = 0else output = input

  • 17/05/2010 CompSci 373 - Tutorial 12

    Linear Functions

    ● Change intensity of a pixel linearly● In general:

    – Gain (gradient) a– Bias (y-intercept) b

    ● In image processing, the output is clamped to the allowed pixel values (e.g. [0 - 255]).

    out x , y = a⋅in x , y b

    input

    output

  • 17/05/2010 CompSci 373 - Tutorial 13

    Examples

    ● Threshold below 128out = (in < 128) ? 0 : in

  • 17/05/2010 CompSci 373 - Tutorial 14

    Examples

    ● Reducing contrastout = 0.60 * in

  • 17/05/2010 CompSci 373 - Tutorial 15

    Examples

    ● Negation (8-bit)out = -1 * in + 255out = 255 - in

  • 17/05/2010 CompSci 373 - Tutorial 16

    Histograms

  • 17/05/2010 CompSci 373 - Tutorial 17

    Definition

    ● A histogram of an image is the frequency of each possible intensity value of the image.

    0

    500

    1000

    1500

    2000

    2500

    0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250

    H i = count { f x , y=i} for 0≤i≤I max

  • 17/05/2010 CompSci 373 - Tutorial 18

    Cumulative Histograms

    ● The cumulative histogram is the frequency of all pixels with intensity lower than a value.

    0

    10000

    20000

    30000

    40000

    50000

    60000

    70000

    0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250

    CH i = count { f x , y≤i} for 0≤i≤I maxCH i = ∑

    j=0

    i

    H j

  • 17/05/2010 CompSci 373 - Tutorial 19

    Example

    2 6 9 2 68 9 3 7 72 6 3 8 96 0 2 1 35 0 9 6 7

    Value Count0 21 12 43 34 05 16 57 38 29 4

    Total 25

    0

    1

    2

    3

    4

    5

    6

    0 1 2 3 4 5 6 7 8 9

    0

    5

    10

    15

    20

    25

    0 1 2 3 4 5 6 7 8 9

    H CH

  • 17/05/2010 CompSci 373 - Tutorial 20

    Normalised Histograms

    ● Express the histogram as observed probability.–

    where N = cols * rows is the number of pixels

    ● For the cumulative histogram, the normalised version is called the cumulative distribution function (cdf).

    H i =H iN

    cdf i = ∑j=0

    iH j

  • 17/05/2010 CompSci 373 - Tutorial 21

    Histograms

    ● Note:– CH(Imax) = N

    – cdf has range [0.0 - 1.0]– cdf(Imax) = 1.0

  • 17/05/2010 CompSci 373 - Tutorial 22

    Histograms

    ● Thought exercise:– What is the histogram of the clown image

    flipped upside down?

    ● An image is not uniquely defined by its histogram.

    ● Any permutation (rearrangement) of pixels of an image will give an identical histogram.

    – And thus, an identical cumulative histogram.

  • 17/05/2010 CompSci 373 - Tutorial 23

    Linear Mapping and Histograms

    0

    500

    1000

    1500

    2000

    2500

    0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 0

    10000

    20000

    30000

    40000

    50000

    60000

    1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176 181 186 191 196 201 206 211 216 221 226 231 236 241 246 251

  • 17/05/2010 CompSci 373 - Tutorial 24

    Linear Mapping and Histograms

    0

    500

    1000

    1500

    2000

    2500

    1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176 181 186 191 196 201 206 211 216 221 226 231 236 241 246 251 0500

    1000

    1500

    2000

    2500

    3000

    3500

    4000

    4500

    1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176 181 186 191 196 201 206 211 216 221 226 231 236 241 246 251

  • 17/05/2010 CompSci 373 - Tutorial 25

    Colour Histogram

    ● Different ways to express colours in histogram:– Convert to grey intensities– Graph each colour separately– Graph in 3D

  • 17/05/2010 CompSci 373 - Tutorial 26

    Histogram Equalisation

  • 17/05/2010 CompSci 373 - Tutorial 27

    Idea

    ● Stretch histogram such that frequencies are equally distributed.

    – The integral of the histogram (the cumulative histogram) increases linearly.

    – CH i ≈ i⋅KH i ≈ K

  • 17/05/2010 CompSci 373 - Tutorial 28

    How to do it

    ● Apply the CH or cdf as a Look Up Table (LUT).–

    ● However, we need to normalize the result to the maximum range available. Use contrast stretching.

    ● When CH(input) = CHmin

    , result = 0

    ● When CH(input) = CHmax

    , result = Imax

    result x , y = cdf input x , y ⋅I max

    result x , y =CH input x , y −CH min

    CH max−CH min⋅I max

  • 17/05/2010 CompSci 373 - Tutorial 29

    How to do it

    ● It does not matter whether we use the normalized version or not.

    ● CHmin

    is the first non-zero value.

    ● CHmax

    is always N = cols * rows.

    ● Round the result to the nearest integer in the case of integer data types.

  • 17/05/2010 CompSci 373 - Tutorial 30

    Example

    2 6 6 25 7 3 72 6 3 56 7 2 4

    Value H CH0 0 01 0 02 4 43 2 64 1 75 2 96 4 137 3 16

  • 17/05/2010 CompSci 373 - Tutorial 31

    Example

    0 5.25 5.25 02.92 7 1.17 7

    0 5.25 1.17 2.925.25 7 0 1.75

    Value CH out0 0 N/A1 0 N/A2 4 03 6 1.174 7 1.755 9 2.926 13 5.257 16 7

    out x , y =CH in x , y−CH minCH max−CH min

    ⋅I max

    out x , y = CH in x , y−416−4

    ⋅7

  • 17/05/2010 CompSci 373 - Tutorial 32

    Example

    0

    500

    1000

    1500

    2000

    2500

    0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255

  • 17/05/2010 CompSci 373 - Tutorial 33

    Demos

  • 17/05/2010 CompSci 373 - Tutorial 34

    Demos

    ● Linear mapping with GIMP

  • 17/05/2010 CompSci 373 - Tutorial 35

    Demos

    ● Building histograms with ImageJ macrowidth = getWidth();height = getHeight();histo = newArray(256);

    for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { v = getPixel(x, y); histo[v]++; }}

    for (i = 0; i < 256; i++) { print(i, histo[i], "\n"); // equivalent to print(i + “ “ + histo[i] + “ “ + “\n”);}

  • 17/05/2010 CompSci 373 - Tutorial 36

    Extras

  • 17/05/2010 CompSci 373 - Tutorial 37

    LUT Linear Mapping

    ● Linear Mapping can be inefficient.– Recompute linear equation for every pixel.

    ● Use a Look Up Table (LUT)– One input value can only have one output, no

    matter which pixel it is.– Build a LUT of all possible input values (e.g. [0 –

    255]) and their output.● LUT is faster

    if possible input values

  • 17/05/2010 CompSci 373 - Tutorial 38

    LUT Linear Mapping

    width = getWidth();height = getHeight();lut = newArray(256);

    // build LUTfor (i = 0; i < 256; i++) { lut[i] = 0.95 * i + 25;}

    for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { v = getPixel(x, y); setPixel(x, y, lut[v]); }}

    Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38