Module 1JH

download Module 1JH

of 61

Transcript of Module 1JH

  • 8/18/2019 Module 1JH

    1/61

    Introduction

    · Quickstart: Read & Display Images·

    · Types of Images· mag ng eometry· Image Representation

    1

  • 8/18/2019 Module 1JH

    2/61

    “ ” .

    ● Many of our images will have file type “.bin”● This is not a “standard” file type!

    ► These files contains raw image data without any

    header information.● rograms e croso t ce cture

    Manager can’t display “.bin” files directly.

    because the format is simple and there’s nocom licated header to arse.

    2

  • 8/18/2019 Module 1JH

    3/61

    ’ “ ” .

    ● It is usually a grayscaleimage.

    is one byte.► The data type is uchar or.► The min value is 00000000b

    = 00h = 0d, which displays as

    .► The max value is 11111111b= FFh = 255d, which displays

    .

    3

  • 8/18/2019 Module 1JH

    4/61

    “ ” .

    ●The file contains a linear arrayof bytes: one for each pixel.

    ● The ixels are stored in “raster scan” order: left to right, top tobottom.

    ● × 256 bytes are the pixels of thetop row from left to right.

    ● pixels of the second row, fromleft to right, and so on.

    4

  • 8/18/2019 Module 1JH

    5/61

    “ ” .

    fidLena = fopen( 'lena.bin' , 'r' );[Lena,junk] = fread(fidLena,[256,256], 'uchar' );junk % echo the number of bytes that were read

    = '

    figure(1);colormap(gray(256));image(Lena);ax s mage ;title( 'Original Lena Image' );print -dtiff M_Lena.tif ; % write figure as tif

    fidOut = fopen( 'Outfile.bin' , 'w+' );LenaOut = Lena';fwrite(fidOut,LenaOut, 'uchar' ); % write raw image data

    5

  • 8/18/2019 Module 1JH

    6/61

    ● When you use fread to read a “.bin” image, you must transpose

    e array a er rea ng.► This is a legacy issue related to the fact that Matlab was originallywritten in FORTRAN, but was later re-implemented in C.

    nd ,array the 1st index is the column.

    ● If you read the image into a 2D Matlab array X, then X(m,n) isthe pixel at row=m and col=n. X(1,1) is the upper left pixel oft e mage. , s t e p xe on row an co umn .

    ● Reasons TO use Matlab: it’s easy and has lots of built infunction support (to display images, for example).

    ● Use matrix operations and avoid loops wherever possible!► Ex: instead of a loop, use K(1:256,1:128) = J(1:256,129:256) to

    .

    6

  • 8/18/2019 Module 1JH

    7/61

    “ ” .#include #include # nc u e #include

    #include

    #define BYTE unsigned char

    /** Function Prototypes (forward declarations)*/

    void disk2byte();void byte2disk();

    *----------------------------------------------------------------------*/* MAIN *//*----------------------------------------------------------------------*/

    main(argc,argv)

    int argc;c ar argv ;

    {

    int size; /* num rows/cols in images */int so2; /* size / 2 */int i; /* counter */int row; /* image row counter */

    7

    int col; /* image col counter */BYTE *I1; /* input image I1 */

  • 8/18/2019 Module 1JH

    8/61

    BYTE *I2; /* input image I2 */BYTE *J; /* output image J */BYTE *K; /* output image K */

    * * * char *InFn2; /* input filename for image I2 */char *OutFnJ; /* output filename for image J */char *OutFnK; /* output filename for image K */

    /** Check for proper invocation, parse args*/if (argc != 6) {

    printf("\n%s: Swap image halves for hw01.",argv[0]);printf("\nUsage: %s size InFn1 InFn2 OutFnJ OutFnK\n",

    argv[0]);exit(0);

    }size = atoi(argv[1]);if (size % 2) {

    printf("\n%s: size must be divisible by 2.\n",argv[0]);exit(0);

    }InFn1 = argv[2];=

    OutFnJ = argv[4];OutFnK = argv[5];

    so2 = size >> 1;

    /** A ocate mage arrays*/if ((I1 = (BYTE *)malloc(size*size*sizeof(BYTE))) == NULL) {

    printf("\n%s: free store exhausted.\n",argv[0]);exit(-1);

    }

    8

  • 8/18/2019 Module 1JH

    9/61

    if ((I2 = (BYTE *)malloc(size*size*sizeof(BYTE))) == NULL) {printf("\n%s: free store exhausted.\n",argv[0]);exit(-1);

    if ((J = (BYTE *)malloc(size*size*sizeof(BYTE))) == NULL) {printf("\n%s: free store exhausted.\n",argv[0]);exit(-1);

    }if ((K = (BYTE *)malloc(size*size*sizeof(BYTE))) == NULL) {

    printf("\n%s: free store exhausted.\n",argv[0]);exit(-1);

    }

    /** Read input images*/disk2byte(I1,size,size,InFn1);disk2byte(I2,size,size,InFn2);

    /** Make output image J: left half is I1, right half is I2

    */for (i=row=0; row < size; row++) {= ,

    J[i] = I1[i];}for ( ; col < size; col++,i++) {

    J[i] = I2[i];}

    }

    9

  • 8/18/2019 Module 1JH

    10/61

    /** Make output image K: swap left and right halves of J*/

    for (col=0; col < so2; col++) {

    K[row*size + col] = J[row*size + col+so2];}for ( ; col < size; col++) {

    K[row*size + col] = J[row*size + col-so2];}

    }

    /** Write the output images*/byte2disk(J,size,size,OutFnJ);byte2disk(K,size,size,OutFnK);

    return;} /*---------------- Main ----------------------------------------------*/

    /*----------------------------------------------------------------------* disk2byte.c** function reads an unsigned char (byte) image from disk*** jph 15 June 1992*------------------------------------------------------------------------*/

    void disk2byte(x,row_dim,col_dim,fn)

    BYTE *x; /* image to be read */int row_dim; /* row dimension of x */int col_dim; /* col dimension of x */char *fn; /* filename */

    10

    {

  • 8/18/2019 Module 1JH

    11/61

    int fd; /* file descriptor */int n_bytes; /* number of bytes to read */

    ** detect zero dimension input*/if ((row_dim==0) || (col_dim==0)) return;

    /** create and open the file*/if ((fd = open(fn, O_RDONLY))==-1) {

    printf("\ndisk2byte.c : could not open %s !",fn);return;

    }

    /** read image data from the file*/n_bytes = row_dim * col_dim * sizeof(unsigned char);if (read(fd,x,n_bytes) != n_bytes) {

    printf("\ndisk2byte.c : complete read of %s did not succeed.",fn);}

    /** close file and return*/if (close(fd) == -1) printf("\ndisk2byte.c : error closing %s.",fn);return;

    }

    11

  • 8/18/2019 Module 1JH

    12/61

    /*----------------------------------------------------------------------* byte2disk.c***** jph 15 June 1992*------------------------------------------------------------------------*/

    void byte2disk(x,row_dim,col_dim,fn)

    BYTE *x; /* image to be written */int row_dim; /* row dimension of x */int col_dim; /* col dimension of x */char *fn; /* filename */

    {

    int fd; /* file descriptor */int n_bytes; /* number of bytes to read */

    /** detect zero dimension input*if ((row_dim==0) || (col_dim==0)) return;

    /** create and open the file*/if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644))==-1) {

    pr nt n yte2 s .c : cou not open %s ! , n ;return;

    }

    12

  • 8/18/2019 Module 1JH

    13/61

    /** write image data to the file*/

    * *_ _ _if (write(fd,x,n_bytes) != n_bytes) {

    printf("\nbyte2disk.c : complete write of %s did not succeed.",fn);}

    /** close file and return*/if (close(fd) == -1) printf("\nbyte2disk.c : error closing %s.",fn);

    return;}

    13

  • 8/18/2019 Module 1JH

    14/61

    ● C is good because it is FAST and has powerful syntax for performing low level

    .●

    You need a good compiler. See “links” on the course web site to obtain theexcellent GNU gcc compiler. It will compile the code in the previous example.● You need a good debugger. See “links” on the course web site to get DDD.● ou nee goo rary rout nes to sp ay mages an to rea mages w t

    headers. See “links” on the course web site to get ImageMagick.► Also see the “Image Magick HOWTO” under “Handouts” on the course web site.

    ● Array indexing in C starts at ZERO. This is an important difference from.► The top row of the image is row 0. The first column is col 0.► For a 256× 256 image, X[m*256 + n] is the pixel at row=m and col=n.► For a 2D C array, X[n][m] is the pixel at col=n and row=m.

    ’ ,image SIZE is unknown at compile time.

    ● The main reasons to use C are that it is FAST and PORTABLE… fasterdebugging and doesn’t depend on having a Matlab installation.

    14

  • 8/18/2019 Module 1JH

    15/61

    “ ” “ ”. .● If you want to directly display a “.bin” image using standard programs

    , .● The easiest way is to make a “.pgm” file.● For a 256× 256 “.bin” image, add the following 15-byte header:

    HEX 50 35 0A 32 35 36 20 32ASCII P 5 . 2 5 6 _ 2

    HEX 35 36 0A 32 35 35 0AASCII 5 6 . 2 5 5 .

    ● ou can o t s manua y w t a ex-capa e e tor e v un xor Vim (windows or unix).● Or you can use a program like the function “pgm_convert.m”

    “ ”

    15

    .

  • 8/18/2019 Module 1JH

    16/61

    ● LearnDigital Image & Video Processing

    ► Algorithms and Programming► pp cat ons an ro ects

    ● Have fun doing it

    16

  • 8/18/2019 Module 1JH

    17/61

    • , . . . , , .

    User-friendly textbook, nicely illustrated. Used previously in this course• Digital Image Processing , W.K. Pratt, Wiley, 4th Edition, 2007.Enc clo edic, rather dated.

    • Digital Picture Processing , Rosenfeld & Kak, 2nd Edition, 1982.Encyclopedic but readable.

    • Fundamentals of Digital Image Processing , Jain, 1988.Handbook-style, terse. Meant for advanced level.

    • Digital Video Processing , M. Tekalp, Prentice-Hall, 1995 .

    Only book devoted to digital video; high-level; excellent

    • ac ne s on , a n, astur , an c un , c raw- ,Beginner’s book on computer vision.• Robot Vision , B.K.P. Horn, MIT Press, 1986

    17

    - .

  • 8/18/2019 Module 1JH

    18/61

    • IEEE Transactions on:- Image Processing - Pattern nal sis & achine Intelli ence- Multimedia- Geoscience and Remote Sensing - edical ma in

    • Computer Vision, Graphics, and Image Processing - Image Understanding -

    • Pattern Recognition• Image and Vision Computing

    18

  • 8/18/2019 Module 1JH

    19/61

    astronomy seismology radiology

    meteorology

    "imaging" inspection

    navigation

    microscopy

    u rason cimaging

    reconnassaincesurveillance robotic assembly digital library

    mapp ngremotesensing radar,SARinternet

    19

  • 8/18/2019 Module 1JH

    20/61

    videocommunications

    surfacephysics

    optics

    "imaging" psychophysics visualization

    vision, AI computergraphics

    mathematics

    20

  • 8/18/2019 Module 1JH

    21/61

    opaquereflectiveobject

    emittedradiation

    self- sensor s

    reflected radiation

    electricalsignal

    um nousobject emitted

    radiation

    radiation

    alteredradiation

    21

    translucent

    objectemitted

    radiation

  • 8/18/2019 Module 1JH

    22/61

    • Image information is surface information:how an ob ect reflects absorbs radiation- Optical (visual, photographic)-- Ultrasound, sonar (non-EM)

    - Electron Microscopy

    22

  • 8/18/2019 Module 1JH

    23/61

    • Image information is internal information:how an ob ect creates radiation- Thermal, infrared (FLIR)- , , .- Nuclear (particle emission, e.g., MRI)

    23

  • 8/18/2019 Module 1JH

    24/61

    • Image information is internal information:how an ob ect modifies absorbs radiation- X-Rays in many applications-- Tomography (CAT, PET) in medicine

    - “Vibro-Seis” in geophysical prospecting

    24

  • 8/18/2019 Module 1JH

    25/61

    “ ”

    Electromagnetic Spectrum radiofrequency

    cosmicrays

    gammarays

    X-Rays UV

    visible

    IR

    microwave(SAR)

    --4 --2 2 4 6 8 121 10101010101010 10

    10

    wave engt ngstroms1 Å 10

    -10m

    25

  • 8/18/2019 Module 1JH

    26/61

    From the i antic … ?

    The Great Wall

    1 0 28

    m

    (of galaxies)

    26

  • 8/18/2019 Module 1JH

    27/61

    … …

    video camera

    1 m

    27

  • 8/18/2019 Module 1JH

    28/61

    …to thetin .

    -6electron microscope

    28

    m

  • 8/18/2019 Module 1JH

    29/61

    • Images and videos are multi-dimensional≥ 2 dimensions si nals.

    Dimension 3

    2-D image Dimension 2 Dimension 2

    3-D Image

    or video

    29

    Dimension 1

  • 8/18/2019 Module 1JH

    30/61

    • Let’s quantify the geometric relationship -

    projected 2-D image coordinates.

    point light source emitted rays

    objectimage

    Sensingplate,CCDarra

    30

    lens

    emulsion,etc.

    Reflected rays

    focal length

  • 8/18/2019 Module 1JH

    31/61

    - -• Image projection is a reduction of dimension

    (3D-to-2D): 3-D info islost . Getting this infoback isver y hard .

    " f ie ld - o f -v i e w "

    • It is a topicl e n s c e n t erof many years of

    intensive research:

    31

    2 - D i m a g e“Computer Vision”

  • 8/18/2019 Module 1JH

    32/61

    Perspective Projection

    • There is a geometric relationship between3-D s ace coordinates and 2-D ima ecoordinates under perspective projection.

    • We will require some coordinate systems :

    32

  • 8/18/2019 Module 1JH

    33/61

    -

    • (X, Y, Z ) denote points in 3-D space• , , , ,

    • (x, y ) denote points in the 2-D image

    - -• The optical axis passes through both origins

    33

  • 8/18/2019 Module 1JH

    34/61

    • The lens is modeled as a pinhole through whichall light rays hitting the image plane pass.

    • The image plane is one focal length f from thelens. This is where the camera is in focus.

    • The image is recorded at the image plane, using aphotographic emulsion, CCD sensor, etc.

    34

  • 8/18/2019 Module 1JH

    35/61

    Z

    Idealized "Pinhole"Camera Model

    NOTE: this is a LEFT-handedcoordinate system!

    X

    lens center(X, Y, Z ) = (0, 0, 0)

    image plane

    Problem : In this model (and in reality), the image is reversed

    35

    and upside down. It is convenient to change the model to correct this.

  • 8/18/2019 Module 1JH

    36/61

    Z

    Y

    Upright Projection Model y

    X

    image plane

    (Not to scale!)Note: still a LEFT-handed

    lens center (X, Y, Z ) = (0, 0, 0)

    36

    • Let us make our model more mathematical…

  • 8/18/2019 Module 1JH

    37/61

    Z y

    Y(X, Y, Z ) = (A, B, C)

    B C

    =

    = focal length image plane x

    , ,

    X (0, 0, 0) A

    • All of the relevant coordinate axes and labels …

    37

  • 8/18/2019 Module 1JH

    38/61

    A

    B

    a

    b C

    •the relevant data relating (X, Y, Z ) = (A, B, C) toits projection (x, y ) = (a, b).

    38

  • 8/18/2019 Module 1JH

    39/61

    • Triangles are similar if their correspondingan les are e ual :

    α β

    39similar triangles

  • 8/18/2019 Module 1JH

    40/61

    • Similar triangles have their side lengths inthe same ro ortions .

    D = d D d

    β

    D d

    e

    etc=E eF f

    α α γ

    F ef F f

    D d=

    40

  • 8/18/2019 Module 1JH

    41/61

    • Similar triangles solves the relationship between- -

    • e raw e geome ry once more, s memaking apparent two pairs of similar triangles :

    41

    A

  • 8/18/2019 Module 1JH

    42/61

    B B

    b

    a

    b

    a

    A

    • By the Similar Triangles Theorem , wea A b B

    anC C f f = = f

    42

    , , ,C⋅

  • 8/18/2019 Module 1JH

    43/61

    • The relationship between a 3-D point-, , ,

    (x, y ) = · (X, Y ) f

    where f = focal length ,

    which varies with therange Z from the lens

    43

    .

  • 8/18/2019 Module 1JH

    44/61

    - A man stands 10 m in front of you.- He is 2 m tall.

    - Your eye’s focal length is about 17 mm

    on your retina?

    44

    Michael J

  • 8/18/2019 Module 1JH

    45/61

    Michael J

    H

    10 m m m

    , H = 3.4 mm2 m H=

    45

  • 8/18/2019 Module 1JH

    46/61

    Straight Lines Under PerspectiveProjection

    • Why do straight lines (or line segments) in3-D ro ect to strai ht lines in 2-D ima es?• Not true of all lenses, e.g. "fish-eye“ lenses

    .

    Y

    Z y

    3-D line

    To show this to be true, one could

    image plane2-D line write the equation for a line in3-D, and then project it to theequation of a 2-D line…

    46X

    x

    (0, 0, 0)

  • 8/18/2019 Module 1JH

    47/61

    • Easier way:• Any two lines touching the lens center and the 3-D

    line must lie inthe same plane (a point and a linedefine a lane .

    • The intersection of this plane with the image planegives the projection of the line.• The intersection of two (nonparallel) planes is a

    line., - - .

    3-D line

    •In image analysis , this

    2-D line property makes finding straightlines much easier.

    47

  • 8/18/2019 Module 1JH

    48/61

    -

    using charge-coupled device (CCD) sensor.

    • The output is typically a line-by-line (raster)ana og s gna :

    amplifier analog

    video out(NTSC, RS-170)

    vertical scangenerator

    analog shift register

    48photosensitive array

  • 8/18/2019 Module 1JH

    49/61

    • Consists of sampling and quantization .• Sam lin is the rocess of creatin a si nal

    that is defined only at discrete points , from

    one that is continuously defined.• Quantization is the process of convertingeach sample into a finite digital

    representation.• We will explore these in depth later .

    49

  • 8/18/2019 Module 1JH

    50/61

    m lin• Each video raster is converted from acon nuous vo age wave orm n o asequence of voltage samples :

    continuous electrical signal from one scanline

    50sampled electrical signal from one scanline

  • 8/18/2019 Module 1JH

    51/61

    • g a mage s an array o num ers row,

    column) representing image intensities

    rows

    columns

    depiction of 10 x 10 image array

    • Each of these picture elements is called a

    51

    pixel.

  • 8/18/2019 Module 1JH

    52/61

    • The image array is rectangular (N x M)•

    128 x 128 (2 ≈ 16,000 pixels)

    256 x 256 2 ≈ 65 500 ixels

    14

    16

    512 x 512 (2 ≈ 262,000 pixels)1024x1024 (2 ≈ 1,000,000 pixels)

    18

    20

    52

  • 8/18/2019 Module 1JH

    53/61

    • s essen a a e mage e samp e

    sufficiently densely ; else the image qualityw e severe y egra e .

    • Can be expressed mathematically via theSampling Theorem, but the effects arevisually obvious .

    • With sufficient samples, the image appearscontinuous …..

    53

  • 8/18/2019 Module 1JH

    54/61

    between 0 and K-1.

    • There are K = 2 possible gray levels.

    • Each pixel is represented by B bits; usually1 ≤ B ≤ 8.

    54

    -

  • 8/18/2019 Module 1JH

    55/61

    • The pixel intensities or gray levels must beuantized sufficientl densel so thatexcessive information is not lost.

    • This ishard to express mathematically, but,

    obvious .

    55

  • 8/18/2019 Module 1JH

    56/61

    • Total storage required for one digital

    image with 2P x 2Q pixels spatial resolutionand B bits / pixel gray-level resolution is

    B x 2P+Q

    bits.• Usually B=8 and often P=Q=9 . A common

    • Five years ago this wasa lot .

    56

  • 8/18/2019 Module 1JH

    57/61

    • Storing 1 second of a gray-level movie (TVrate = 30 images / sec) requires 7.5 Mbytes.

    • A 2-hour gray-level video (8x512x512x30)

    requires 27,000 megabyte or 27 gigabytesof storage at nowhere near theatre quality.That's a lot today .

    • Later, we will discuss ways tocompressdigital images and videos.

    57

  • 8/18/2019 Module 1JH

    58/61

    ● A color image has vector-valued pixels. For example, 8 bits of

    e , s o ue, an s o reen.●

    So you can think of a color image as a collection of threegrayscale images.. ., , , .

    ► These images are usually called color “bands” or “channels.”► Note: some color spaces like CMYK havemore than 3 bands.

    ,luminance , usually denoted I or Y.

    ● For an RGB color image, the intensity (luminance) is given byI = 0.2989R + 0.5870G + 0.1140B

    ● This formula is not unique (i.e., it is not the only way to convertRGB to luminance), but it iswidely accepted .● Matlab rovides a function r b2 ra to com ute this.

    58

  • 8/18/2019 Module 1JH

    59/61

    ● Some color spaces like YUV and YCrCb represent the grayscale image

    directl .► This was originally done so that black-and-white TV’s could receive color

    TV signals… and just display the Y band.● Many color image processing algorithms process the color bands

    in epen ent y i e graysca e images.► For example, there may be noise that affects the bands independently.► This approach can produce weird changes in the colors, however.

    ● en, s es ra e o mo y an mage w ou c ang ng e co ors.► For example, we might want to just make a color image brighter without

    making any visually obvious changes to the colors of the pixels.’ ,

    image (grayscale image) only, then reconstruct the colors.● We’ll spend most of our time on grayscale images in this class.● Development of true vector color filters is an active research area.

    59

    C l

  • 8/18/2019 Module 1JH

    60/61

    Color

    R G B

    60Intensity

    Common Image Formats

  • 8/18/2019 Module 1JH

    61/61

    Common Image Formats• JPEG (Joint Photographic Experts Group) images are compressed

    with loss – see Module 7. All digital cameras today have the option tosave images in JPEG format. File extension: image.jpg

    • TIFF (Tagged Image File Format) images can be lossless (LZWcompressed) or compressed with loss. Widely used in the printing.

    extension: image.tif

    • GIF (Graphic Interchange Format) an old but still-common format,limited to 256 colors. Lossless and lossy (LZW) formats. Fileextension: image.gif

    • .

    true color (16 million colors). File extension: image.png

    • BMP (bit mapped) format is used internally by Microsoft Windows.

    61

    Not compressed. Widely accepted. File extension: image.bmp