Color Image Perception, Representation and Contrast...

Post on 09-Sep-2018

221 views 0 download

transcript

Color Image Perception, Representation and Contrast Enhancement

Yao Wang Tandon School of Engineering, New York University

© Yao Wang, 2016 EL-GY 6123

Why Study Image and Video Processing?

•  Purpose of image and video processing –  Improvement of pictorial information for human interpretation –  Compression of image and video data for storage and

transmission –  Preprocessing to enable object detection, classification, and

tracking •  Typical application areas

–  Television Signal Processing –  Satellite Image Processing –  Medical Image Processing –  Robotics –  Visual Communications –  Law Enforcement –  Etc.

2

Breakdown of the Course

•  Image processing basics •  Video processing basics •  Image and video compression •  Stereo and multiview image/video basics

© Yao Wang, 2016 EL-GY 6123 3

© Yao Wang, 2016 EL-GY 6123

Today’s Lecture

•  Color perception •  Color production using primary colors •  Color specification •  Color image representation •  Image capture and display •  Image contrast measurement using histograms •  Image contrast enhancement

4

© Yao Wang, 2016 EL-GY 6123

Color Perception and Specification

•  Light -> color perception •  Human perception of color •  Type of light sources •  Trichromatic color mixing theory •  Specification of color

–  Tristimulus representation –  Luminance/Chrominance representation

•  Color coordinate conversion

5

© Yao Wang, 2016 EL-GY 6123

Light is part of the EM wave

from [Gonzalez02]

6

© Yao Wang, 2016 EL-GY 6123

Illuminating and Reflecting Light

•  Illuminating sources: –  emit light (e.g. the sun, light bulb, TV monitors) –  perceived color depends on the emitted freq. –  follows additive rule

•  R+G+B=White

•  Reflecting sources: –  reflect an incoming light (e.g. the color dye, matte surface,

cloth) –  perceived color depends on reflected freq (=emitted freq-

absorbed freq.) –  follows subtractive rule

•  R+G+B=Black

7

© Yao Wang, 2016 EL-GY 6123

Eye Anatomy

From http://www.stlukeseye.com/Anatomy.asp

8

© Yao Wang, 2016 EL-GY 6123

Eye vs. Camera

Camera components Eye components

Lens Lens, cornea

Shutter Iris, pupil

Film Retina

Cable to transfer images Optic nerve send the info to the brain

9

© Yao Wang, 2016 EL-GY 6123

Human Perception of Color

•  Retina contains photo receptors –  Cones: day vision, can perceive color

tone •  Red, green, and blue cones •  Different cones have different frequency

responses •  Tri-receptor theory of color vision

[Young1802] –  Rods: night vision, perceive brightness

only •  Color sensation is characterized by

–  Luminance (brightness) –  Chrominance

•  Hue (color tone) •  Saturation (color purity)

From http://www.macula.org/anatomy/retinaframe.html

10

© Yao Wang, 2016 EL-GY 6123

Frequency Responses of Cones and the Luminous Efficiency Function

ybgridaCC ii ,,,,)()( == ∫ λλλ

11

© Yao Wang, 2016 EL-GY 6123

Three Attributes of Color

•  Luminance (brightness) •  Chrominance

–  Hue (color tone) and Saturation (color purity) •  Represented by a “color cone”

θ

12

© Yao Wang, 2016 EL-GY 6123

Trichromatic Color Mixing

•  Trichromatic color mixing theory –  Any (actually most) color can be obtained by mixing three primary

colors with a right proportion

•  Primary colors for illuminating sources: –  Red, Green, Blue (RGB) –  Color monitor works by exciting red, green, blue phosphors using

separate electronic guns •  Primary colors for reflecting sources (also known as secondary

colors): –  Cyan, Magenta, Yellow (CMY) –  Color printer works by using cyan, magenta, yellow and black (CMYK)

dyes

valuessTristimulu :,3,2,1

kk

kk TCTC ∑=

=

13

© Yao Wang, 2016 EL-GY 6123

RGB vs CMY

Magenta = Red + Blue Cyan = Blue + Green Yellow = Green + Red

Magenta = White - Green Cyan = White - Red Yellow = White - Blue

14

© Yao Wang, 2016 EL-GY 6123

red

Green Blue

15

© Yao Wang, 2016 EL-GY 6123

Tristimulus Values and Chromaticity

•  Tristimulus values –  The amounts of three primary colors needed to form any

particular color are called the tristimulus values, denoted by X, Y, and Z (or any other three symbols).

•  Trichromatic (or chromaticity) coefficients

–  Only two chromaticity coefficients are necessary to specify the chrominance of a light.

.,,ZYX

ZzZYX

YyZYX

Xx++

=++

=++

=

1=++ zyx

16

Problems with Using Primary Colors for Color Representation

•  Tristimulus values depend on the primary colors used by the camera/display

•  Tristimulus values may be negative for some visible colors •  How to compare colors obtained using different devices •  Need for standardization •  Standard using physical primary colors (CIE RGB) •  Standard using imaginary primary colors (CIE XYZ): device

invariant •  Colors captured by cameras using different physical RGB

primaries can be converted to the standard XYZ representation •  CIE: International commission on illumination

© Yao Wang, 2016 EL-GY 6123 17

CIE XYZ Color Space

•  Use imaginary color primaries X, Y, Z so that the tristimulus values are all non-negative

•  Encompasses all visible colors by an average person •  Device invariant color representation •  Color primaries used by each device can be specified

using XYZ values •  Colors captured by cameras using different physical

RGB primaries can be converted to the standard XYZ representation

© Yao Wang, 2016 EL-GY 6123 18

CIE XYZ Color Matching Functions

•  Y approximates the brightness ~= green cone response of an average person (standard observer)

•  Z approximates the blue cone response of an average person •  X is NOT red, rather derived so that XY,Z values for all visible colors

are all >=0.

© Yao Wang, 2016 EL-GY 6123 19

https://en.wikipedia.org/wiki/CIE_1931_color_space

© Yao Wang, 2016 EL-GY 6123

CIE Chromaticity Diagram

•  Colors on the boundary: spectrum colors, highest saturation

•  Shows all visible colors by humans

•  Mixing any three colors in the visible range can generate colors in the triangle formed by these three points.

20 from [Gonzalez02]

Color Gamut of Monitors and Printers

•  Not all visible colors can be reproduced by RGB primaries used for display, or CMY primaries used for printing.

•  The triangle: gamut of color monitors using the Red, Green, and Blue colors at the corner as primary colors

•  The irregular region inside: gamut of most color printers

•  A printer cannot reproduce all colors that you see on the screen!

© Yao Wang, 2016 EL-GY 6123 21

CIE RGB Color Space

•  Using single spedtral color for primary colors: –  700 nm (R), 546.1

nm (G), 435.8nm (B)

•  Color matching functions are the amounts of primaries needed to match each single spectral color

© Yao Wang, 2016 EL-GY 6123 22

https://en.wikipedia.org/wiki/CIE_1931_color_space

Color Primary Defined by ITU Rec. 709 for HDTV

© Yao Wang, 2016 EL-GY 6123 23

RGB

!

"

###

$

%

&&&=

3.240479 −1.537150 −0.498535−0.969256 1.875992 0.0415560.055648 −0.204043 1.057311

!

"

###

$

%

&&&

XYZ

!

"

###

$

%

&&&

XYZ

!

"

###

$

%

&&&=

0.412453 0.357580 0.1804230.212671 0.715160 0.0721690.019334 0.119193 0.950277

!

"

###

$

%

&&&

RGB

!

"

###

$

%

&&&

The three columns in the RGB-> XYZ conversion are the tristimulus values of the R,G,B primaries defined in terms of CIE XYZ primaries. From these, you could derive the chromaticity coefficients of RGB primaries and correspondingly locate them in the CIE diagram (HW!)

Comparison of Colors Defined Using Different Primiaries

•  To compare colors defined using different primaries, we should convert them all to CIE XYZ primaries and compare their XYZ values

© Yao Wang, 2016 EL-GY 6123 24

© Yao Wang, 2016 EL-GY 6123

Color Representation Models

•  Specify the tristimulus values associated with the three primary colors –  RGB, CMY, XYZ

•  Specify the luminance and chrominance –  HSI (Hue, saturation, intensity) –  YIQ (used in NTSC color TV) –  YCbCr (used in digital color TV)

•  Amplitude specification: –  8 bits for each color component, or 24 bits total for each pixel –  Total of 16 million colors –  A true RGB color display of size 1Kx1K requires a display buffer

memory size of 3 MB •  High dynamic range (HDR) Image: up to 16 bits/component

25

© Yao Wang, 2016 EL-GY 6123

HSI Color Model

•  Hue represents dominant color as perceived by an observer. It is an attribute associated with the dominant wavelength.

•  Saturation refers to the relative purity or the amount of white light mixed with a hue. The pure spectrum colors are fully saturated. Pink and lavender are less saturated.

•  Intensity reflects the brightness.

26

© Yao Wang, 2016 EL-GY 6123

Conversion Between RGB and HSI

•  Converting from RGB to HSI

•  Converting from HSI to RGB

[ ]

[ ]

][31

)],,[min()(

31

))(()(

)()(21

cos360

21

2

1

BGRI

BGRBGR

S

BGBRGR

BRGRwith

GBifGBif

H

++=

++−=

⎪⎪⎭

⎪⎪⎬

⎪⎪⎩

⎪⎪⎨

−−+−

−+−=

⎩⎨⎧

>−≤

= −θθ

θ

)(1)60cos(

cos1

)1(

BRGHHSIR

SIB

+−=

⎥⎦

⎤⎢⎣

⎡−

+=

−=

RG sector (0≤H<120)

)(1))120(60cos()120cos(1

)1(

GRBHHSIG

SIR

+−=

⎥⎦

⎤⎢⎣

⎡−−

−+=

−=GB sector (120≤H<240)

)(1))240(60cos()240cos(1

)1(

BGRHHSIB

SIG

+−=

⎥⎦

⎤⎢⎣

⎡−−

−+=

−=

BR sector (240≤H<360)

27 from [Gonzalez02]

© Yao Wang, 2016 EL-GY 6123

YIQ Color Coordinate System

•  YIQ is defined by the National Television System Committee (NTSC) for US analog color TV system –  Y describes the luminance, I and Q describes the chrominance. –  A more compact representation of the color. –  YUV plays similar role in PAL and SECAM.

•  Conversion between RGB and YIQ

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

−−−=

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

−−−=

⎥⎥⎥

⎢⎢⎢

QIY

BGR

BGR

QIY

703.1106.10.1649.0272.00.1621.0956.00.1

,311.0523.0211.0322.0274.0596.0114.0587.0299.0

28

© Yao Wang, 2016 EL-GY 6123

YUV/YCbCr Coordinate

•  YUV is the color coordinate used in color TV in PAL system, somewhat different from YIQ.

•  YCbCr is the digital equivalent of YUV, used for digital TV, with 8 bit for each component, in range of 0-255

29

© Yao Wang, 2016 EL-GY 6123

Comparison of Different Color Spaces

30

from [Gonzalez02]

© Yao Wang, 2016 EL-GY 6123

Color Coordinate Conversion

•  Conversion between different primary sets are linear (3x3 matrix)

•  Conversion between primary and XYZ/YIQ/YUV are also linear

•  Conversion to LSI/Lab are nonlinear –  LSI and Lab coordinates

•  coordinate Euclidean distance proportional to actual color difference

•  Conversion formulae between many color coordinates can be found in [Gonzalez92]

•  Color space of HDTV by Recommendation 709 can be found in [Woods2012]

31

© Yao Wang, 2016 EL-GY 6123

Grayscale Image Specification

•  Each pixel value represents the brightness of the pixel. With 8-bit image, the pixel value of each pixel is 0 ~ 255

•  Matrix representation: An image of MxN pixels is represented by an MxN array, each element being an unsigned integer of 8 bits

⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢

=

10999555894795560

69122158162154166162160

M

32

© Yao Wang, 2016 EL-GY 6123

Color Image Specification

•  Three components –  M = {R, G, B}

R G B

⎥⎥⎥

⎢⎢⎢

⎡=

⎥⎥⎥

⎢⎢⎢

⎡=

⎥⎥⎥

⎢⎢⎢

⎡=

1436

6131,

1336

9866,

1727

8773

BGR

Red nose is brightest! Blue Cheek is brightest 33

© Yao Wang, 2016 EL-GY 6123

Chrominance Subsampling Formats

4:2:0For every 2x2 Y Pixels

1 Cb & 1 Cr Pixel(Subsampling by 2:1 bothhorizontally and vertically)

4:2:2 For every 2x2 Y Pixels

2 Cb & 2 Cr Pixel(Subsampling by 2:1

horizontally only)

4:4:4 For every 2x2 Y Pixels

4 Cb & 4 Cr Pixel(No subsampling)

Y Pixel Cb and Cr Pixel

4:1:1For every 4x1 Y Pixels

1 Cb & 1 Cr Pixel(Subsampling by 4:1

horizontally only)

34

© Yao Wang, 2016 EL-GY 6123

Image Capture and Display

•  Light reflection physics •  Imaging operator •  Color capture •  Color display

35

© Yao Wang, 2016 EL-GY 6123 36

Image Capture

•  Light source (λ: wavelength of the source) –  E(x, y, z, λ): incident light on a point (x, y, z world coordinates

of the point) •  Each point of the scene has a reflectivity function.

–  r(x, y, z, λ): reflectivity function •  Light reflects from a point and the reflected light is

captured by an imaging device. –  c(x, y, z, λ) = E(x, y, z, λ) * r(x, y, z, λ): reflected light.

E(x, y, z, λ) c(x, y, z, λ) = E(x, y, z, λ)*r(x, y, z, λ)

Camera( c(x, y, z, λ) )

Courtesy of Onur Guleryuz

© Yao Wang, 2016 EL-GY 6123

More on Image/Video Capture

•  Reflected light to camera –  Camera absorption function (3 such functions to capture color

images)

–  Projection from 3-D to 2-D

•  The projection operator is non-linear –  Perspective projection –  Othorgraphic projection

λλλψ datCt c )(),,(),( ∫= XX

)),((),(or ),()),(( 1 tPtttPP

xxXX

xX−==

ψψψψ

37

© Yao Wang, 2016 EL-GY 6123

Gray and Color Image Capture

•  Gray images are captured by a single sensor, sensitive to the entire visible spectrum, similar to the rods

•  Color images are captured by having three sensors, each sensitive to a different primary color, similar to the cones –  Alternatively using a single sensor proceeded by different color

filters

•  Each sensor or filter has its own frequency response, which may differ from the cones responses in the human retina.

38

© Yao Wang, 2016 EL-GY 6123

Color Imaging Using Color Filters

•  Single sensor array, with different color filters to separate the RGB primary components

•  Sensors: CCD, CMOS

From http://en.wikipedia.org/wiki/Bayer_filter

Bayer Color Filter Array

39

© Yao Wang, 2016 EL-GY 6123 40

1: Original scene; 2: Output of a 120-pixel × 80-pixel sensor with a Bayer filter; 3: Output color-coded; 4: Reconstructed image after interpolating missing color information From: https://en.wikipedia.org/wiki/Bayer_filter

© Yao Wang, 2016 EL-GY 6123

Gray and Color Image Display/Printing

•  Gray images are displayed by a single light sensitive diode, with intensity proportional to gray level.

•  LCD monitors display color images by having three phosphors at slightly shifted positions near each pixel, each generating a different primary color (red, green, blue)

•  Color images are printed by having three color inks (cyan, magenta, yellow). High end printers use more inks to produce a larger color gamut and more vivid colors.

41

Gamma Correction

•  Displayed light intensity may be nonlinearly related to the actual intensity following the Gamma rule

•  Gamma correction precompensate this nonlinearity

•  Gamma value depends on the display device

© Yao Wang, 2016 EL-GY 6123 42

g = af r

h = f 1/r

g = ahr = af

Captured and Displayed Color

•  Original lighted scene has a spectrum distribution (in terms of wavelength) at a particular space and time (x,y,z,t). Call it c(l)

•  If the human observes it directly, the cones’ responses (each with frequency responses ai(l)) will be

•  If the scene is captured by a camera, with three sensors, each with frequency response bi(l), the captured RGB values will be

•  The display will use three phosphors, each with different spectrum di(l) and the displayed spectrum is the weighted sum of the three spectrums

•  The displayed spectrum again is viewed by the human, generating three other responses

•  Bi should be close to Ai for the perceived scene to be similar to the original scene

© Yao Wang, 2016 EL-GY 6123 43

Ai = C(λ)ai∫ (λ)dλ, i = r,g,b, y

Ci = C(λ)ci∫ (λ)dλ, i = r,g,b

Bi = D(λ)ai∫ (λ)dλ, i = r,g,b, y

! ! = !!!! !!

!

© Yao Wang, 2016 EL-GY 6123

Summary (Color Image Perception, Production, Representation, Capture and Display)

•  What is light? Difference between illuminating and reflecting light. Which light attribute determines the color?

•  How human perceive color? Functions of cones and rods in the retina.

•  Three attributes of color: luminance, hue, saturation •  How to produce different colors ? •  How to represent a color? Different color models. •  Color image representation using a matrix •  Color image capture using color filters •  Color image display

44

MATLAB For Image Processing

•  Toolbox: images •  Useful functions: •  Imread( ), Imwrite( ), Imshow( ), Imresize( ) •  Plot, subplot •  Color conversion: rgb2ycbcr( ), rgb2hsv( ), etc. •  Use “help images” to see all functions •  Use “help imshow” to see all options of imshow

© Yao Wang, 2016 EL-GY 6123 45

Some Caveats to Remember!

•  Images are generally stored in array of unit8 (8 bit unsigned integer)

•  Convert to integer or double before any processing! –  Img=imread(‘lena.jpg’); –  Img=int8(img) or img=double(img)

•  Array index starts at 1, not 0! Top-left pixel is img(1,1); •  Convert back to uint8 when saving an image

–  First scale the processed image so that min=0, max=255, change the type to uint8

–  Then use imwrite( ) •  Image display: Imshow(), imagesc()

–  Be very careful when using imshow( ) on images of single/double. Default range may not show all possible values

–  Imshow(img, [low, high]): low: black, high: white –  Imshow(img,[ ]): low=min, high=max

© Yao Wang, 2016 EL-GY 6123 46

© Yao Wang, 2016 EL-GY 6123

Image Contrast Enhancement

•  Introduction •  Linear stretching •  Nonlinear stretching •  Histogram equalization •  Histogram specification •  Adaptive histogram modification

47

© Yao Wang, 2016 EL-GY 6123

What is Contrast Enhancement

Original image with low contrast Enhanced image

48

© Yao Wang, 2016 EL-GY 6123

How to enhance the contrast?

•  Low contrast à image values concentrated near a narrow range (mostly dark, or mostly bright, or mostly medium values)

•  Contrast enhancement à change the image value distribution to cover a wide range

•  Contrast of an image can be revealed by its histogram

49

© Yao Wang, 2016 EL-GY 6123

Histogram

•  Histogram of a monochrome image with L possible gray levels, l = 0, 1, …, L-1. –  P(l) = nl / n,

•  nl is the number of pixels with gray level l. •  n is the total number of pixels in the image.

50

Histogram vs. Image Contrast

Images with figure captions in this and other slides are from [Gonzalez08]

© Yao Wang, 2016 EL-GY 6123 51

© Yao Wang, 2016 EL-GY 6123

Histogram Calculation

Photoshop has extensive histogram display tools Matlab: imhist( ): can compute and display histograms

function h = histogram(imgname) img = imread(imgname); figure; imshow(img); % method 1 h = zeros(256,1); for l = 0:255 for i = 1:N, for j = 1:M, if img(i, j) == l, h(l + 1) = h(l + 1) + 1; end end end end figure; bar(h);

% method 2 img = int8(img); h = zeros(256,1); for i=1:M, for j=1:N, f = img(i,j); h(f+1) = h(f+1) + 1; end end % method 3 h = zeros(256,1); for l = 0 : 255, h(l + 1)=sum(sum(img == l)); end

52

© Yao Wang, 2016 EL-GY 6123

Very Different Images May Have Same Histogram!

Histogram reflects the pixel intensity distribution, not the spatial distribution!

53

© Yao Wang, 2016 EL-GY 6123

Previous Example

Original image with low contrast Enhanced image

54

© Yao Wang, 2016 EL-GY 6123

Histograms of Example Images

0 50 100 150 200 250

0

200

400

600

800

1000

1200

1400

1600

0 50 100 150 200 250

0

200

400

600

800

1000

1200

1400

1600

Original girl image with low contrast

Enhancement image with histogram equalization

55

© Yao Wang, 2016 EL-GY 6123

How to change the histogram? Using Point-Wise Transformation

•  Use a “function” g(f) to generate a new image B from a given image A via:

•  The function g(f) operates on each image pixel independently. All pixels with original gray level f are changed to have gray level g(f)

•  Properties that g(f) should satisfy –  Monotonically non-decreasing, so that relative brightness of pixels do

not change. –  G(f) in the same range as original f, i.e. with same min (e.g. 0) and

max values (e.g. 255), and be integers for digital images. •  Rounding/truncation may be needed

1,...,0,1,...,0)),,((),( −=−== MjNijiAgjiB

56

How to Determine the Transformation Function?

•  How to design the transformation function g(f)? –  depends on the histogram of the original image hA(f) and the

desired histogram of the transformed image hB(f). –  To enhance contrast, we like hB(f) to be as flat as possible.

•  Different approaches –  Using fixed functional forms: linear, non-linear –  Using adaptive transform, that is determined from hA(f) and

hB(f): •  Histogram equalitzation (hB(f) is uniform): Fully automatic! •  Histogram specification or matching

© Yao Wang, 2016 EL-GY 6123 57

© Yao Wang, 2016 EL-GY 6123

Illustration of Linear Stretching

f

p(f)

100 150 f

g(f)

100 150 g

p(g)

255

255

Linear stretching

58

© Yao Wang, 2016 EL-GY 6123

Piece-Wise Linear for Middle Range Image

59

© Yao Wang, 2016 EL-GY 6123

Nonlinear Stretching

•  Nonlinear functions with a fixed form •  Fewer parameters to adjust •  Satisfying •  Examples

–  Logarithmic transformation •  Stretch dark region, suppress bright region

–  Exponential transformation •  Expand bright region

–  Power Law •  K = 2: square law, similar to exponential •  K = 1/3: cubic root, similar to logarithmic

10 maxmin −=≤≤= Lfgf

)1log( += afbg

)1( −= afebgkafg =

60

© Yao Wang, 2016 EL-GY 6123

Example of Log Transformation

Eq. (3.2-2) “log” function: g=c log (1+f)

61

© Yao Wang, 2016 EL-GY 6123

Power Law (Gamma) Transformations

62

© Yao Wang, 2016 EL-GY 6123

Histogram Equalization

•  Transforms an image with an arbitrary histogram to one with a flat histogram –  Suppose f has PDF pF(f), 0 ≤ f ≤ 1

–  Transform function (continuous version)

–  g is uniformly distributed in (0, 1) ∫=f

F dttpfg0

)()(

Histogram Equalization

63

© Yao Wang, 2016 EL-GY 6123

Proof

)1,0(,1)(

)(

)1,0(,)()(

,)()(min

∈=

=

∈=

= ∫

ggp

fpdfdg

g

dfdgfpgp

dttpfg

G

F

FG

f

fF

64

© Yao Wang, 2016 EL-GY 6123

Example

)1,0(,1)(

)1,2/1()1(21)1(44

)2/1,0(24)(

)1,2/1()1(4)2/1,0(4

)(

2/1

0 2/1

20

2

∈=

⎪⎩

⎪⎨⎧

∈−−=−+

∈==

⎩⎨⎧

∈−∈

=

∫ ∫∫

ggp

ffdfffdf

fffdffg

ffff

fp

G

f

f

F

f

pF(f)

0 1/2 1

2

g

pG(g)

0 1

1

f

g(f)

0 1/2 1

1/2

1

65

© Yao Wang, 2016 EL-GY 6123

Discrete Implementation

•  For a discrete image f which takes values k=0,…,K-1, use

•  To convert the transformed values to the range of (0, L-1):

–  Note: {x} is the rounding of x

.1,...,1,0,)()(~0

−==∑=

Klkplgl

kF

⎪⎭

⎪⎬⎫

⎪⎩

⎪⎨⎧

−⎟⎟⎠

⎞⎜⎜⎝

⎛= ∑

=

)1(*)()(0

Lkproundlgl

kF

66

© Yao Wang, 2016 EL-GY 6123

Example fk pF(l) pG(l) gk

0 0.19 0.19 [1.33]=1 0 0 1 0.25 0.44 [3.08]=3 0.19 1 2 0.21 0.65 [4.55]=5 0 2 3 0.16 0.81 [5.67]=6 0.25 3 4 0.08 0.89 [6.03]=6 0 4 5 0.06 0.95 [6.65]=7 0.21 5 6 0.03 0.98 [6.86]=7 0.16+0.08=0.24 6 7 0.02 1.00 [7]=7 0.06+0.03+0.02=0.11 7

∑ == l

k Fl kpg0

)(~ ]7*~[ ll gg =

l

pF(l)

0 1 2 3 4 5 6 7 l

pG(l)

0 1 2 3 4 5 6 7 We don’t get perfectly flat histogram with discrete implementation! 67

© Yao Wang, 2016 EL-GY 6123

Sample Matlab Code

function histogram_eq(inimgname) img=imread(imgname); figure; imshow(img); [M,N]=size(img); H=imhist(img); H=H/(M*N); figure; bar(H); %Computing the mapping function for (k=1:256) C(k)=uint8(sum(H(1:k))*255); end; % C = uint8(cumsum(H)*255); figure;plot(C);

%perform mapping for (i=1:M) for (j=1:N) f=int8(img(i,j))+1; histeqimg(i,j)=C(f); end; end; %note the above loop can be replaced by: %histeqimg=C(int8(img)+1); %this will be much faster! figure; imshow(histeqimg);

68

© Yao Wang, 2016 EL-GY 6123

Adaptive Histogram Modification

•  Local histogram equalization

69

© Yao Wang, 2016 EL-GY 6123

Contrast Enhancement for Color Images

•  How should we apply the previous techniques to color images –  To all three color components (RGB or CMY) separately –  To the Intensity component only while keeping the Hue and

Saturation in the HSI coordinate –  Can also enhance saturation for more vivid colors –  Can change individual color components to add certain tone to

an image

70

© Yao Wang, 2016 EL-GY 6123

Examples for Color Image (1)

71

From [Gaonzalez08]

© Yao Wang, 2016 EL-GY 6123

Examples for Color Image (2)

72

From [Gaonzalez08]

MATLAB functions

•  Imhist( ) •  Histeq() •  Adapthisteq( ) •  Imadjust( ) •  Imcontrast() •  Brighten() •  stretchlim()

•  Use help to learn what does each do, and play around with these functions to see the effect

© Yao Wang, 2016 EL-GY 6123 73

Summary (Contrast Enhancement)

•  What is image histogram? •  How to tell whether an an image have a good contrast

from its histogram? •  Given the histogram of an image, can you sketch a

transformation that will likely improve the image contrast.

•  The principle of histogram equalization •  The principle of histogram specification •  Color image enhancement

© Yao Wang, 2016 EL-GY 6123 74

© Yao Wang, 2016 EL-GY 6123

Reading Assignments

•  Image formation, perception and representation: –  Wang et al, Digital video processing and communications.

Chap 1 (Sec. 1.3-1.4 optional) –  Or –  Woods, Multidimensional signal, image, and video processing

and coding, Chap 6. (Sec. 6.2-6.3 optional)

•  Contrast enhancement –  Gonzalez & Woods, “Digital Image Processing”, Prentice Hall,

2008, Chapter 3 (Section 3.1 – 3.3)

75

© Yao Wang, 2016 EL-GY 6123

Written Homework

1.  Consider the Rec. 709 color primaries. Determine the chromaticity coefficients (x,y) associated with these primary colors and roughly indicate their location in the CIE chromaticity diagram.

2.  Following figure shows the histogram of an image and two transformation functions. Sketch the histograms of the images obtained by applying the two functions to the original image.

3.  Following figure (next slide) shows the histograms of three different images, and three possible point functions. For each original image, which point operation can best equalize its histogram? Briefly explain your reasoning.

h(f)

f

a

0 128 255

g1(f)

f

128

0 128 255

g2(f)

f

128

0 128 255

255

76

© Yao Wang, 2016 EL-GY 6123

Written Homework (cnt’d)

3.  For the histogram h3(f) given in the following figure, determine analytically the histogram equalizing function (assuming the original image value can vary continuously in the range of 0-255).

4.  An 8-level images have histograms h=[.1, .2, .3, 0, .1, 0, 0, .3]. Find a point function g(f) that will equalize this histogram. Show the resulting mapping function and the histogram. Is the resulting histogram flat? If not, what possibe mechanism you can use to make it better?

h1(f) a

0 128 255 f

h2(f) a

0 64 192 f

h3(f) a

0 128 255 f g1(f)

128

0 64 255 f

255 g2(f)

128

0 128 255 f

255 g3(f)

128

0 128 255 f

255

192

77

MATLAB Assignment

1.  If you do not have a copy of MATLAB, get one installed by IS helpdesk or use the PC Lab at Poly. If you have not used Matlab before, go through Matlab getting started guide at http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf. Go through all exercises step by step. Make sure that you know basic operations and how to write m-files (scripts or functions).

2.  If you have not used MATLAB for image processing, go through the supplementary document on course website (MATLAB for image manipulation)

3.  Take a picture of yourself or anything interesting and upload to your computer. 4.  Write a matlab program that can i) read your color image into a matrix (you can use

imread() function in MATLAB); ii) covert it to a 8-bit grayscale image using the RGB to Y conversion formula in the RGB to YCbCr formula provided (You should not use the MATLAB built-in function rgb2ycbcr), iii) generate a digital negative version of your grayscale image, and iv) display both the color image, the original gray scale and the negative image. REMEMBER THE CAVEATS!

5.  Write a Matlab function that can compute the histogram of a grayscale image (assuming 256 levels of gray). Try the three possible ways described in slide 7, and see which one is faster. Finalize your program with the fastest method. In a separate main program, apply the program to a test image, and display the histogram as a stem plot besides the image (using “subplot” function). You are not allowed to simply use the “imhist” or “hist” function in Matlab, although you are encouraged to compare your results with those obtained using these functions.

© Yao Wang, 2016 EL-GY 6123 78

MATLAB Assignment (cnt’d)

5. Write a Matlab program that performs histogram equalization on a grayscale image. Your program should: i) compute the histogram of the input image by calling your own histogram function from previous problem; ii) compute the histogram equalizing transformation function; iii) apply the function to the input image; iv) compute the histogram of the equalized image; v) display (and print) the original and equalized images, as well as their corresponding histograms, all in one figure. You are not allowed to simply use the “histeq” function in Matlab, although you are encouraged to compare your results with those obtained using these functions. For Prob. 4 and 5: try to capture an image with low contrast so that you could see the effect of contrast enhancement. Alternatively you could use the test images provided in the class homepage. Note: For this (and future) computer assignment, you should submit a lab report that includes a copy of your matlab programs, the pictures you created, as well as a description of your findings if applicable. All printed figures should have a caption that explains what they are. You should submit a printed copy of your report together with your other written homework.

© Yao Wang, 2016 EL-GY 6123 79