OpenCV - RxJS, Ggplot2, Python Data Persistence,

2y ago
16 Views
2 Downloads
1.86 MB
28 Pages
Last View : 20d ago
Last Download : 3m ago
Upload by : Angela Sonnier
Transcription

OpenCVAbout the TutorialOpenCV is a cross-platform library using which we can develop real-time computer visionapplications. It mainly focuses on image processing, video capture and analysis includingfeatures like face detection and object detection. In this tutorial, we explain how you canuse OpenCV in your applications.AudienceThis tutorial has been prepared for beginners to make them understand the basics ofOpenCV library. We have used the Java programming language in all the examples,therefore you should have a basic exposure to Java in order to benefit from this tutorial.PrerequisitesFor this tutorial, it is assumed that the readers have a prior knowledge of Javaprogramming language. In some of the programs of this tutorial, we have used JavaFX forGUI purpose. So, it is recommended that you go through our JavaFX tutorial beforeproceeding further - http://www.tutorialspoint.com/javafx/.Copyright & Disclaimer Copyright 2017 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com1

OpenCVTable of ContentsAbout the Tutorial . 1Audience . 1Prerequisites . 1Copyright & Disclaimer . 1Table of Contents . 21.OpenCV – Overview . 5Computer Vision . 5Applications of Computer Vision . 5Features of OpenCV Library. 6OpenCV Library Modules . 7A Brief History of OpenCV . 82.OpenCV – Environment . 9Installing OpenCV . 9Eclipse Installation . 11Setting the Path for Native Libraries . 183.OpenCV — Storing Images . 21The Mat Class . 21Creating and Displaying the Matrix . 23Loading Image using JavaSE API . 254.OpenCV – Reading Images . 275.OpenCV Writing an Image . 296.OpenCV— GUI . 31Converting Mat to Buffered Image . 31Displaying Image using AWT/Swings . 32Displaying Image using JavaFX . 34TYPES OF IMAGES . 387.OpenCV — The IMREAD XXX Flag. 398.OpenCV Reading an Image as Grayscale . 419.OpenCV Reading Image as BGR . 45IMAGE CONVERSION . 4910. OpenCV Colored Images to GrayScale . 5011. OpenCV Colored Image to Binary . 5412. OpenCV Grayscale to Binary . 582

OpenCVDRAWING FUNCTIONS . 6213. OpenCV – Drawing a Circle . 6314. OpenCV – Drawing a Line . 6715. OpenCV Drawing a Rectangle . 7116. OpenCV – Drawing an Ellipse . 7517. OpenCV – Drawing Polylines . 7918. OpenCV – Drawing Convex Polylines. 8419. OpenCV—Drawing Arrowed Lines. 8820. OpenCV – Adding Text . 92BLUR OPERATIONS . 9621. OpenCV – Blur (Averaging) . 9722. OpenCV – Gaussian Blur . 10023. OpenCV – Median Blur . 103FILTERING . 10624. OpenCV – Bilateral Filter . 10725. OpenCV – Box Filter . 11026. OpenCV – SQRBox Filter . 11327. OpenCV – Filter2D. 11628. OpenCV—Dilation . 11929. OpenCV – Erosion . 12230. OpenCV Morphological Operations . 12531. OpenCV Image Pyramids . 131Pyramid Up . 131Pyramid Down . 133Mean Shift Filtering . 1363

OpenCVTHRESHOLDING . 13932. OpenCV – Simple Threshold . 14033. OpenCV – Adaptive Threshold . 144Other Types of Adaptive Thresholding . 14734. OpenCV Adding Borders . 148SOBEL DERIVATIVES . 15335. OpenCV – Sobel Operator . 15436. OpenCV – Scharr Operator . 157More Scharr Derivatives . 159TRANSFORMATION OPERATIONS . 16037. OpenCV – Laplacian Transformation . 16138. OpenCV – Distance Transformation . 164CAMERA & FACE DETECTION . 16939. OpenCV – Using Camera . 17040. OpenCV Face Detection in a Picture . 17541. OpenCV Face Detection using Camera . 179GEOMETRIC TRANSFORMATIONS . 18442. OpenCV Affine Translation . 18543. OpenCV – Rotation . 18844. OpenCV – Scaling . 19145. OpenCV – Color Maps . 194MISCELLANEOUS CONCEPTS. 20246. OpenCV – Canny Edge Detection . 20347. OpenCV – Hough Line Transform . 20648. OpenCV – Histogram Equalization. 2104

1. OpenCV – OverviewOpenCVOpenCV is a cross-platform library using which we can develop real-time computer visionapplications. It mainly focuses on image processing, video capture and analysis includingfeatures like face detection and object detection.Let’s start the chapter by defining the term "Computer Vision".Computer VisionComputer Vision can be defined as a discipline that explains how to reconstruct, interrupt,and understand a 3D scene from its 2D images, in terms of the properties of the structurepresent in the scene. It deals with modeling and replicating human vision using computersoftware and hardware.Computer Vision overlaps significantly with the following fields: Image Processing: It focuses on image manipulation. Pattern Recognition: It explains various techniques to classify patterns. Photogrammetry: It is concerned with obtaining accurate measurements fromimages.Computer Vision Vs Image ProcessingImage processing deals with image-to-image transformation. The input and output ofimage processing are both images.Computer vision is the construction of explicit, meaningful descriptions of physicalobjects from their image. The output of computer vision is a description or aninterpretation of structures in 3D scene.Applications of Computer VisionHere we have listed down some of major domains where Computer Vision is heavily used.Robotics Application Localization Determine robot location automatically Navigation Obstacles avoidance Assembly (peg-in-hole, welding, painting) Manipulation (e.g. PUMA robot manipulator) Human Robot Interaction (HRI): Intelligent robotics to interact with and servepeopleMedicine Application Classification and detection (e.g. lesion or cells classification and tumor detection)5

OpenCV 2D/3D segmentation 3D human organ reconstruction (MRI or ultrasound) Vision-guided robotics surgeryIndustrial Automation Application Industrial inspection (defect detection) Assembly Barcode and package label reading Object sorting Document understanding (e.g. OCR)Security Application Biometrics (iris, finger print, face recognition) Surveillance Detecting certain suspicious activities or behaviorsTransportation Application Autonomous vehicle Safety, e.g., driver vigilance monitoringFeatures of OpenCV LibraryUsing OpenCV library, you can – Read and write images Capture and save videos Process images (filter, transform) Perform feature detection Detect specific objects such as faces, eyes, cars, in the videos or images. Analyze the video, i.e., estimate the motion in it, subtract the background, andtrack objects in it.OpenCV was originally developed in C . In addition to it, Python and Java bindings wereprovided. OpenCV runs on various Operating Systems such as windows, Linux, OSx,FreeBSD, Net BSD, Open BSD, etc.This tutorial explains the concepts of OpenCV with examples using Java bindings.6

OpenCVOpenCV Library ModulesFollowing are the main library modules of the OpenCV library.Core FunctionalityThis module covers the basic data structures such as Scalar, Point, Range, etc., that areused to build OpenCV applications. In addition to these, it also includes themultidimensional array Mat, which is used to store the images. In the Java library ofOpenCV, this module is included as a package with the name org.opencv.core.Image ProcessingThis module covers various image processing operations such as image filtering,geometrical image transformations, color space conversion, histograms, etc. In the Javalibrary of OpenCV, this module is included as a package with the nameorg.opencv.imgproc.VideoThis module covers the video analysis concepts such as motion estimation, backgroundsubtraction, and object tracking. In the Java library of OpenCV, this module is included asa package with the name org.opencv.video.Video I/OThis module explains the video capturing and video codecs using OpenCV library. In theJava library of OpenCV, this module is included as a package with the nameorg.opencv.videoio.calib3dThis module includes algorithms regarding basic multiple-view geometry algorithms, singleand stereo camera calibration, object pose estimation, stereo correspondence andelements of 3D reconstruction. In the Java library of OpenCV, this module is included asa package with the name org.opencv.calib3d.features2dThis module includes the concepts of feature detection and description. In the Java libraryof OpenCV, this module is included as a package with the name org.opencv.features2d.ObjdetectThis module includes the detection of objects and instances of the predefined classes suchas faces, eyes, mugs, people, cars, etc. In the Java library of OpenCV, this module isincluded as a package with the name org.opencv.objdetect.HighguiThis is an easy-to-use interface with simple UI capabilities. In the Java library of OpenCV,the features of this module is included in two different packages namely,org.opencv.imgcodecs and org.opencv.videoio.7

OpenCVA Brief History of OpenCVOpenCV was initially an Intel research initiative to advise CPU-intensive applications. Itwas officially launched in 1999. In the year 2006, its first major version, OpenCV 1.0 was released. In October 2009, the second major version, OpenCV 2 was released. In August 2012, OpenCV was taken by a nonprofit organization OpenCV.org.8

2. OpenCV – EnvironmentOpenCVIn this chapter, you will learn how to install OpenCV and set up its environment in yoursystem.Installing OpenCVFirst of all, you need to download OpenCV onto your system. Follow the steps given below.Step 1: Open the homepage of OpenCV by clicking the following link: http://opencv.org/On clicking, you will see its homepage as shown below.9

OpenCVStep 2: Now, click the Downloads link highlighted in the above screenshot. On clicking,you will be directed to the downloads page of OpenCV.Step 3: On clicking the highlighted link in the above screenshot, a file named opencv3.1.0.exe will be downloaded. Extract this file to generate a folder opencv in your system,as shown in the following screenshot.10

OpenCVStep 4: Open the folder OpenCV - build - java. Here you will find the jar file ofOpenCV named opencv-310.jar. Save this file in a separate folder for further use.Eclipse InstallationAfter downloading the required JAR files, you have to embed these JAR files to your Eclipseenvironment. You can do this by setting the Build Path to these JAR files and by usingpom.xml.Setting Build PathFollowing are the steps to set up OpenCV in Eclipse:Step 1: Ensure that you have installed Eclipse in your system. If not, download and installEclipse in your system.11

OpenCVStep 2: Open Eclipse, click on File, New, and Open a new project as shown in the followingscreenshot.12

OpenCVStep 3: On selecting the project, you will get the New Project wizard. In this wizard,select Java project and proceed by clicking the Next button, as shown in the followingscreenshot.13

OpenCVStep 4: On proceeding forward, you will be directed to the New Java Project wizard.Create a new project and click Next, as shown in the following screenshot.14

OpenCVStep 5: After creating a new project, right-click on it. Select Build Path and clickConfigure Build Path as shown in the following screenshot.15

OpenCVStep 6: On clicking the Build Path option, you will be directed to the Java Build Pathwizard. Click the Add External JARs button, as shown in the following screenshot.Step 7: Select the path where you have saved the file opencv-310.jar.16

OpenCVStep 8: On clicking the Open button in the above screenshot, those files will be added toyour library.Step 9: On clicking OK, you will successfully add the required JAR files to the currentproject and you can verify these added libraries by expanding the Referenced Libraries.17

OpenCVSetting the Path for Native LibrariesIn addition to the JAR files, you need to set path for the native libraries (DLL files) ofOpenCV.Location of DLL files: Open the installation folder of OpenCV and go to the sub-folderbuild - java. Here you will find the two folders x64 (64 bit) and x86 (32 bit) whichcontain the dll files of OpenCV.Open the respective folder suitable for your operating system, then you can see the dllfile, as shown in the following screenshot.Now, set the path for this file too by following the steps given below—18

OpenCVStep 1: Once again, open the JavaBuildPath window. Here you can observe the added JARfile and the JRE System Library.Step 2: On expanding it, you will get the system libraries and Native library location,as highlighted in the following screenshot.19

OpenCVStep 3: Double-click on the Native library location. Here, you can see the NativeLibrary Folder Configuration window as shown below—Here, click the button External Folder and select the location of the dll file in yoursystem.20

3. OpenCV — Storing ImagesOpenCVTo capture an image, we use devices like cameras and scanners. These devices recordnumerical values of the image (Ex: pixel values). OpenCV is a library which processes thedigital images, therefore we need to store these images for processing.The Mat class of OpenCV library is used to store the values of an image. It represents ann-dimensional array and is used to store image data of grayscale or color images, voxelvolumes, vector fields, point clouds, tensors, histograms, etc.This class comprises of two data parts: the header and a pointer Header: Contains information like size, method used for storing, and the addressof the matrix (constant in size). Pointer: Stores the pixel values of the image (Keeps on varying).The Mat ClassThe OpenCV Java library provides this class with the same name (Mat) within the packageorg.opencv.core.ConstructorsThe Mat class of OpenCV Java library has various constructors, using which you canconstruct the Mat object.S.No1.Constructors and DescriptionMat()This is the default constructor with no parameters in most cases. We use this toconstructor to create an empty matrix and pass this to other OpenCV methods.2.Mat(int rows, int cols, int type)This constructor accepts three parameters of integer type representing thenumber of rows and columns in a 2D array and the type of the array (that is tobe used to store data).3.Mat(int rows, int cols, int type, Scalar s)Including the parameters of the previous one, this constructor additionallyaccepts an object of the class Scalar as parameter.4.Mat(Size size, int type)21

OpenCVThis constructor accepts two parameters, an object representing the size of thematrix and an integer representing the type of the array used to store the data.5.Mat(Size size, int type, Scalar s)Including the parameters of the previous one, this constructor additionallyaccepts an object of the class Scalar as parameter.6.Mat(long addr)7.Mat(Mat m, Range rowRange)This constructor accepts an object of another matrix and an object of the classRange representing the range of the rows to be taken to create a new matrix.8.Mat(Mat m, Range rowRange, Range colRange)Including the parameters of the previous one, this constructor additionallyaccepts an object of the class. Range representing the column range.9.Mat(Mat m, Rect roi)This constructor accepts two objects, one representing another matrix and theother representing the Region Of Interest.Note: Array type. Use CV 8UC1, ., CV 64FC4 to create 1-4 channel matrices, orCV 8UC(n), ., CV 64FC(n) to create multi-channel (up to CV CN MAX channels)matrices. The type of the matrices were represented by various fields of the class CvTypewhich belongs to the package org.opencv.core.Methods and DescriptionFollowing are some of the methods provided by the Mat class.S.NoMethods and DescriptionMat col(int x)1.2.This method accepts an integer parameter representing the index of a columnand retrieves and returns that columnMat row(int y)22

OpenCVThis method accepts an integer parameter representing the index of a row andretrieves and returns that rowint cols()3.This method returns the number of columns in the matrixint rows()4.This method returns the number of rows in the matrixMat setTo(Mat value)5.This method accepts an object of the Mat type and sets the array elements tothe specified value.Mat setTo(Scalar s)6.This method accepts an object of the Scalar type and sets the array elementsto the specified value.Creating and Displaying the MatrixIn this section, we are going to discuss our first OpenCV example. We will see how tocreate and display a simple OpenCV matrix.Given below are the steps to be followed to create and display a matrix in OpenCV.Step 1: Load the OpenCV native libraryWhile writing Java code using OpenCV library, the first step you need to do is to load thenative library of OpenCV using the loadLibrary(). Load the OpenCV native library asshown below.//Loading the core librarySystem.loadLibrary(Core.NATIVE LIBRARY NAME);Step 2: Instantiate the Mat classInstantiate the Mat class using any of the functions mentioned in this chapter earlier.//Creating a matrixMat matrix new Mat(5, 5, CvType.CV 8UC1, new Scalar(0));Step 3: Fill the matrix using the methods23

OpenCVYou can retrieve particular rows/columns of a matrix by passing index values to themethods row()/col().And, you can set values to these using any of the variants of the setTo() methods.//Retrieving the row with index 0Mat row0 matrix.row(0);//setting values of all elements in the row with index 0row0.setTo(new Scalar(1));//Retrieving the row with index 3Mat col3 matrix.col(3);//setting values of all elements in the row with index 3col3.setTo(new Scalar(3));ExampleYou can use the following program code to create and display a simple matrix in Java usingOpenCV library.import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.CvType;import org.opencv.core.Scalar;class DisplayingMatrix{public static void main(String[] args) {//Loading the core librarySystem.loadLibrary(Core.NATIVE LIBRARY NAME);//Creating a matrixMat matrix new Mat(5, 5, CvType.CV 8UC1, new Scalar(0));//Retrieving the row with index 0Mat row0 matrix.row(0);24

OpenCV//setting values of all elements in the row with index 0row0.setTo(new Scalar(1));//Retrieving the row with index 3Mat col3 matrix.col(3);//setting values of all elements in the row with index 3col3.setTo(new Scalar(3));//Printing the matrixSystem.out.println("OpenCV Mat data:\n" matrix.dump());}}On executing the above program, you will get the following output.OpenCV Mat 0,3,0]Loading Image using JavaSE APIThe BufferedImage class of the java.awt.image.BufferedImage package is used tostore an image and the ImageIO class of the package import javax.imageio providesmethods to read and write Images.ExampleYou can use the following program code to load and save images using JavaSE library.import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class LoadingImage JSE library {public static void main( String[] args ) throws IOException {25

OpenCV//Input FileFile input new File("C:/EXAMPLES/OpenCV/sample.jpg");//Reading the imageBufferedImage image ImageIO.read(input);//Saving the image with a different nameFile ouptut new File("C:/OpenCV/sample.jpg");ImageIO.write(image, "jpg", ouptut);System.out.println("image Saved");}}On executing the above program, you will get the following output.image SavedIf you open the specified path, you can observe the saved image as follows—26

OpenCVEnd of ebook previewIf you liked what you saw Buy it from our store @ https://store.tutorialspoint.com27

Computer Vision Vs Image Processing Image processing deals with image-to-image transformation. The input and output of image processing are both images. Computer vision is the construction of explicit, meaningful descriptions of physical objects from their image. The output of computer vision is a description or an interpretation of structures .

Related Documents:

2.Basic Numpy Tutorials 3.Numpy Examples List 4.OpenCV Documentation 5.OpenCV Forum 1.1.2Install OpenCV-Python in Windows Goals In this tutorial We will learn to setup OpenCV-Python in your Windows system. Below steps are tested in a Windows 7-64 bit machine with Visual Studio 2010 and Visual Studio 2012. The screenshots shows VS2012.

Outline: OPENCV 3.0 Intro –Learning OpenCV Version 2.0 coming by Aug –Announcing 50K Vision Challenge OpenCV Background OpenCV 3.0 High Level OpenCV 3.0 Modules Brand New

7. Sumber Referensi Belajar OpenCV 8. Tip-Tip Belajar OpenCV 9. Penutup 1. Apa Itu OpenCV? OpenCV (Open Computer Vision) Pustaka computer vision yang open source dan dipakai secara luas di macam-macam sistem operasi dan arsitektur komputer untuk keperluan pe

1.2 OpenCV and Python The application programmer has access to the necessary algorithms by OpenCV an API for solving computer vision problems. OpenCV incorporates methods for acquiring, processing and analyzing image data from real scenes. Interfaces to languages as C , Java and Python ar

Change the "Overview" dropdown to "Packages (PyPl)", and type opencv-python in the search box, then click on the run command pip install opencv-python link to install the opencv library in pytorch1x environment. Above will install opencv library in the pytorch1x environment. Now you can switch the tab in

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python provides huge set of libraries for different requirements, so it is appropriate for web scraping as well as for data visualization, machine learning, etc. Easily Explicable Syntax Python is a very readable programming language as python syntax are easy to understand. Python is ve