You are on page 1of 149

IntroductiontoImage Processing

Lecture_IP 1 GrayScale/ColorImages,MATLAB

ImageProcessingBasics
i image f ( x, y) x, y are spatial coordinates f is image amplitude (intensity, gray level, color level) i digital image x, y, f finite and discrete f ( x0 , y0 ) called a picture element (pel) or pixel

ImageProcessingBasics

512 x 512 pixels

256 x 256 pixels

128 x 128 pixels

64 x 64 pixels
All images interpolated to 512 x 512 format

512 x 512 image

64 x 64 image interpolated to 512 x 512 format

VaryingIntensityLevels

VaryingIntensityLevels

ImageProcessingBasics
Whydowecareaboutimages,image processing?
Apictureisworth1000words rich informationsourcefromvisualdata

ImageProcessingBasics Imagetypes:
naturalphotographicimages(jpg) artisticandhanddonedrawings(pdf) scientificimages(satellitephotos,medicalimages suchasXraysandMRIs,catscans,UVscans) (bmp,tiff) engineeringdrawings,technicalspecifications,etc. (scannedandconvertedtodesiredformat,e.g., png portablenetworkgraphics)

WhyProcessImages
EnhancementandRestoration(PhotoShop)
removalofartifactsandscratchesfromoldphotos improvecontrastandlightingofexistingphotos correctblurring(orredeye)duetomotion(orcameraflash)inphotos

TransmissionandStorage(DigitalCamera)
compressionforreducedstorageandtransmissioncosts(e.g.,images transmittedfromouterspace) encodingforefficienttransmission

ImageUnderstanding(ImageSearch)
analysisandclassificationofimagecomponents facerecognitionandclassification objectidentification(e.g.,people,beach,sunset)

SecurityandRightsProtection(Legal)
encryptionandwatermarking

WhyDigitalImageProcessing
Losslessrepresentationsofimages
nodegradationandperfectreproductionpossible abilitytoduplicatewithoutanylossofquality

Sophisticatedandpowerfuldigitalsignalprocessing algorithms
AdobePhotoshop;PhotoshopElements easytoprocessandmanipulateimages

Lowcostofstorageandtransmission

moderncompressionmethodscanreducestoragerateby factorsof30ormorewithalmostnolossofperceivedquality singleCDcanstorethousandsofphotos

CDStorageforImages
StorageavailableonCD
1.4Mbpsx60secx80minutes=6.72Gb =0.84 GB=840MB

Assume1megapixelcameragiving24bit colorimages
eachpictureuses1megapixelx24bits/pixel=24 Mb=3MB withoutcompresscanstore840MB/3MB=280 pictures withjpegcompressionof30to1canstore840 MB/0.1MB=8400pictures

ProcessedImages

Famousprocessed/spoofedimagefrom WorldTradeCenter9/11/2001

ImagesFromOuterSpace

ImagesFromOuterSpace

ApplicationsofDigitalImageProcessing
Compression
standardsbased,JBIG,JPEG,JPEG2000

ManipulationandRestoration
noiseremoval noisereduction morphingpairsofimages restorationofblurredanddamagedimages visualmosaicing andvirtualviews facedetection visibleandinvisiblewatermarking errorconcealmentandresilienceintransmission

Other

Image/VideoCompression
Colorimagewith600x800pixels
withoutcompressioneachpixelneeds8bitsforeachofred, greenandbluecomponents totalbitrateof600*800*24bits/pixel=11.52megabits=1.44 megabytes afterJPEGcompressioncanachievealmostlossless (perceptually)compressionusing89kilobytesgivinga compressionratioofabout16:1

Moviewith720x480pixelsperframe,30 frames/sec,24bits/pixel(colorframes)
withoutcompressionthetotalbitrateis720x480x30x24=243 megbits/secondor30megabytespersecond withMPEGcompression(forDVD)cancompressvideotoabout 5megabits/secondoracompressionratioofabout48:1

ImageQuantization

Original Image (8-bit gray scale)

Original Image (5-bit gray scale)

Original Image (2-bit gray scale)

Original Image (4-bit color scale, pseudo-color)

ImageCompression

8 bit

0.5 bit

0.33 bit

0.25 bit

ImageCompression 24 bit 1 bit

0.5 bit

0.25 bit

ImageCompression

ExamplesofImageProcessing
Enhancement,Denoising, Deblurring,SamplingRate Conversion

ImageEnhancement(enhance_image.m)
% read and display dark image I=imread('pout.tif'); imshow(I); % enhance contrast and display by computing image histogram and equalizing histogram levels figure,imhist(I); I2=histeq(I); % display enhanced image % display enhanced image histogram figure,imshow(I2,'border','tight'); figure,imhist(I2);

ContrastAdjustedImage

Original image with poor contrast

Image histogram adjusted image

ImageDenoisingMedianFiltering

% Read in the image and display it. I = imread('eight.tif'); imshow(I,'border','tight');

% Add noise to the image. % Filter the noisy image with an J = imnoise(I,'salt & pepper',0.02); averaging filter and display the figure, imshow(J,'border','tight') results. K=filter2(fspecial('average',3),J)/255; figure, imshow(K,'border','tight') % Now use a median filter to filter the noisy image and display the % results. Notice that medfilt2 does a better job of removing noise, % with less blurring of edges. L = medfilt2(J,[3 3]); figure, imshow(L,'border','tight')

NoiseCorruptionofImage

Original image, noise corrupted during digitization process

Cleaned up image using appropriate size median filter

camerman.tifimage filtered

camerman.tifimage+noiseand filtered

camerman.tifimage+noiseand medianfiltered

ImageDenoisingAdaptiveFiltering
(1) (2) (3)

(1) Original Saturn Image Color Image

(2) Gray Scale Saturn Image (4) Noise Removed Image Using Wiener Filter

(4)

(3) Saturn Image with Additive Gaussian Noise

ImageDenoising

ImageLightingCorrection
Top Left: Image captured under poor lighting conditions (background brightness varying across image) Top Right: Foreground objects removed by applying a 25 x 25 grayscale dilation operation Bottom Left: Original image subtracted from background image Bottom Right: Color inversion and improved contrast by adjuisting image histogram

FrequencyDomainFiltering
Top Left: Image with a corrugated effect due to fault in the acquisition process Top Right: Two-dimensional FFT of image

Bottom Left: Filtering in the frequency domain (removal of bright spots) Bottom Right: Cleaned up image without the corrugated background

GaussianFilteringofImages

Original Lena Image

Lena Image Filtered by 2D Gaussian Filter with Sigma=3

GaussianFilteringofImages

Original Lena Image

Lena Image Filtered by 2D Gaussian Filter with Sigma=1

GaussianFilteringofNoisyImages

Lena Image with Additive Gaussian Noise (0.3*randn(512))

Noisy Lena Filtered with 2DGaussian Filter with Sigma=3

GaussianFilteringofNoisyImages

Lena Image with Additive Gaussian Noise (0.3*randn(512))

Noisy Lena Filtered with 2DGaussian Filter with Sigma=1

Image Measurement Extraction


Top Left: Original image with objects (want to estimate distribution of the sizes of the objects) Top Right: Threshold image to separate objects from background (binary) Second Row Left: Watershed separation of image to improve object separation Second Row Right: Logical AND of two images on left-side frames Histogram: Measure object properties (object areas) and display resulting object area histogram

ImageDeblurring

ImageDeblurring LucyRichardsonFilter

% Read an image into the MATLAB workspace.; crop image I = imread('board.tif'); I = I(50+[1:256],2+[1:256],:); figure;imshow(I,'border','tight'); title('Original Image');

% Use deconvlucy to restore the blurred and noisy image luc1 = deconvlucy(BlurredNoisy,PSF,5); figure; imshow(luc1,'border','tight');

% Create the PSF. PSF = fspecial('gaussian',5,5); % Create a simulated blur in the image and add noise. Blurred =imfilter(I,PSF,'symmetric','conv'); V = .002; BlurredNoisy = imnoise(Blurred,'gaussian',0,V); figure;imshow(BlurredNoisy,'border','tight'); title('Blurred and Noisy Image');

(e) Original Image Up-Sampled by 2:1, then DownSampled by 2:1

(a) Original Image

(c) Original Image DownSampled by 2:1, then UpSampled by 2:1

(b) Original Image Down-Sampled by 2:1

ImageSampling RateChanges

(d) Original Image Up-Sampled by 2:1

OtherImageProcessingEffects
VisualMosaic,Watermarking,Error Concealment,Resampling

VisualMosaic

Watermarks
Avisiblewatermarkisavisibletranslucentimagewhichis overlaidontheprimaryimage. Awatermarkallowstheprimaryimagetobeviewed,butstill marksitclearlyasthepropertyoftheowningorganization. Itisimportanttooverlaythewatermarkinawaywhichmakes itdifficulttoremove,ifthegoalofindicatingpropertyrightsis tobeachieved.

Watermarks
Aninvisiblewatermarkisanoverlaidimagewhichcannot beseen, butwhichcan bedetectedalgorithmically. Differentapplicationsofthistechnologycallfortwoverydifferent typesofinvisiblewatermarks:
Awatermarkwhichisdestroyedwhentheimageismanipulated digitallyinanywaymaybeusefulinprovingauthenticityofanimage. Ifthewatermarkisstillintact,thentheimagehasnotbeen "doctored."Ifthewatermarkhasbeendestroyed,thentheimagehas beentamperedwith.Suchatechnologymightbeimportant,for example,inadmittingdigitalimagesasevidenceincourt. Aninvisiblewatermarkwhichisveryresistanttodestructionunder anyimagemanipulationmightbeusefulinverifyingownershipofan imagesuspectedofmisappropriation.Digitaldetectionofthe watermarkwouldindicatethesourceoftheimage.

VisibleDigitalWatermarks

CourtesyIBMWatsonwebpageVatican DigitalLibrary

InvisibleWatermark

Upperrighthalfofimageinvisibly watermarked,lowerlefthalfnot watermarked.

Upperpatterniswatermarkdetected fromupperrighthalfofimage;lower patterniswatermarkdetectedfrom lowerlefthalfofimage.

ErrorConcealment

ErrorConcealment

UnderSamplingofImage

Propersamplingofimage

Undersamplingofimage notetheMoire patternsduetoimagealiasing

ImageProcessingUsingMATLAB

RelatedFieldstoImageProcessing
Imageanalysis
determineimageproperties(typeofimage: photo,drawing,medicalimage) determineimagecomponents(objectsinimage, scenedescription)

Computervision
analyzeimagetodetermineimpedimentsto motionofautonomousvehicles identifyfacesinimages identifyfishincoralreef(JoeWildervision lecture)

ImageProcessingLevels
LowLevelProcessing(inputandoutputareimages)
noisereduction denoising ofimages contrastenhancement improvelightingforviewingimages imageenhancement sharpenimageatedgesorinpointsofmaximal interest

MidLevelProcessing(inputisanimage,outputisasetof imageattributes)
imagesegmentation(partitionintoregionsorobjects);edgedetection objectdescription(e.g.,size,color,orientation) objectclassification(recognition,e.g.,faces)

HighLevelProcessing(Inputisanimage,outputisimage understanding)
makingsenseofensembleofrecognizedobjects(e.g.,pathplanning, alerting,alarming)

MATLABImageProcessing

MATLABImageProcessingFundamentals
f ( x, y ) M rows, N columns M N size image conventional indexing (0 : M 1 0 : N 1) MATLAB Image: f(r,c) (1: M ,1: N )

Conventionalimage coordinates

MATLABimage coordinates

MATLABImageProcessing
f (0, 0) f (1, 0) f ( x, y ) = f ( M 1,1) f (0,1) f (1,1) ... ... f (0, N 1) f (1, N 1) f ( M 1, N 1)

f ( M 1, 2) ...

Conventional Image Indexing

f(1,1) f(1,2) ... f(1,N) f(2,1) f(2,2) ... f(2,N) f= f(M,1) f(M,2) ... f(M,N)
MATLAB Image Indexing

f(1, 1) = f(0,0) f(M, N) = f(M-1,N-1) Equivalence of MATLAB and Conventional Image Indexing

ImageFormatsforMATLAB

ImageIOinMATLAB
Imagereadcommand:
f=imread(filename);%f=imread(lena.tif); [M,N]=size(f);%M=512,N=512,;pixels8bits

Imagedisplaycommand:
imshow(f,[low,high]);%displayasblackimage intensityatorbelowlow;displayaswhiteimage intensityatorabovehigh; imshow(f,[]);%expandsdynamicrangesothat max=white,min=black impixelinfo;%usescursortoreadimage coordinatesandpixelintensityvalues

Example
f=imread(lena.tif); whos name:f size:512x512 bytes:262144 class:uint8 imshow(f);
Full path read: f=imread(C:\data\matlab\image files\lena.tif);

Example

f=imread(saturn.tif); [M,N]=size(f);%M=328,N=448 imshow(f); M=size(f,1);numberofrowsinf N=size(f,2);numberofcolumnsinf

imshow(f,[64,128]);

Example

Example
f=[255230180;155130105;80500]; imshow(f,[],InitialMagnification,fit);

ImageTool

Pixel Value Overview Pixel Region

Cropped Image

f=imread(rose.tif); imtool(f);

ImageTool

imtool is extremely useful tool for gaining an understanding of image properties

ImageTool

pseudo-color with colormap set to hot; filename:rose_hot.jpg

ImageIOinMATLAB
Imagewritecommand:
imwrite(f,filename);%writestofilename(musthavevalid extension) imwrite(f,filename,tif);%writestofilename.tif imwrite(f,filename.jpg,quality,q);%0q100

Filesizeinformation:
imfinfo filename;imfinfo(filename); K=imfinfo(filename);%Kisstructurevariable K.Height,K.Width;

Exportfigureimagestodisk:
printfno dfileformat rresno filename;%noisfigurenumber; fileformat fromlist;resno=dpiresolutionoffile printf1dtiff r300saturn_clip.tif;%savefigure1,intiffformat, using300dpiresolution,tofilenamesaturn_clip.tif

Example imfinfo(saturn.tif);
Filename:'saturn.tif' StripByteCounts:[19x1double] FileModDate:'07Jul200415:31:54' XResolution:72 FileSize:144184 YResolution:72 Format:'tif' ResolutionUnit:'Inch' FormatVersion:[] Colormap:[] Width:438 PlanarConfiguration:'Chunky' Height:328 TileWidth:[] BitDepth:8 TileLength:[] ColorType:'grayscale' TileOffsets:[] FormatSignature:[7373420] TileByteCounts:[] ByteOrder:'littleendian' Orientation:1 NewSubFileType:0 FillOrder:1 BitsPerSample:8 GrayResponseUnit:0.0100 Compression:'Uncompressed' MaxSampleValue:255 PhotometricInterpretation:'BlackIsZero' MinSampleValue:0 StripOffsets:[19x1double] Thresholding:1 SamplesPerPixel:1 ImageDescription:[1x168char] RowsPerStrip:18

Example

Example

ImageSize
f=imread(circuitboard.tif); imwrite(f,circuitboard_r300.tif,compre ssion,none,resolution,[300300]); K=imfinfo(circuitboard_r300.tif); K.XResolution=300;K.YResolution=300; 1.5x1.5inches imwrite(f,circuitboard_r200.tif,compre ssion,none,resolution,[200200]); K=imfinfo(circuitboard_r200.tif); K.XResolution=200;K.YResolution=200; 2.25x2.25inches

ImageDataClasses

Dataclassesdoublethroughsinglearenumericdataclasses; Dataclasscharisacharacterclassanddataclasslogicalisa logicalclass.

ImageDataClasses
f=imread(lena.tif); whos
NameSizeBytesClassAttributes f512x512262144uint8

max(f(:))=245;min(f(:))=24; g=double(f); whos


NameSizeBytesClassAttributes f512x512262144uint8 g512x5122097152double

max(g(:))=245;min(g(:))=24;

ImageDataClasses
AllnumericcomputationsinMATLABare doneusingdoublequantities Dataclassuint8ismostlyusedforreadingand storingimagesas8bitimages Dataclassdoublerequires8bytesper number;dataclassuint8requires1byteper number

ImageTypes
Intensityimages generallygrayscaleimages whosevaluesrepresentimageintensity;classcanbe double(floatingpoint),uint8(integersfrom0to 255),uint16(integersfrom0to65535)orscaled doubleintherange[0,1] Binaryimages logicalarrayof0sand1s;obtained usingfunctionlogicalonanumericarrayofonesand zeros(halftone) Indexedimages imagewithtwocomponents, namelyadatamatrixofintegers,X,andacolormap matrix,map. RGBimages imagewith3complementarycolorsets ofintensity

3x3BinaryImages

fht{1}=[0 0 0; 0 0 0; 0 0 0]; fht{2}=[0 255 0; 0 0 0; 0 0 0]; fht{3}=[0 255 0; 0 0 0; 0 0 255]; fht{4}=[255 255 0; 0 0 0; 0 0 255]; fht{5}=[255 255 0; 0 0 0; 255 0 255]; fht{6}=[255 255 255; 0 0 0; 255 0 255]; fht{7}=[255 255 255; 0 0 255; 255 0 255]; fht{8}=[255 255 255;0 0 255; 255 255 255]; fht{9}=[255 255 255; 255 0 255; 255 255 255]; fht{10}=[255 255 255; 255 255 255; 255 255 255]; lh=[ones(1,19)*128]; lv=[ones(3,1)*128]; f=[fht{1} lv fht{2} lv fht{3} lv fht{4} lv fht{5};lh; fht{6} ... lv fht{7} lv fht{8} lv fht{9} lv fht{10}]; f1=uint8(f); figure;imshow(f1,'InitialMagnification','fit');

DigitalImageHalftoning
BinaryPixels(0,255)
Halftoning: simulate continuous tone images through the use of dots, varying in size, shape or spacing.

Varying size dots; view from a distance.

ImageHalftoning

lena.tif half tone image

ImageCropandBlowUp

Lena Eyes Region

Lighthouse Tower Blow-Up

HalfToneImages ReducedinSize

OtherPatterns

% Black-to-white pattern: g=[ones(256,1)*(0:255)]; figure;imshow(g,[ ]);

Dithered Pattern

Dithered pattern: fht{1}=[0 0 0; 0 0 0; 0 0 0]; fht{2}=[0 255 0; 0 0 0; 0 0 0]; fht{3}=[0 255 0; 0 0 0; 0 0 255]; fht{4}=[255 255 0; 0 0 0; 0 0 255]; fht{5}=[255 255 0; 0 0 0; 255 0 255]; fht{6}=[255 255 255; 0 0 0; 255 0 255]; fht{7}=[255 255 255; 0 0 255; 255 0 255]; fht{8}=[255 255 255;0 0 255; 255 255 255]; fht{9}=[255 255 255; 255 0 255; 255 255 255]; fht{10}=[255 255 255; 255 255 255; 255 255 255]; ght=[]; for row=1:256 gloc=[]; for col=1:256 gloc=[gloc fht{floor(g(row,col)/25.6)+1}]; end ght=[ght ; gloc]; end figure,imshow(ght,[ ])

ImageTypes
toconvertfromgrayscaleimagetobinary image:
f=imread(image.tif); g=f; g(find(f>=128))=1; g(find(f<128))=0; b=logical(g); islogical(b)
1

ImageTypes

lena.tif (512 x 512 x 8 bits)

lena_binary.tif (512 x 512 x 1 bits)

ImageConversions
Imagesclassifiedaccordingtobothclass_image (double,uint8,)andtype_image (intensity,binary, indexed,RGB) Canreadilyconvertbetweendataclassesandimage types,e.g.,
Dataclassconversions:B=class_name(A); A:classuint8image B=double(A)convertsfromuint8todouble C:classdoubleimage D=uint8(C)convertsfromdouble[0255]touint8(integers in[0255]);uint8truncatesbelow0andabove255 g=im2uint8(f)scalesfto[0255]thenconvertstointeger values

ImageConversions

ImageConversions
Processing Model #1
f, Gray Scale Image, uint8 [0 255] g, Gray Scale Image, double [0 1] h, Gray Scale Image, double [0 1] p, Gray Scale Image, uint8 [0 255]

g=im2double(f)

Image Processing

scaleby255; p=uint8(h)

f, Gray Scale Image, uint8 [0 255]

g=mat2gray(f)

g, Gray Scale Image, double [0 1]

Image Processing

h, Gray Scale Image, double [0 1]

p=im2uint8(h)

p, Gray Scale Image, uint8 [0 255]

Processing Model #2

ImageConversions
Processing Model #1:
im2double:convertintegerstodoubleprecision in[01],scale by 255; clip below 0, above 255 uint8:convertsdoubletointegersinrangeof [0255] mat2gray:scaletodoubleinrange[01]; minto0,maxto1 im2uint8:scaleby255andintegerize torange [0255];clipbelow0,above255

Processing Model #2:

ImageConversions im2uint8
im2uint8scalesanimageintherange[01]toan imageintherange[0255]byscalingtheimage valuesby255andclippingbelow0andabove255

f=[0.50.5;0.751.5]; g=im2uint8(f);Whatisg? g=[0128;191255]; f<00;%0.5goesto0 f>11;%1.5goesto255 scaleby255;0.5goesto128,0.75goesto191

ImageConversions im2double
im2doubleconvertsinputtoclassdouble
ifinputofclassuint8,uint16orlogical,im2double convertstoclassdoublewithvaluesinrange[01] ifinputofclasssingleordouble,im2double returnsarrayofclassdouble,butnumerically equaltotheinput mat2graycanbeusedtoconvertanarrayofany classtoadoublearraywithvaluesinrange[01]

ImageConversions im2double
Example:
h=uint8([2550;128200]); g=im2double(h); g=[0.09800.1961;0.50200.7843];%converts integerstodoubleprecisionvaluesinrange[01];

ImageConversions mat2gray
mat2grayconvertsimageofanyclasstoarray ofclassdoublescaled totherange[01] callingsequenceis:
g=mat2gray(A,[Amin,Amax]);%ghasvaluesin range0(black)to1(white)withclippingbelow Amin andaboveAmax;Amin andAmaxneednot bespecified routinefindsminandmaxpriorto conversionandtreatsthemasAmin andAmax

ImageConversionExamples mat2gray
h=uint8([2550;128200]); g=mat2gray(h);%whatisg? g=[00.1429;0.58861];Amin=25;Amax=200
25 0;50 (5025)/175=0.1429; 128 (12825)/175=0.5886;200 (20025)/175=1

g=im2bw(f,T);%producesabinaryimage,g, fromintensityimage,f,bythresholdingsuch thatallpixels<Tgoto0,allothersgoto1

SimpleExample

f=imread(lena.tif); figure,imshow(f); max(f(:))=245; min(f(:))=24;

f1=mat2gray(f); f2=im2uint8(f1); figure,imshow(f2); max(f2(:))=255; min(f2(:))=0;

ImageConversionExamples im2bw
im2bwperformsconversiontoclasslogical wherenonzeroelementsareconvertedto1s andzeroelementsareconvertedto0sinthe output equivalentlywecoulddothefollowing:
g=f>T;%whereTisthethresholdforconversion to1sandbelowTinputsconvertedto0s g=im2bw(f,T);performssameoperationsas above;thresholdTmustbeinrange[01];foruint8 input,thresholdisT*255

ImageConversionExamples
f=[12;34]; converttobinarysuchthatvalues1and2become0andthe othertwovaluesbecome1
firstconverttorange[01] g=mat2gray(f);g=[00.3333;0.66671.0]; nextconverttobinaryusingthresholdof0.6 gb =im2bw(g,0.6);gb=[00;11]

alternativelycanuse
gb =f>2;gb=[00;11]

convertgb tonumericalarrayof0sand1s:
gbd =im2double(gb);gbd=[00;11]

gbd=im2double(im2bw(mat2gray(f),0.6)); gbd =double(f>2);

ImageConversionExamples
25 50 f1= (double) 75 100 25 50 f 2 = uint8( f 1) = 75 100
255 255 f 3 = im2uint8( f 1) = 255 255

0.5 0.75 g1 = (double) 0.85 0.99


1 1 g 2 = uint8( g1) = (integerize to [0 255] range) 1 1

128 191 g 3 = im2uint8( g1) = (scale by 255 and integerize) 217 252

0.51 0.33 0 0 g 4 = mat2gray( g 1) = f 4 = mat2gray( f 1) = 0.71 1 (scale to [0 1] range) 0.66 1 25 50 f 5 = im2double( f 1) = 75 100
0.5 0.75 g 5 = im2double( g1) = (convert to double) 0.85 0.99

ImageConversionExamples
25 50 f1= (uint8) 75 100 25 50 f 2 = uint8( f 1) = 75 100
255 255 f 3 = im2uint8( f 1) = 255 255 0.33 0 f 4 = mat2gray( f 1) = 1 0.66

25 50 f 5 = im2double( f 1) = 75 100

ImageConversionExamples

original lena.tif image f = imread(lena.tif); % image is 512 x 512 gray scale image (8 bits/pixel)

ImageConversionExamples
f = imread(lena.tif); % image is 512 x 512 gray scale image (8 bits/pixel) g = mat2gray(f); % convert scale from [0 255] to [0 1] h = dither(g); % convert to black-and-white image using dithering, 512 x 512 bits

ImageConversionExamples
hnd = im2bw(g); % convert to black-andwhite image without dithering

ImageConversionExamples

ImageConversionExamples

ImageConversionExamples

MATLABDemo:Counting GrainsofRiceinan Image

CountingGrainsofRice
rawimageshowsgrainsofrice
theimagehasnonuniformillumination;the backgroundisbrighterinthecenteroftheimage thanatthebottom goalistoenhancetheimagetocorrectforthe nonuniformillumination usetheenhancedimagetoidentifyindividual grains computethegrainstatistics

Counting Grains of Rice


% Step 1 -- read in image and display I=imread('rice.png'); imshow(I); % Step 2 -- estimate background using morphological opening operation % Morphological opening is an erosion followed by a dilation, % using the same structuring element for both operations. % The opening operation has the effect of removing objects % that cannot completely contain the structuring element. % strel('disk',15): disk-shaped structuring element with radius 15 % structuring element must be bigger than a single grain of rice background=imopen(I,strel('disk',15)); % Step 3 -- view background approximation as a surface; first convert background to double and use surf command to display every 8th pixel in each direction figure; surf(double(background(1:8:end,1:8:end))); % limit range to that of uint8 data to match input image (for subtraction) zlim([0 255]); % reverse y-axis of display to provide better view of surface set(gca,'ydir','reverse'); % [0 0] is origin of image; high part of curve indicates highest pixel values of background; this occurs at middle rows of image

Counting Grains of Rice


% Step 4 -- subtract background image from original image I2=I - background; figure; imshow(I2); % Step 5 -- increase image contrast -background is too dark, using imadjust I3=imadjust(I2); figure; imshow(I3);

Counting Grains of Rice


% Step 6 -- threshold image to create binary version of image in order to count the number of grains of rice % graythresh automatically computes threshold for conversion from gray % scale to binary image level=graythresh(I3); % im2bw converts grayscale image to binary image using thresholding bw=im2bw(I3,level); % bwareaopen removes background noise bw=bwareaopen(bw,50); figure; imshow(bw); % Step 7 -- identify objects in image % bwconncomp finds all connected components (objects) in binary image cc=bwconncomp(bw,4); cc.NumObjects ans = 95 % Step 8 -- examine a single detected object; each distinct object is labeledshow 50th connected component grain=false(size(bw)); grain(cc.PixelIdxList{50})=true; figure; imshow(grain);

Counting Grains of Rice


% Step 9 -- view all objects % create a label matrix and display it as a pseudo-color indexed image % labelmatrix creates a label matrix from output of bwconncomp labeled=labelmatrix(cc); whos labeled Name labeled Size 256x256 Bytes Class 65536 uint8 Attributes

% label2rgb creates colormap, choose background color, and maps objects in % labelmatrix map to colors in colormap RGB_label=label2rgb(labeled,@spring,'c','shuffle'); figure; imshow(RGB_label); % Step 10 -- compute area of each object % regionprops operates on cc to compute object areas graindata=regionprops(cc,'basic') % find area of 50th component graindata(50).Area % Step 11 -- compute area-based statistics grain_areas=[graindata.Area]; % find grain with smallest area [min_area,idx]=min(grain_areas); grain=false(size(bw)); grain(cc.PixelIdxList{idx})=true; figure; imshow(grain); min_area

graindata = 95x1 struct array with fields: Area Centroid BoundingBox ans =194 min_area = 61

Counting Grains of Rice


% Step 12 -- create area histogram nbins=20; figure; hist(grain_areas,nbins); title('Histogram of Rice Grain Area');

ArrayIndexing
V=[13579];%1row,5columns V=V(:)=[1;3;5;7;9];%5rows,1column V(1:3)=[135];%1row,3columns V(3:end)=[579];%1row,3columns V(end:2:1)=[951];%1row,3columns V([145])=[179];%1row,3columns V=linspace(a,b,n);generatesrowvectorbeginning ata,endingatb,withnequispacedelements V=linspace(1,9,5)=[13579];%1row,5columns

MatrixIndexing
A=[123;456;789];%3rows,3columns A(2,3)=6;%secondrow,thirdcolumnentry A(:,3)=[3;6;9];%thirdcolumn,allentries A(2,:)=[456];%secondrow,allentries A(1:2,1:3)=[123;456];%firstandsecondrows,allthree columns A(end,end)=9;%lastentryinmatrix A(end,end 2)=7;%entryinendrow,endcolumn2 A(2:end,end:2:1)=[64;97];rows23,columns3and1 (backwards) A([13],[23])=[23;89];%rows1and3,columns2and3 A(:)=[1;4;7;2;5;8;3;6;9];%columnsconcatenated

MatrixOperations matrixsums
matrixsums(3possiblewaysofcomputing)
A = [1 2 3; 4 5 6; 7 8 9]; % 3 rows, 3 columns col_sums = sum(A); % col_sums=[12 15 18] total_sum = sum(col_sums); % total_sum=45 total_sum1 = sum(A(:)); % total_sum1=45; total_sum2 = sum(sum(A)); % total_sum2=45

MaxOperator
C=max(A);%C=rowvectorwithmaxofeach column C=max(A,B);%C=arrayoflargestelementsof AorB C=max(A,[],dim);%Chasthelargest elementsalongdimensionofAspecifiedby scalardim [C,I]=max(A);%Cisarowvectorwithmaxof eachcolumn,andIisarowvectorwithrowindex foreachcolumnmax

MaxOperatorExamples
A=[147;258;369];B=[963;852;741]; C=max(A)=[369];%maximumofeachcolumn C=max(A,B)=[967;858;769];%largest elementsofAorBateachrowandcolumnindex C=max(A,[],2)=[7;8;9];%largestelements alongdimensionofAspecifiedbylastargument (2=rows,1=columns) C=max(A,[],1)=[369]; [C,I]=max(A)=max(A,[],1);C=[369];I=[333];% I=indicesofmaximums [C,I]=max(A,[],2);C=[7;8;9],I=[333];

MatrixOperations linearindexing
useasinglesubscripttoindexamatrix example:
0.5 0.3333 0.25 1 0.5 0.3333 0.25 0.2 H= 0.3333 0.25 0.2 0.1667 0.25 0.2 0.1667 0.1429

H([211]);%H=[0.50.2] extracts2nd and11th elementsofH;elementscountedbycolumns extractvaluesofHat(1,3),(2,4)and(4,3) wedo thisbyextractingrowandcolumnindicesofthethree pixels,i.e.,(1,3)mapsto9;(2,4)mapsto14;(4,3) mapsto12

MatrixOperations linearindexing
MATLABfunctionssub2indandind2sub convertbackandforthbetweenrowcolumn subscriptsandlinearindices
linear_indices =sub2ind(size(H),r,c);%risthe rowindex(orarrayofindices),cisthecolumn index(orarrayofindices) - linear_indices = sub2ind([4 4],[1 2 4], [3 4 3]); % linear_indices = [9 14 12]; [r,c]=ind2sub(size(H),linear_indices); - [r, c]=ind2sub([4 4], [9 14 12]); % r=[1 2 4], c=[3 4 3]

MatrixIndexingExample
f=imread(rose.tif); imshow(f); fp=f(end:1:1,:); flipvertically

(c) - fc=f(257:768,257:768); % image section (d) fs=f(1:2:end,1:2:end); % subsampled image (e) = plot(f(512,:)); horizontal % scan line

StandardMATLABArrays
zeros(M,N) generatesanMrow,Ncolumnarrayof 0sofclassdouble ones(M,N) generatesanMrow,Ncolumnarray of1sofclassdouble rand(M,N) generatesanMrow,Ncolumnarray whoseentriesareuniformlydistributedrandom numbersintheinterval[01] randn(M,N) generatesanMrow,Ncolumnarray whosenumbersarenormallydistributed(i.e., Gaussian)randomnumberswithmean0and variance1

StandardMATLABArrays
A=5*ones(3,3);%A=[555;555;555] B=randn(1,5000);hist(B,100);

ArrayandMatrixArithmeticOperators

ArrayandMatrixArithmeticOperators
a1 a 2 b1 b 2 A= , B= a a b b 3 4 3 4

i The array product of A and B is: a1b1 a 2b 2 A.* B = a b a b 3 3 4 4


i whereas the matrix product yields the familiar result: a1b1 + a 2b3 a1b 2 + a 2b 4 A *B = a 3 b 1 a 4 b 3 a 3 b 2 a 4 b 4 + +

MATLABmfunction
writeMATLABfunctionthatblendstwoinputimagesinequal weight function[w,wmax,wmin]=imblend(f,g) %wisablendoffandgwithwmaxandwminthemaximumand minimumvaluesoftheblend w1=0.5*f; f = [1 2; 3 4]; g = [1 2; 2 1]; w2=0.5*g; [w, wmax, wmin] = imblend(f, g); w=[1 2; 2.5 2.5]; w=w1+w2; wmax=2.5; wmax=max(w(:)); wmin=1; wmin=min(w(:)); %couldusew=0.5*(f+g);%howeverwhen(f+g)exceeds 255(forauint8array)thesumsaturatesandwegetawrong result

MATLABmfunction
toalleviatethesaturationproblemuse MATLABfunction g=imlincomb(k1,f1,k2,f2) k1=weightforf1; f1=image1; k2=weightforf2; f2=image2; w=imlincomb(0.5,f,0.5g);%thisno longersaturatesonuint8arrays

ArrayandMatrixArithmetic OperatorsforImages(uint8arrays)

MATLABRelationalandLogicalOperators

A==B;1whenA(i,j)=B(i,j),0otherwise A~=B;1whenA(i,j)B(i,j),0otherwise A>=B;1whenA(i,j)B(i,j),0otherwise A<=B;1whenA(i,j)B(i,j),0otherwise

A=[120;045];B=[123;011]; A&B=[110;011]; A|B=[111;011]; ~A=[001;100]; ~B=[000;100]; A==B=[100;100]; A~=B=[011;011]; A>=B=[110;111]; A<=B=[101;100];

MATLABRelationalExamples
A=[123;456;789]; B=[024;356;349]; A==B;[010;011;001]; A>=B;[110;111;111]; C=[120;045]; D=[123;011]; C&D;[110;011]; C|D;[111;011];

ImageAddition

ImageSubtraction

Image 1 Image 2; tendency to blacks (intensities near 0)

Image 1 Image 2; histogram equalized; improved brightness and contrast (still dark image)

MATLABFunction,fgprod
WriteMATLABfunction,fgprod,thatmultipliestwoinputimages andoutputstheproduct,maxandminoftheproduct,andthe normalizedproductwhosevaluesareinthe[01]range.
function[p,pmax,pmin,pn]=fgprod(f,g) %FGPRODcomputestheproductoftwoimages. %[P,PMAX,PMIN,PN]=FGPROD(F,G)outputstheelementbyelementproduct %oftwoinputimages,FandG,theproductmaximumandminimumvalues, %andanormalizedproductarraywithvaluesintherange[0,1].Theinputimages %mustbeofthesamesize.Theycanbeofclassuint8,uint16,ordouble.The %outputsareofclassdouble. fd=double(f);%usedoubleratherthanim2doubletoretainoriginalintensityrange gd=double(g); p=fd.*gd; pmax=max(p(:)); pmin=min(p(:)); pn=mat2gray(p);%obtainanormalizedarrayintherange[0,1]

MATLABFunction,fgprod

Product of two images; again there is a tendency to black as the product of two numbers (both <1) is smaller than either number (i.e., it tends to 0 which is black.

Product of two images histogram equalized. We see a huge improvement in brightness and contrast as the histogram fills in white shades from dark shades.

MATLABFlowControl

MATLABFlowControl Switch
switchswitch_expression casecase_expression statement(s) case{case_expression1,case_expression2,} statement(s) switch newclass otherwise case uint8 statement(s) g = im2uint8(f); end case uint16
g = im2uint16(f); case double g = im2double(f); otherwise error(Unknown or improper image class.); end

MATLABTiming
tic;%setsvariabletictocurrentclocktime runMATLABcodetobetimed toc; Elapsedtimeis0.xxxseconds fortimingfunctioncallscanusespecial functions=timeit(f)wherefisthefunction handleforthefunctiontobetimed,andsis themeasurementtime,inseconds(timeit mustberetrievedfromcoursewebsite)

Vectorizing Loops Example1


f(x) = Asin(x / 2), x = 0,1,2,...,M -1 For loop implementation : A = 10; for x = 1: M % Array indices in MATLAB cannot be 0 f(x) = A * sin((x -1) / (2 * pi)); end Vectorized MATLAB code : A = 10; tpi = 2 * pi; x = 0 : M -1; f = A * sin(x / tpi);
Ratio of time for loop versus vectorized MATLAB code: 1.8

Vectorizing Loops Example2


Create MATLAB function to create a synthetic image of the form : f ( x, y ) = A sin(u0 x + v0 y ) First version uses nested "for" loops function f = twodsin1(A, u0, v0, M, N) f = zeros(M, N); % pre - allocation of memory for c = 1: N v0y = v0 * (c -1); for r = 1: M u0x = u0 * (r - 1); f(r, c) = A * sin(u0x + v0y); end end Time this version : f = twodsin1(1, 1/ (4 * pi), 1/ (4 * pi), 512, 512); imshow(f, [ ]);

Time: 0.0215 seconds

Vectorizing Loops Example2


Vectorize function using meshgrid i [C, R] = meshgrid(c, r); i c, r vectors of horizontal (column) and vertical (row) coordinates i meshgrid transforms coordinate vectors into arrays C and R that can be used to compute function of two variables Example : want to evaluate function z = x + y for integer values of x ranging from 1 to 3, and for integer values of y ranging from 10 to 14 [X, Y] = meshgrid(1: 3, 10 :14); X = [1 2 3; 1 2 3; 1 2 3; 1 2 3; 1 2 3]; Y = [10 10 10; 11 11 11; 12 12 12; 13 13 13; 14 14 14]; Z = X + Y = [11 12 13; 12 13 14; 13 14 15; 14 15 16; 15 16 17];

Vectorizing Loops Example2


Rewrite 2 - D sine function without loops function f = twodsin2(A, u0, v0, M, N) r = 0 : M - 1; % row coordinates c = 0 : N - 1; % column coordinates [C, R] = meshgrid(c, r); f = A * sin(u0 * R + v0 * C);
Time: 0.0157 seconds Ratio of times: 1.369

InteractiveIO
disp(argument) displayonscreen A=[12;34];disp(A); 12 34 sc=textstring;disp(sc); textstring t=input(message:,s) acceptscreeninput n=input(specifyvalueforn:); t=input(specifyvalueforn:,s);n=str2num(t); [a,b,c,]=strread(cstr,format,param,value); format=%f(floatingpoint),%q(characterstrings),%d(integer) t=12.6,x2y,z;[a,b,c]=strread(t,%f%q%q,delimiter,,); a=12.6;b=x2y,c=z(bandcarecells);convertcellstocharacterarray d=char(b)=x2y,e=char(c)=z

StringCompare
Weassumethatparameterparamcanhavetwoforms,namelynorm1([01])or norm255([0255]) Usestrcmptodecidewhichoptiontouse fisimageinput f=double(f);%floatingpointvalues f=fmin(f(:));%min(f)setto0 f=f./max(f(:));%max(f)setto1 if(strcmp(param,norm1) g=f; elseifstrcmp(param,norm255) g=255*f; else error(Unknownvalueofparam.); end

CellArraysandStructures
Cellarray multidimensionalarraywhoseelementsare copiesofotherarrays signaledby{and}brackets c={gauss,[10;01],3}; c{1}=gauss(acharacterstring) c{2}=[10;01](a2x2matrix) c{3}=3(ascalar) Structure groupingofacollectionofdissimilardataintoa singlevariable elementsofstructuresaddressedbyfields S=structure enterelementsofSviasimplecommands,e.g., S.char_string =gauss; S.matrix =[10;01]; S.scalar =3;

MATLABImageProcessing Commands
f=imread(filename);%readsinfiletoarray imshow(f);%displaysarrayasimage,[0255] imshow(f,[lowhigh]);%f(i,j)low=>low(black) %f(i,j)high=>high(white) imshow(f,[]);%low=min(min(f));high=max(max(f)); pixval;%displaysintensityvaluesofpixels imwrite(f,filename);%writesimagetodisk imwrite(f,filename,tif); imwrite(f,filename.jpg,quality,q);%qin[0100]range

MATLABImageProcessing Commands
K=imfinfo filename;%getfileinformationinstructureK image_bytes =K.Width*K.Heights*K.Depth/8; g=im2uint8(f);%convertfromimagetouint8 f0=>0;f1=>1;scaleby255andround g=mat2gray(A,[Amin Amax]);%convertsdouble[]to double[01] g=im2double(f);%convertsimageofuint8ordoubleto double g=im2double(mat2gray(f));%convertsto[01]range

ImagesonWebsite
blood1.tif; 265 rows, 272 columns, min=46, max=255 breast.tif; 570 rows, 482 columns, min=21, max=255 building.tif; 240 rows, 320 columns, min=21, max=255 chalk.tif; 1040 rows, 1040 columns, min=0, max=255 checker.tif; 512 rows, 512 columns, min=0, max=255 circuitboard.tif; 450 rows, 450 columns, min=0, max=255 FTspectrum.tif; 257 rows, 257 columns, min=0, max=255 hardware.tif; 240 rows, 320 columns, min=4, max=248

ImagesonWebsite
iris.tif; 600 rows, 600 columns, min=0, max=255 iris_gray.tif; 600 rows, 600 columns, min=27, max=255 iris_luminence.tif; 600 rows, 600 columns, min=22, max=255 lena.gif; 512 rows, 512 columns, min=19, max=191 lena.tif; 512 rows, 512 columns, min=24, max=245 lighthou.tif; 240 rows, 320 columns, min=21, max=255 moon.tif; 540 rows, 466 columns, min=0, max=254 parrots.tif; 256 rows, 384 columns, min=0, max=254

ImagesonWebsite
peppers.jpg; 512 rows, 512 columns, min=0, max=245 rose.tif; 1024 rows, 1024 columns, min=0, max=255 saturn.tif; 256 rows, 256 columns, min=0, max=255 shuttle.tif; 240 rows, 320 columns, min=32, max=255 tooth1.jpg; 512 rows, 512 columns, min=0, max=229 xray.tif; 240 rows, 320 columns, min=42, max=255