Introduction to Vision Systems in LabVIEW PAnS-L11L8 PALvC8SLn, 2013.02.19
1e|emark Un|vers|ty Co||ege ueparLmenL of LlecLrlcal Lnglneerlng, lnformaLlon 1echnology and CyberneLlcs Faculty of Technology, Postboks 203, Kjlnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01
Preface ln Lhls LuLorlal we wlll lnLroduce Lhe concepLs of Mach|ne V|s|on and how we can lmplemenL such appllcaLlons ln LabvlLW. lor more lnformaLlon and addlLlonal resources: http:]]home.h|t.no]~hansha]?tutor|a|=v|s|on
lor more lnformaLlon abouL LabvlLW, vlslL my 8log: http:]]home.h|t.no]~hansha].
?ou need Lhe followlng sofLware: LabVILW NI V|s|on Acqu|s|t|on Software NI V|s|on Deve|opment Modu|e (nl vlslon 8ullder for AuLomaLed lnspecLlon) 1hese sofLware packages wlll be explalned ln deLalled laLer. ln Lhe examples we wlll use a ClgL vlslon camera (8asler scA640-70gc), l.e. Lhe camera ls connecLed Lo Lhe compuLer uslng a sLandard LLherneL cable.
lv
Table of Contents reface .................................................................................................................................................... 3 1able of ConLenLs ................................................................................................................................... lv 1 lnLroducLlon Lo LabvlLW .................................................................................................................. 1 1.1 uaLaflow rogrammlng ............................................................................................................. 1 1.2 Craphlcal rogrammlng ............................................................................................................ 1 2 lnLroducLlon Lo Machlne vlslon ........................................................................................................ 3 2.1 lnLroducLlon .............................................................................................................................. 3 2.2 lmage rocesslng and Analysls .................................................................................................. 3 2.2.1 1hresholdlng ...................................................................................................................... 3 2.2.2 aLLern recognlLlon and maLchlng ..................................................................................... 3 2.2.3 1exLure recognlLlon............................................................................................................ 3 2.2.4 8arcode readlng ................................................................................................................. 3 2.2.3 CC8 .................................................................................................................................... 4 2.2.6 Cauglng .............................................................................................................................. 4 2.2.7 oslLlon .............................................................................................................................. 3 2.2.8 Ldge ueLecLlon ................................................................................................................... 3 2.2.9 Color analysls ..................................................................................................................... 3 2.2.10 lllLerlng ............................................................................................................................ 3 2.2.11 CounLlng and ClasslflcaLlon .............................................................................................. 3 3 vlslon Cameras ................................................................................................................................. 6 3.1 ClgL (LLherneL) Cameras ........................................................................................................... 6 4 lnLroducLlon Lo vlslon SysLems ln LabvlLW ..................................................................................... 7 4.1 SofLware .................................................................................................................................... 7 v 1able of ConLenLs Tutorial: Introduction to Vision Systems in LabVIEW
1 Introduction to LabVIEW LabvlLW (shorL for LaboraLory VlrLual InsLrumenLaLlon Lnglneerlng Workbench) ls a plaLform and developmenL envlronmenL for a vlsual programmlng language from naLlonal lnsLrumenLs. 1he graphlcal language ls named "C". Crlglnally released for Lhe Apple MaclnLosh ln 1986, LabvlLW ls commonly used for daLa acqulslLlon, lnsLrumenL conLrol, and lndusLrlal auLomaLlon on a varleLy of plaLforms lncludlng MlcrosofL Wlndows, varlous flavors of Llnux, and Mac CS x. vlslL naLlonal lnsLrumenLs aL www.n|.com. 1he code flles have Lhe exLenslon .vl", whlch ls an abbrevlaLlon for vlrLual lnsLrumenL". LabvlLW offers loLs of addlLlonal Add-Cns and 1oolklLs. 1.1 Dataflow Programming 1he programmlng language used ln LabvlLW, also referred Lo as C, ls a daLaflow programmlng language. LxecuLlon ls deLermlned by Lhe sLrucLure of a graphlcal block dlagram (Lhe Lv-source code) on whlch Lhe programmer connecLs dlfferenL funcLlon-nodes by drawlng wlres. 1hese wlres propagaLe varlables and any node can execuLe as soon as all lLs lnpuL daLa become avallable. Slnce Lhls mlghL be Lhe case for mulLlple nodes slmulLaneously, C ls lnherenLly capable of parallel execuLlon. MulLl-processlng and mulLl-Lhreadlng hardware ls auLomaLlcally explolLed by Lhe bullL-ln scheduler, whlch mulLlplexes mulLlple CS Lhreads over Lhe nodes ready for execuLlon. 1.2 Graphical Programming LabvlLW Lles Lhe creaLlon of user lnLerfaces (called fronL panels) lnLo Lhe developmenL cycle. LabvlLW programs/subrouLlnes are called vlrLual lnsLrumenLs (vls). Lach vl has Lhree componenLs: a block dlagram, a fronL panel, and a connecLor panel. 1he lasL ls used Lo represenL Lhe vl ln Lhe block dlagrams of oLher, calllng vls. ConLrols and lndlcaLors on Lhe fronL panel allow an operaLor Lo lnpuL daLa lnLo or exLracL daLa from a runnlng vlrLual lnsLrumenL. Powever, Lhe fronL panel can also serve as a programmaLlc lnLerface. 1hus a vlrLual lnsLrumenL can elLher be run as a program, wlLh Lhe fronL panel servlng as a user lnLerface, or, when dropped as a node onLo Lhe block dlagram, Lhe fronL panel deflnes Lhe lnpuLs and ouLpuLs for Lhe glven node Lhrough Lhe connecLor pane. 1hls lmplles each vl can be easlly LesLed before belng embedded as a subrouLlne lnLo a larger program. 1he graphlcal approach also allows non-programmers Lo bulld programs slmply by dragglng and dropplng vlrLual represenLaLlons of lab equlpmenL wlLh whlch Lhey are already famlllar. 1he LabvlLW 2 lnLroducLlon Lo LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
programmlng envlronmenL, wlLh Lhe lncluded examples and Lhe documenLaLlon, makes lL slmple Lo creaLe small appllcaLlons. 1hls ls a beneflL on one slde, buL Lhere ls also a cerLaln danger of underesLlmaLlng Lhe experLlse needed for good quallLy C" programmlng. lor complex algorlLhms or large-scale code, lL ls lmporLanL LhaL Lhe programmer possess an exLenslve knowledge of Lhe speclal LabvlLW synLax and Lhe Lopology of lLs memory managemenL. 1he mosL advanced LabvlLW developmenL sysLems offer Lhe posslblllLy of bulldlng sLand-alone appllcaLlons. lurLhermore, lL ls posslble Lo creaLe dlsLrlbuLed appllcaLlons, whlch communlcaLe by a cllenL/server scheme, and are Lherefore easler Lo lmplemenL due Lo Lhe lnherenLly parallel naLure of C-code.
3
2 Introduction to Machine Vision 2.1 Introduction Wlklpedla: Machlne vlslon (Mv) ls Lhe process of applylng a range of Lechnologles Lo provlde lmaglng-based auLomaLlc lnspecLlon, process conLrol and roboL guldance ln lndusLrlal appllcaLlons. A generally accepLed deflnlLlon of machlne vlslon ls the ana|ys|s of |mages to extract data for contro|||ng a process or act|v|ty". 1he prlmary uses for machlne vlslon are auLomaLlc lnspecLlon and roboL guldance. 1he maln caLegorles lnLo whlch Mv appllcaLlons fall are quallLy assurance, sorLlng, maLerlal handllng, roboL guldance, and callbraLlon, eLc. 2.2 Image Processing and Analysis AfLer Lhe lmage ls Laken we need Lo do some lmage processlng and analysls. 8elow we llsL some of Lhe mosL used funcLlons: 2.2.1 Thresholding ConverLlng a grayscale lmage Lo black and whlLe. 2.2.2 Pattern recognition and matching 2.2.3 Texture recognition 2.2.4 Barcode reading We have dlfferenL klnd of barcodes and we see barcodes everywhere Loday. We have 1u barcodes, 2u barcodes, C8 codes, eLc. 1oday barcodes have a loL of appllcaLlons. 4 lnLroducLlon Lo Machlne vlslon Tutorial: Introduction to Vision Systems in LabVIEW
A barcode ls an opLlcal machlne-readable represenLaLlon of daLa, whlch shows daLa abouL Lhe ob[ecL by varylng Lhe wldLhs and spaclng's of parallel llnes, and may be referred Lo as llnear or 1 dlmenslonal (1u) barcode. 8elow we see a sLandard barcode:
LaLer Lhey evolved lnLo recLangles, doLs, hexagons and oLher geomeLrlc paLLerns ln 2 dlmenslons (2u). AlLhough 2u sysLems use a varleLy of symbols, Lhey are generally referred Lo as barcodes as well. 8elow we see a so-called C8 code:
1he C8 code ls one of Lhe mosL popular Lypes of Lwo-dlmenslonal barcodes. 8arcodes orlglnally were scanned by speclal opLlcal scanners called barcode readers, laLer, scanners and lnLerpreLlve sofLware became avallable on devlces lncludlng deskLop prlnLers and smarLphones. 2.2.5 OCR CC8 or CpLlcal CharacLer 8ecognlLlon ls Lhe mechanlcal or elecLronlc LranslaLlon of scanned lmages of handwrlLLen, LypewrlLLen or prlnLed LexL lnLo machlne-encoded LexL. lL ls wldely used Lo converL books and documenLs lnLo elecLronlc flles. 1yplcally mulLlfuncLlon prlnLers wlLh scanner funcLlonallLy lnclude some sofLware for CC8. 2.2.6 Gauging Measurlng ob[ecL dlmenslons. 3 lnLroducLlon Lo Machlne vlslon Tutorial: Introduction to Vision Systems in LabVIEW
2.2.7 Position 2.2.8 Edge Detection 2.2.9 Color analysis 2.2.10 Filtering 2.2.11 Counting and Classification ClasslflcaLlon ls a Lool for ldenLlfylng an unknown ob[ecL by comparlng lLs slgnlflcanL feaLures Lo a seL of feaLures LhaL represenL known samples. 1yplcally e.g., we could have some bolLs and screws we wanL Lo classlfy or counL on an assembly llne or someLhlng.
ln Lhese slLuaLlons we Lyplcally have a LemplaLe lmage of each of Lhe ob[ecLs we wanL Lo classlfy or counL LhaL ls used for comparlson.
6
3 Vision Cameras 1here exlsL dlfferenL cameras used ln machlne vlslon. We can dlvlde lnLo 3 caLegorles based on Lhelr connecLlons Lo Lhe C: uS8 cameras lLLL 1394 (llreWlre) cameras ClgL (LLherneL) cameras ln Lhls LuLorlal we wlll use a 8as|er scA640-70gc ClgL camera. 3.1 GigE (Ethernet) Cameras 8elow we see a sLandard ClgL LLherneL camera:
1he camera ls connecLed Lo Lhe compuLer uslng a sLandard LLherneL cable.
7
4 Introduction to Vision Systems in LabVIEW 4.1 Software naLlonal lnsLrumenLs offers dlfferenL klnd vlslon sofLware dependlng on your appllcaLlon and your needs: NI V|s|on Acqu|s|t|on Software NI V|s|on Deve|opment Modu|e nl vlslon 8ullder for AuLomaLed lnspecLlons We wlll dlscuss Lhe dlfferenL packages more ln deLall below. lf you lnsLall all Lhese 3 packages, you wlll end up wlLh Lhe followlng paleLLe ln LabvlLW:
4.1.1 NI Vision Acquisition Software 1he NI V|s|on Acqu|s|t|on software ls Lhe baslc sofLware you need lf you wanL Lo creaLe vlslon appllcaLlons for LabvlLW or Lhe .nL1 plaLform. 1he nl vlslon AcqulslLlon sofLware lncludes Lhe necessary drlvers, such as nl-lMAC and nl-lMACdx. 1he NI-IMAdx drlver sofLware glves you Lhe ablllLy Lo acqulre lmages wlLh lLLL 1394 (llreWlre), ClgL vlslon (LLherneL), and uS8 cameras. 8 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
4.1.2 Vision Development Module lor more advanced machlne vlslon and lmage processlng you wlll need Lhe V|s|on Deve|opment Modu|e. 1he vlslon uevelopmenL Module conLalns hundreds of lmage processlng and machlne vlslon funcLlons, boLh for LabvlLW and Lhe .nL1 plaLform. 1hls package lncludes bullL-ln funcLlons for: aLLern maLchlng 1exLure recognlLlon CounLlng and ClasslflcaLlon CC8 (CpLlcal CharacLer 8ecognlLlon) 8ar Code readers lmage lllLers eLc. 8elow we see Lhe Image rocess|ng" paleLLe ln LabvlLW:
8elow we see Lhe Mach|ne V|s|on" paleLLe ln LabvlLW: 9 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
We wlll learn more abouL Lhese laLer. 4.1.3 Vision Builder for Automated Inspections nl vlslon 8ullder for AuLomaLed lnspecLlon (Al) ls an exLernal and lndependenL appllcaLlon for bulldlng and machlne vlslon appllcaLlons wlLhouL Lhe need for programmlng. 4.2 Configuration When Lhe necessary sofLware ls lnsLalled, we use Lhe Measurement & Automat|on Lxp|orer (MAk) Lo geL sLarLed. When we plug ln Lhe camera uslng an LLherneL cable lnLo Lhe compuLer, Lhe camera should appear ln Lhe llsL. ?ou can connecL a camera Lo a local Wlndows machlne or a LabvlLW 8eal-1lme LargeL machlne. We wlll focus on connecLlon Lhe camera Lo a local machlne. CompleLe Lhe followlng sLeps Lo connecL a ClgL camera or an lLLL 1394 camera Lo a local Wlndows machlne: 1. ConnecL Lhe camera Lo Lhe LLherneL porL on Lhe local machlne. 2. ln Lhe MAx conflguraLlon Lree, expand uevlces and lnLerfaces Lo obLaln a llsL of lnsLalled devlces. 3. Lxpand nl-lMACdx uevlces Lo obLaln a llsL of avallable cameras. 10 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
When Lhe camera ls successfully connecLed, you can conflgure and LesL Lhe camera ln MAx. Cllck Lhe camera name Lo selecL Lhe approprlaLe camera (ln Lhls LuLorlal we use a 8asler scA640-70gc" camera). Cllck Snap Lo acqulre a slngle lmage or cllck Grab Lo conLlnuously acqulre an lmage. lf everyLhlng works, you should be able Lo see an lmage lnslde camera wlndow:
1hough you mlghL geL an error llke Lhls: 11 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
Whlch means you should reduce Lhe ackage S|ze ln Lhe conflguraLlon and/or conflgure Lhe I|rewa|| on your compuLer. When lL comes Lo Lhe llrewall, Lhe easlesL Lhlng Lo do ls Lo Lurn Lhe whole llrewall off ln order Lo make sure Lhe camera works.
8uL lL ls noL recommended Lo Lurn off Lhe llrewall enLlrely and leL Lhe compuLer be unproLecLed for a long Llme. lf sLlll noL worklng, you should also Lry Lo Lurn of Lhe Ant| V|rus sofLware Lemporary. 4.3 Building Vision Systems in LabVIEW 12 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
We sLarL by demonsLraLlng how Lo acqulre lmages from Lhe camera uslng LabvlLW code. 4.3.1 Using the Vision Acquisition Express VI 1he slmplesL way Lo acqulre lmages from LabvlLW ls Lo use Lhe vlslon AcqulslLlon Lxpress vl. We flnd Lhe vlslon AcqulslLlon Lxpress vl" ln Lhe vlslon Lxpress paleLLe ln LabvlLW:
When we drag Lhe vlslon AcqulslLlon Lxpress vl" Lo Lhe lock dlagram, a wlzard wlll appear:
1he flnlshed LabvlLW program wlll slmply look llke Lhls: 13 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
1he lmage wlll be acqulred on Lhe lronL anel ln Lhe lmage CuL":
4.3.2 Using the IMAQdx VIs 8elow we see Lhe nl-lMACdx paleLLe ln LabvlLW: 14 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
8elow we see a slmple example where we use Lhe lMACdx vls Lo creaLe an appllcaLlon where we acqulre a slngle lmage from Lhe camera.
13 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
Cn Lhe lronL anel we can use dlfferenL conLalners for showlng lmages on Lhe screen:
4.3.3 Open Images from a File When worklng wlLh vlslon sysLems lL ls lmporLanL Lo be able Lo save Lhe lmages Lo a flle or open an exlsLlng lmage from a flle. 16 lnLroducLlon Lo vlslon SysLems ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
8elow we see an example of how we can open and load an lmage from a flle lnLo LabvlLW:
ln Lhe example above lL wlll pop-up an open llle dlalog box, buL we can also speclfy Lhe flle paLh dlrecLly ln Lhe LabvlLW code. Cn Lhe lronL panel we wlll see Lhe lmage lnslde an lmage conLalner:
17
5 Vision functionality in LabVIEW 8elow we see Lhe vlslon and MoLlon paleLLe ln LabvlLW:
8elow we see Lhe lmage rocesslng" (vlslon and MoLlon - lmage rocesslng) paleLLe ln LabvlLW:
8elow we see Lhe Machlne vlslon" (vlslon and MoLlon - Machlne vlslon) paleLLe ln LabvlLW: 18 vlslon funcLlonallLy ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
5.1.1 Thresholding vlslon and MoLlon - lmage rocesslng - rocesslng
5.1.2 Pattern recognition and matching vlslon and MoLlon - Machlne vlslon - llnd aLLerns 19 vlslon funcLlonallLy ln LabvlLW Tutorial: Introduction to Vision Systems in LabVIEW
(Intelligent Systems, Control and Automation_ Science and Engineering 72) B. S. Goh, W. J. Leong, K. L. Teo (Auth.), Honglei Xu, Xiangyu Wang (Eds.)-Optimization and Control Methods in Industrial Engi