15
CS103L PA3 – It's So Belurry 1 Introduction In this assignment you will design and implement a program to perform simple kernel based image processing filters on an image. We will be implementing three filters: 1) the Sobel operator for edge detection (using a fixed sized kernel), 2) the Gaussian blur filter for low-pass filtering, and 3) unsharp mask filter for sharpening an image (which uses the Gaussian blur filter). The kernel-based implementation you will design forms the basis for a large-class of image processing techniques, therefore through this programming assignment you will learn to implement an algorithm with wide applicability across computer science. Tommy Trojan filtered with a Gaussian blur filter Black and White tiles filtered with the Sobel Kernel USC v. UCLA filtered with the unsharp mask operation

CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

CS103LPA3–It'sSoBelurry

1 IntroductionInthisassignmentyouwilldesignandimplementaprogramtoperformsimplekernelbasedimageprocessingfiltersonanimage.Wewillbeimplementingthreefilters:1)theSobeloperatorforedgedetection(usingafixedsizedkernel),2)theGaussianblurfilterforlow-passfiltering,and3)unsharpmaskfilterforsharpeninganimage(whichusestheGaussianblurfilter).Thekernel-basedimplementationyouwilldesignformsthebasisforalarge-classofimageprocessingtechniques,thereforethroughthisprogrammingassignmentyouwilllearntoimplementanalgorithmwithwideapplicabilityacrosscomputerscience.

TommyTrojanfilteredwithaGaussianblurfilter

BlackandWhitetilesfilteredwiththeSobelKernel

USCv.UCLAfilteredwiththeunsharpmaskoperation

Page 2: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

2 WhatyouwilllearnThisassignmentwillexposeyoutosimpleimagerepresentationandmanipulationtechniquesaswellasfamiliarizeyouwithC/C++operationson2Darrays.

1. Understandmultiplefilecompilationunitsandtheirlinkage.2. Usecommandlineargumentstoprovideinputtotheprogramvs.interactiveuser

input3. UnderstandimagerepresentationasacollectionofpixelsandunderstandtheRGB

colorspace4. Applyknowledgeofarrays(includingmulti-dimensionalarrays)toimplementan

image-processingapplication5. Create,develop,andevaluateyourownprocessingapproachtoperformthe

operation

3 ColorImagesas3DArraysinC/C++Imageprocessingisamajorsubfieldofcomputerscienceandelectricalengineeringandtoalesserextentbiomedicalengineering.Graphicsareusuallyrepresentedviatwomethods:vectororbitmap.Vectorgraphicstakeamoreabstractapproachandusemathematicalequationstorepresentlines,curves,polygonsandtheirfillcolor.Whenaprogramopensthevectorimageithastotranslatethoseequationsandrendertheimage.Thisvectorapproachisoftenusedtorepresentclipartand3Danimations.Bitmapimagestaketheoppositeapproachandsimplyrepresenttheimageasa2Darrayofpixels.Eachpixelisasmalldotorsquareofcolor.Thebitmapapproachisusedmostcommonlyforpictures,video,andotherimages.Inaddition,bitmapsdonotforceustotranslatethevectorequations,andthusaresimplertomanipulateforourpurposes.Colorbitmapscanuseoneofseveraldifferentcolorrepresentations.ThesimplestofthesemethodsisprobablytheRGBcolorspace(HSL,HSV,&CMYKareothers),whereeachpixelhasseparatered,green,andbluecomponentsthatarecombinedtoproducethedesiredcolor.Usuallyeachcomponentisan8-bit(unsigned charinC)value.Given3-valuesthisyieldsatotalof24-bitsforrepresentingaspecificcolor(knownas24-bitcolor=224=16millionuniquecolors).Storingared,greenandbluevalueforeachpixelcanbeachievedusing3separate2Darrays(oneforeachRGBcomponent)orcanbecombinedintoa3Darraywithdimensions[256][256][3]asshownbelow.[Note:BMPimagefilesuseasimpleformatlikethisoneandthuswillbethefileformatusedinourlab.]

Page 3: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

(0,0) (0,255)

(255,255)(255,0)

(0,1)

(1,0) (1,1)

R = Image[i][j][0]G = Image[i][j][1]

B = Image[i][j][2]

RG

B

Red Green Blue

Figure1–Representationofacolorbitmapasa3Darray

4 KernelBasedImageProcessingAlgorithmsSimilartohowimagesarerepresentedinacomputer,themostcommonapproachtoimageprocessingtechniquesalsousesamatrixrepresentation.Kernelbasedimageprocessingalgorithmsdefineasmallmatrix(a.k.a.thekernel)offloating-pointnumbers,whichisthenconvolvedwiththesourceimage.Convolutionisacommonmathematicaltechnique,andinthecaseofimagesisnotdifficulttoimplement.Todotheconvolutionthecenterofthekernelisalignedwitheachpixelinthesourceimage.Ateachpixelthefloating-pointnumbersinthekernelrepresenthowmuchofeachsourceimagepixelcontributetothepixelintheoutputimage.Thefigurebelowshowsanexamplefora3x3kernelknownasthe“cross”kernel.Foreach(i,j)pixelintheinputimagewecalculateanoutputpixelusingthe3x3kernel.Youcanthinkofstartingwiththecenterofthekerneloverpixel(0,0)andslidingitovertherestoftheimageonepixelatatime.

Color RGBValueWhite 255,255,255Red 255,0,0Yellow 255,255,0Orange 255,128,0Blue 0,0,255Green 0,255,0Purple 255,0,255Black 0,0,0

Page 4: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

Basedonthisexampleforagrayscaleimage(oronecolorplaneofacolorimage)wecanwriteanequationthatrepresentsthepixelvalueweneedtocalculateatposition(x,y)givena256x256inputimage(In),aNxNsizedkernel(K)anda256x256outputimage(Out):

𝑂𝑢𝑡 𝑦 [𝑥] = 𝐼𝑛 𝑦 + 𝑖 𝑥 + 𝑗 ∗ 𝐾[𝑁2 + 𝑖][

𝑁2 + 𝑗]

34

56734

34

86734

Oneissueisthatouroutputpixelvalueduetothisoperationsmayproducearesultoutsideoftherange0-255thatunsignedchar’scansupport.Thusweshouldstoreourresultinaninteger,thencheckifitliesoutsideoftherange0-255.Ifitislessthan0,justsetitto0.Ifit

Page 5: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

isgreaterthan255,justsetitto255.Thisisknownasclamping.Asaruleofthumbyoushouldstoretheresultsofyourconvolutionastemporaryinttypesandthenperformclampingtoproducetheactualpixelvalueyouplaceintheoutputarray.Anotherissuewiththeequationaboveisthatitonlyholdsforpixelsawayfromtheedgeoftheimage.Whenx<N/2ory<N/2,theindexeswillhavenegativevalues.Similarly,whenx>255–(N/2)ory>255–(N/2)theindexeswillhavevaluesgreaterthan255.Thesearewhatarecallededgecasesandcanbehandledinseveralways.Inthisprogrammingassignmentyouwillcopytheinputimageintoanarraythatisslightlybiggerthantheoriginalimage.Theextrapixelswillbefilledwithavalueandthentheconvolutionisstartedattheoffsetoriginalimage.Thistechniqueiscalledpaddingandisstraightforwardtoimplement.Thenextfigureillustratesthistechnique.Weseeouroriginalimagesurroundedby1pixelofpadding.Thiswillallowustousetheequationabove(withcarefulselectionofthestartingandendingvaluesofiandj).Whatgoesinthepaddingisuptotheprogrammerhowever,forthislabwewillpadwithzeros(i.e.blackpixels).YouwillneedtopadwithN/2rows/columnstosupportanNxNkernel.

Padwith0’saroundtheborder.Thiswouldworkfora3x3kernel.Howmuchpaddingwouldbeneededforan11x11kernel?

Page 6: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

5 TheSobelOperatorForourfirstimageprocessingfilterwewillimplementafilterdefinedbyafixedkernelsize.TheSobelimplementationwewilldohastwokernels,horizontal1andhorizontal2:

-1 0 1

-2 0 2

-1 0 1

Thissimplekernelwasdevelopedtodetect(orhighlight)edgesinanimage.Thekernelscanbeusedaloneorincombination.Tousethemincombinationwecansimplyapplyeachonetotheinputandproduce2separatearrays/images.Thenaddtheseparatearrays/imagestogether(clampingat0and255)toproducethefinaloutput.

6 The2DGaussianFilter

The2DGaussiandistributionisawell-knownfunctionoftwovariablesthatfindsapplicationinprobabilityandstatistics,chemistry,quantummechanicsandofcourseimageprocessing.Ithasthefollowingform:

𝑔 𝑥, 𝑦 = 𝐴 ∗ 𝑒7 (>7>?)A

4BCAD(E7E?)

A

4BFA Where(x0,y0)isthecenterposition,Aistheamplitude,σisthevariance.Thefigureaboveshowsthree2DGaussianswithseveraldifferentvaluesofσ.

1 0 -1

2 0 -2

1 0 -1

Horizontal2SobelKernelHorizontal1SobelKernel

Page 7: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

Forimageprocessingasablurfilter,thewiderthecentralpeak,thegreatertheblurringeffect.OurtaskthenistotaketheequationaboveandgeneratetheNxNkernelneededtofilteranimage.TodosowesetA=1,andsetthecenterpointofthekernelasx0=0,y0=0.Thevariance,σ,isaparameterthatcanbeusedtoadjustthesizeofthecentralpeak.Thex,yvaluesoftheotherkernelcellsaresetasoffsetsfromthecenter.Forexample,a3x3kernelisshownbelow.Thenforeachcell,theGaussianequationisevaluatedandthefloatingpointvalueassigned.Finally,wemustnormalizethevaluessothatthesumofthevaluesisequalto1.Thisisdonesothatthebrightnessoftheimagedoesnotchange.Thetwotablesbelowshowtherawandnormalizedvaluesforthe3x3Gaussianblurkernel(N=3,sigma=1.5).

OneissuetobeawareofwhenyouproducetheGaussiankernelisindexing.TocorrectlyapplytheGaussionequation(0,0)shouldbethecenterofthekernel.However,forarrayindexing0,0isalwaystheupperleft.ThinkabouthowyoucanconvertthearrayindexingtogeneratetheappropriateGuassianindices.

7 TheUnsharp-maskfilterSomeimageprocessingfilterscanbequicklyconstructedfromadding,subtractingandscalingtheresultsofotherfilters.Theunsharp-maskfilterisonesuchfilter.Sharpeninganimageistheoppositeofblurringandimage.Sharpeninganimageattemptstoenhancedetail.Ifyousubtractablurredversionofanimagefromtheoriginalimage,intuitivelytheresultingimagewillhavethemostdetailremaininginareasthathadalotof

Raw3x3Gaussian Normalized3x3Gaussian

0.0947 .1183 .0947

0.1183 .1478 0.1183

0.0947 0.1183 0.0947

0.6412 0.8007 0.6412

0.8007 1 0.8007

0.6412 0.8007 0.6412

Gaussian3x3row/columnindexingperspective C++2Darrayindexingperspective

0,0 0,1 0,2

1,0 1,1 1,2

2,0 2,1 2,2

-1,-1 -1,0 -1,1

0,-1 0,0 0,1

1,-1 1,0 1,1

Page 8: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

detailintheoriginalimage.Thinkingaboutitanotherway,inareasoftheoriginalimagewithhigh-detailtheblurfilterwill‘domore’thaninareaswithlowdetail.Ifwetakethis‘detail’mapandadditbacktotheoriginalimage,wewillenhanceareaswithalotofdetail,ineffectsharpeningtheimage.IfwehaveanoriginalimageIMwecancreateablurredversionBbyapplyingtheGaussianblurfunctionblur():

𝐵 = 𝑏𝑙𝑢𝑟(𝐼𝑀)WecanthencreateadetailmapDbysubtractingBfromIM.Note:Theequationbelowisamatrixoperationandimpliesweperformthesubtractiononeverycorrespondingpixelintheimage(i.e.D[0][0]=IM[0][0]–B[0][0],D[0][1]=IM[0][1]–B[0][1],etc.)

𝐷 = 𝐼𝑀 − 𝐵Finally,wecancreatetheenhanced,orsharpenedimageSbyaddingsomefractionofDbacktoIM(againthisisamatrixoperationwhereweperformthefollowingoneachpixel):

𝑆 = 𝐼𝑀 + 𝛼𝐷Whereαtypicallyrangesfrom0.3-0.7

8 DummyKernelThefollowingkernelwillsimplycopyaninputimagetoanoutputimage.Itmaybeusefulfortestingpurposes.

9 C/C++LanguageDetailsThissectioncontainshelpfulbackgroundinformation.YoushouldreadthroughittounderstandvariousaspectsoftheC++language.Itismeantjustasareviewandoverviewofrelatedconcepts.SomefeaturesofCthatwewillutilizeinclude:

0.0 0.0 0.0

0.0 1.0 0.0

0.0 0.0 0.0

Page 9: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

2-and3-DArrays:Wewillstorethemaskandimagesin2-and3-Darrays,respectively.Recallthedeclarationandaccesssyntax:int example2d[10][5]; unsigned char array3d[256][256][3]; x = example2d[9][4]; // accesses element in the bottom row, last column pixel_red = array3d[255][255][0]; // access lower, right pixel’s red value

NotethateachoftheRED,GREEN,andBLUEcomponentsofapixelisan8-bitvalue,logicallybetween0and255.Thuswewillusetheunsigned chartypetorepresentthesenumbers.Also,thelibraryfunctionsthatwewillprovideusetheconventionthatthefirstindexistherowandthesecondindexisthecolumn(withthethirdindexbeingtheR,G,orBvalue.)Mathfunctions:Youwillneedtocomputeexponentsandalsousetheconstante.C++providesanexponentialfunctioninthe<cmath>library.Thefunctioncalculatesthevalueofexforsomedoublex.Thefunctionisprototypedas:

double exp(double); BMPLibrary&FileI/O:Forthislab,wewillprovideyoupredefinedfunctionsthatyoucanusetoreadinandwriteout.bmpimagefiles.Thesefunctionsaregiveninthecompiledlibrarybmplib.oandareprototypedintheheaderfilebmplib.h.Besuretoincludebmplib.hinyourprogrambyaddingthefollowinglinewiththeother#includestatements.#include "bmplib.h"

Thefunctionsyouwilluseare“readRGBBMP“and“writeRGBBMP“.Prototypesareshownbelow.Youmustpasseachfunctionacharacterarray(textstring)ofthefilenameyouwishtoread/writeanda256x256x3arrayofunsignedchar’s(8-bitvalues)thatrepresentthedatatoberead/written.int readRGBBMP(char filename[], unsigned char inputImage[][256][3]); int writeRGBBMP(char filename[], unsigned char outputImage[][256][3]);

Note:Thesefunctionsreturn0ifsuccessfulandnon-zeroiftheycannotopen,read,orwritetheparticularfile.Fordebuggingpurposes,youmayalsousethefunctionvoid showRGBBMP(unsigned char outputImage[][256][3]);

thatdisplaysanimage.However,itmaynotworkifyouarenotusingtheVM.

Page 10: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

CommandLineArguments:Ratherthanpromptingtheuserduringyourprogramtoenterthefilesyouwillprocess,wewillpassthefilenamestoyourprogramascommandlinearguments.Commandlineargumentsprovideawaytopassaprogramsomeinitialinputvalueswithouthavingtoprompttheuserexplicitlywhenyourprogramexecutes.Mostprogramsprovidethiskindoffeature.E.g.inWindowsfromtheStart..Runbox,type“notepadmydoc.txt”.Thiswillstartnotepadandattempttoopenafilenamedmydoc.txtwithoutrequiringyoutousetheGUIinterface.YourOSprovidesthisabilitybyparsingthecommandlinewhenyoustartyourprogramandpassingtheadditionalcommandlinewordsasarguments:int argcandchar *argv[]tothemain()routine. int main(int argc, char *argv[]) { ... }

Theargcvalueisanintegerindicatinghowmanycommandlineargumentswereentered(notethattheexecutableprogramnameisincludedinthecount,soargcwillalwaysbeatleastone.)Theargvargumentisanarrayofcharacterstrings.Forexample,ifwerun:

$ ./filter input.bmp blur 3 1.5 output.bmp

Thenargc is 6 argv[0] is "./filter" argv[1] is "input.bmp" argv[2] is "blur" argv[3] is "3" argv[4] is "1.5" argv[5] is "output.bmp"

Note:Numericargumentssuchas“1.5”arepassedinascharacter(text)stringsandneedtobeconvertedtotheappropriatenumerictypesbeforeoperatedupon.Thiscanbeaccomplishedwithfunctionslike“atoi”(ASCIItoInteger)or“atof”(ASCIItofloatingpoint)whicharedefinedin<cstdlib>andwhoseprototypesareshownbelow. //returnstheintegervalueofthenumberrepresentedbythecharacterstring“string”int atoi(char *string); //returnsthedoublefloatingpointvalueofthenumberrepresentedbythecharacterstring“string”double atof(char *string);

Asanexample,the“1.5”argumentcanbeconvertedby:double x; x = atof(argv[3]);

Multi-filecompilation:Mostreal-worldprogramsaremadeupofmorethanonesourcecodefileandthusrequirethecompilertogeneratecodeandthenlinkseveralfilestogethertoproduceanexecutable.Inthislab,wehaveprovidedbmplib.handbmplib.o.bmplib.oisan“object”file(.oextension)representingthecompiled(but

Page 11: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

notlinked)functionstoperform.BMPimageI/O.Itisnotatextfilebutbinaryinstructionsandmemoryinitializationcommands.AnobjectfilecanbecreatedfromaC++filebyusingthe-cextensiontothecompiler.

$ compile -g –Wall –c bmplib.cpp

Thiscommandwillcreatethebmplib.o.bmplib.hisaheaderfilethatincludesprototypesandotherdeclarationsthatyoucanincludeintoyourCcodethatwillallowyoutocallthefunctionsinbmplib.o.TocompileyourcodewiththeBMPfunctionsandthenlinkthemtogetheryoucouldrun:

$ compile -g –Wall –o filter bmplib.o filter.cpp

Youcanlistanynumberof.ofilesandCfilesonthecommandline.TheCfileswillbecompiledandthenlinkedtogetherwithallofthe.ofilesspecifiedproducinganexecutableasoutput.‘make’andMakefiles:Asmorefilesbecomepartofyourprogram,youwillnotwanttocompileEVERYfileagainwhenyousimplymakeachangetoonefile.However,keepingtrackofwhichfileshavechangedandthusrequirerecompilationcanalsobecomedifficult.Enterthe‘make’utility.Thisprogramtakesasinputatextfileusuallynamed‘Makefile’whichincludescommandsthatidentifytheorderdependenciesoffilesoneachother(i.e.iffile1changes,thenitmayrequirere-compilingfile2andfile3)andthecommandstoperformthecompilation. Typing‘make’atthecommandlinewillcompileallnecessaryfilesandproducetheoutputexecutable:filter.Needtorecompileyourcode?Justtype’make’againanditwillonlycompilewhatyou’vechanged.Tostartfreshandremoveanytemporaryfiles,type‘make clean’followedby‘make’.Summary:Youdonothavetotypeinany‘compile…’commands.Justuse‘make’.Moreinformationaboutthe‘make’utilityandMakefilescanbefoundat:

http://www.eng.hawaii.edu/Tutor/Make/http://frank.mtsu.edu/~csdept/FacilitiesAndResources/make.htm

Page 12: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

10 PrelabTheexercisesbelowwillhelpyouformalizesomeoftheconceptsdiscussedabovebeforeyoustartprogramming.

1. Paddingdesign:IfwerestrictthesizeoftheGaussiankernelstooddintegersbetween3and11inclusive,andweonlyallow256x256pixelimages,whatisthesizeofthelargestpaddedimageneededtohandlepaddingwithanykernelsize?Atwhatindexwilltheupper-leftpixeloftheoriginalimagebeplacedinthepaddedimage(answerintermsofN,thekernelsize)?Atwhatindexinthepaddedarraywillthelower-rightpixeloftheoriginalimagebeplaced?

2. KernelDesign:ManuallycomputetherawandnormalizedGaussiankernelsforN=3,sigma=2.Use4decimalplaces.Discusswhatwouldhappentotheimageifweusedtherawkernelvalues.

11 ImplementationInthissectionwewilldescribetheinputsandfunctionsyourprogrammustimplementataminimumtogetfullcreditonthisprogrammingassignment.ForthisassignmentallBMPimageswillbeexactly256x256pixels.Inaddition,thelargestkernelusedinthisassignmentis11x11.SinceC/C++doesnotlikearraydeclarationswherethesizeisavariable,youcandeclareallofyourkernelswithfixed11x11dimensions.Atruntime,youwillgetNfromthecommandline,soyouwilljustfillintheupper-leftNxNelementsforyourkernels(tocreatetheSobel,Gaussian,andunsharpfiltervalues).Then,youwillalwayspassan11x11kerneltoyourfunctionsbutthosefunctionswillonlyusetheupper-leftNxNelements.

1. Downloadtheskeletonfileandtestimagesbyrunningthefollowingcommands:a. wgethttp://bits.usc.edu/files/cs103/imfilter/filter.tarb. tarxvffilter.tar

Recallyoucanviewimagesusingthe‘eog’programasin:eog tommy.bmp

2. Ouroverallprogramwillbecalledfilterandimplementthethreeimageprocessingfiltersdescribedabove,allowingtheusertochooseany1filterperrunoftheprogram.Yourprogramwilltakecommandlineargumentstocontrolwhichfilterisappliedtotheinputimage:

a. ./filter <input file name> dummy <output file name> b. ./filter <input file name> sobel <output file name> c. ./filter <input file name> blur <kernel size N> <sigma> <output file name> d. ./filter <input file name> unsharp <kernel size N> <sigma> <alpha>

<output file name><input file name>willbeastringlike‘tommy.bmp’

Page 13: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

The2ndargumentwillbethefiltertoapply:dummy,sobel,blurorunsharp<kernel size N>isthesizeofthekerneltocreatefortheGaussian(min=3,max=11)<sigma>isthevarianceparameterintheGaussian(mustbenon-zero)<alpha>isthemix-inparameterintheunsharpequation(0<alpha<=1.0)

3. Wehavecompletedthemainfunctionforyou.Butyoushouldreadandunderstandwhatitisdoing.Inparticular,mainperformsthefollowingtasks:

a. Checksthataminimumamountofargumentsareprovided.b. Attemptstoopentheinputfileandreadintheinputimagedatatoanarray:

unsigned char input[SIZE][SIZE][3].Ifthefileisunabletobeopened,theprogramwillquit.

c. Checksthe2ndparameterisoneofdummy,sobel,blurorunsharp. Basedonthisargumentwedeterminewhatothercommandlinesargumentsyoushouldexpectandcheck.

d. Wethenconverttheappropriatecommandlineargumentstotheappropriatedatatypeandinvokefunctionstocarryoutthespecifictask.

4. Asyoucreatetheoutputimage,placeitinanarrayunsigned char output[SIZE][SIZE][3]. main willwritethatarraytotheoutputfilename.

5. Thedummyfunctioniscomplete.Youdonotneedtomodifyit.Itshowsyouanexampleofhowtodeclareakernelandprepareitforusewithconvolve.Itthencallsconvolvetogeneratetheoutputimage.

6. Completethefunctionconvolve(unsigned char out[][SIZE][3], unsigned char in[][SIZE][3] , int N, double kernel[][11]) thattakesanoutputarray,aninputarrayandagenerickernelofwhichonlyNxNisusedoutofthe11x11totalsize.Thisfunctionshouldperformtheconvolutionoperationofthekernelwiththeimage.Insidethisfunctioniswheretheimagewillneedtobepadded.Clampingcanalsobeperformed.

7. Completethefunction:void sobel(unsigned char out[][SIZE], unsigned char in[][SIZE]). Itisstartedforyou.

a. Youshouldconvolveeachofthehorizontaldirectionkernelsoneatatimeproducingtworesultingarrays.

b. Thenaddthetworesultstogether(pixel-by-pixel)toproducethefinaloutputimage.8. Implementfromscratchafunctiongaussian(k[][11], int N, double sigma)that

fillstheupperNxNelementsofthe11x11kernelwiththenormalizedGaussian.Thisfunctiononlygeneratesthekerneltobeused.ItdoesNOTactuallyperformthefiltering/convolution.Thisfunctionshouldprintthekerneltothescreeninanice2Dtableformatonthescreen.Thiswillhelpyouensureyou’vecomputedthingscorrectly.

9. Implementfromscratchthegaussian_filterfunction.Todosousethegaussian()[toproducethekernel]andconvolve()functionsfromthegivencommandlineparameters.gaussian_filtershouldbeafunctionwiththeprototypegaussian_filter(output [][SIZE][3], input[][SIZE][3], int N, double sigma).Nandsigmahavethesamemeaningasforthegaussian()function.

Page 14: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

10. Implementfromscratchtheunsharpmaskfilterusingthegaussian()andconvolve()functions,alongwiththecommandlineparameters.Thisshouldbeafunctionwiththeprototypeunsharp(output[][SIZE][3], input[][SIZE][3], int N, double sigma).

11. Compileyourprogramfixinganycompiletimeerrors.12. Runyourprogramonvariousinputs.Herearesomesamplecommandlines:

a. ./filter tommy.bmp blur 5 2.0 tommy_blur.bmpb. ./filter bw_tile_wikimedia.bmp sobel bw_tile_sobel.bmpc. ./filter usc_ucla_wikimedia.bmp unsharp 5 2.0 0.7 usc_ucla_unsharp.bmp

12 ImplementationHintsTheseareafewhintstohelpyougetstarted:

• Donottrytowritethewholeprogramstart-to-finish.Writetheprograminstages.Forexample,youcaninitiallywriteyourconvolve()functionsoitdoesn’tactuallydoanythingbesidescopyinputtooutput.ThenyoucangetyourprogramworkingsoitopensaBMP,callsconvolve()andthenwritestheoutputfile.ThiswillgetyoupracticepassingBMPsasarraysaswellashandlingthecommandlinearguments.Oncethisisworking,moveontoactuallyimplementingtheotherfunctions.

• Pixelmath:pixelsinourimagesarerepresentedbyunsignedcharwithvaluesfrom0-255.Whenimplementingthefiltersyoumayneedtocastthepixelvaluestodoublesbeforeperformingthemathematicaloperations.Besuretochecktheresultingpixelvalueslieintherange0-255beforecastingbacktounsignedchar.Ifapixelvalueisnegative,setitto0andifitisabove255setitto255.Thisstepisknownasclamping.

• Makesureyourcodeiscommentedwell.Thiswillhelpnotonlyyou,butthegraders.

13 ExperimentationDothefollowingexperimentsandcommentontheresultsinyourreadme.txt

• FilterthesameimagewiththeGaussianblurfilterwhilevaryingNandsigma.IfyouholdNconstantandvarysigma,whatdoyousee?Conversely,ifyouvaryNandholdsigmathesame,whatdoyousee?

• FilterafewimageswiththeSobelfilter?WhatdoestheSobelfilterappearto‘do’?• TheGaussianblurfilterandtheunsharpmaskfiltercanbethoughtofasinversesof

eachother.BluranimagewiththeGaussianblurandthenattempttoun-bluritusingunsharp-mask.Doyougettheoriginalimageback?Providea2-3sentenceexplanationforwhyyoudonotrecovertheoriginal(i.e.theyarenotinverseoperations).

Page 15: CS103L PA3 – It's So Belurry - USC Bitsbits.usc.edu/files/cs103/imfilter/pa-filter.pdf · In this assignment you will design and implement a program to perform simple kernel based

14 TroubleshootingBeforeyoupostaquestionorseekhelpmakesureyou:1. Canviewtheimagesyoudownloaded.Ifyouhaveproblemswitheogmakesureyou

areintherightdirectorywhereyourimagesarelocated.

2. Tryprintingoutsomepixelvaluestoensureyouarereadingintheinputimagescorrectly.Justbewarnedifyouwanttoprintoutthenumericvaluesofpixels,you'llneedtocasttoanintegerfirst,asin:

cout << (int)inputImage[0][0][0] << endl;

Thisisbecauseotherwise,theimagearraysareunsignedcharswhichwillprintoutasactualASCIIcharacters.

3. Tryusingadebuggertorunthroughandexamineyourvalues.Whenyouwantto

debugaprogramthatneedscommandlinearguments(likeourswhichrequiresyoutopassitthenameoftheinputfiles,thethreshold,etc.)whenyoustarttheprogram,asin:

$ ./filter tommy.bmp sobel output.bmp

Youstartgdbnormallywithjusttheprogramname:

$ gdb filter

Setanybreakpointsyoudesire.Thenwhengdbstartsup,afterrun,youalsotypeinthecommandlineargument:

run tommy.bmp sobel output.bmp

15 Readme.txtInadditiontoyournameande-mailyour"readme.txt"fileshallincludetheanswerstotheprelabquestionsandthequestionsfoundunder“Experimentation.”Alsoanswerthefollowing:

• Expressinmathematicaltermshowthenumberofcalculationsyourprogramdoesgrowswiththesize,N,ofthekernel.

16 SubmissionSubmityour'filter.cpp'and'readme.txt'filestotheclasswebsite.