Lecture IP1 2012
Lecture IP1 2012
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
64 x 64 pixels
All images 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
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
ImageCompression
8 bit
0.5 bit
0.33 bit
0.25 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
ImageDenoisingMedianFiltering
% 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
camerman.tifimage filtered
camerman.tifimage+noiseand filtered
camerman.tifimage+noiseand medianfiltered
ImageDenoisingAdaptiveFiltering
(1) (2) (3)
(2) Gray Scale Saturn Image (4) Noise Removed Image Using Wiener Filter
(4)
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
GaussianFilteringofImages
GaussianFilteringofNoisyImages
GaussianFilteringofNoisyImages
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');
ImageSampling RateChanges
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
ErrorConcealment
ErrorConcealment
UnderSamplingofImage
Propersamplingofimage
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) ...
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
imshow(f,[64,128]);
Example
Example
f=[255230180;155130105;80500]; imshow(f,[],InitialMagnification,fit);
ImageTool
Cropped Image
f=imread(rose.tif); imtool(f);
ImageTool
ImageTool
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
ImageDataClasses
f=imread(lena.tif); whos
NameSizeBytesClassAttributes f512x512262144uint8
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.
ImageHalftoning
ImageCropandBlowUp
HalfToneImages ReducedinSize
OtherPatterns
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
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)
g=mat2gray(f)
Image Processing
p=im2uint8(h)
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
ImageConversions im2uint8
im2uint8scalesanimageintherange[01]toan imageintherange[0255]byscalingtheimage valuesby255andclippingbelow0andabove255
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
SimpleExample
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]
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
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
CountingGrainsofRice
rawimageshowsgrainsofrice
theimagehasnonuniformillumination;the backgroundisbrighterinthecenteroftheimage thanatthebottom goalistoenhancetheimagetocorrectforthe nonuniformillumination usetheenhancedimagetoidentifyindividual grains computethegrainstatistics
% 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
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
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
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
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; 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)
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