Table Of ContentJava Image
Processing
Recipes
With OpenCV and JVM
—
Nicolas Modrzyk
Java Image
Processing Recipes
With OpenCV and JVM
Nicolas Modrzyk
Java Image Processing Recipes
Nicolas Modrzyk
Tokyo, Japan
ISBN-13 (pbk): 978-1-4842-3464-8 ISBN-13 (electronic): 978-1-4842-3465-5
https://doi.org/10.1007/978-1-4842-3465-5
Library of Congress Control Number: 2018936912
Copyright © 2018 by Nicolas Modrzyk
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Susan McDermott / Shiva Ramachandran
Development Editor: Laura Berendson
Coordinating Editor: Rita Fernando
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233
Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit http://www.apress.
com/rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book's product page, located at www.apress.
com/9781484234648. For more detailed information, please visit http://www.apress.com/
source-code.
Printed on acid-free paper
Table of Contents
About the Author ���������������������������������������������������������������������������������xi
About the Technical Reviewer �����������������������������������������������������������xiii
Acknowledgments ������������������������������������������������������������������������������xv
Introduction ��������������������������������������������������������������������������������������xvii
Chapter 1: OpenCV on the JavaVM �������������������������������������������������������1
1 -1 Getting Started with Leiningen �����������������������������������������������������������������������2
Problem �����������������������������������������������������������������������������������������������������������2
Solution �����������������������������������������������������������������������������������������������������������2
How it works����������������������������������������������������������������������������������������������������3
1 -2 Writing Your First OpenCV Java Program ��������������������������������������������������������9
Problem �����������������������������������������������������������������������������������������������������������9
Solution �����������������������������������������������������������������������������������������������������������9
How it works����������������������������������������������������������������������������������������������������9
1 -3 Automatically Compiling and Running Code �������������������������������������������������13
Problem ���������������������������������������������������������������������������������������������������������13
Solution ���������������������������������������������������������������������������������������������������������13
How it works��������������������������������������������������������������������������������������������������13
1 -4 Using a Better Text Editor �����������������������������������������������������������������������������15
Problem ���������������������������������������������������������������������������������������������������������15
Solution ���������������������������������������������������������������������������������������������������������16
How it works��������������������������������������������������������������������������������������������������16
iii
TTaabbllee ooff CCoonnTTeennTTss
1 -5 Learning the Basics of the OpenCV Mat Object ��������������������������������������������21
Problem ���������������������������������������������������������������������������������������������������������21
Solution ���������������������������������������������������������������������������������������������������������21
How it works��������������������������������������������������������������������������������������������������21
1 -6 Loading Images from a File ��������������������������������������������������������������������������24
Problem ���������������������������������������������������������������������������������������������������������24
Solution ���������������������������������������������������������������������������������������������������������24
How it works��������������������������������������������������������������������������������������������������25
1 -7 Saving Images into a File �����������������������������������������������������������������������������28
Problem ���������������������������������������������������������������������������������������������������������28
Solution ���������������������������������������������������������������������������������������������������������28
How it works��������������������������������������������������������������������������������������������������28
1 -8 Cropping a Picture with Submat �������������������������������������������������������������������30
Problem ���������������������������������������������������������������������������������������������������������30
Solution ���������������������������������������������������������������������������������������������������������30
How it works��������������������������������������������������������������������������������������������������30
1 -9 Creating a Mat from Submats ����������������������������������������������������������������������35
Problem ���������������������������������������������������������������������������������������������������������35
Solution ���������������������������������������������������������������������������������������������������������35
How it works��������������������������������������������������������������������������������������������������35
1 -10 Highlighting Objects in a Picture ����������������������������������������������������������������40
Problem ���������������������������������������������������������������������������������������������������������40
Solution ���������������������������������������������������������������������������������������������������������40
How it works��������������������������������������������������������������������������������������������������41
iv
TTaabbllee ooff CCoonnTTeennTTss
1 -11 Using a Canny Result as a Mask ����������������������������������������������������������������44
Problem ���������������������������������������������������������������������������������������������������������44
Solutions ��������������������������������������������������������������������������������������������������������45
How it works��������������������������������������������������������������������������������������������������45
1 -12 Detecting Edges with Contours ������������������������������������������������������������������47
Problem ���������������������������������������������������������������������������������������������������������47
Solution ���������������������������������������������������������������������������������������������������������47
How it works��������������������������������������������������������������������������������������������������47
1 -13 Working with Video Streams ����������������������������������������������������������������������51
Problem ���������������������������������������������������������������������������������������������������������51
Solution ���������������������������������������������������������������������������������������������������������51
How it works��������������������������������������������������������������������������������������������������52
1 -14 Writing OpenCV Code in Scala ��������������������������������������������������������������������57
Problem ���������������������������������������������������������������������������������������������������������57
Solution ���������������������������������������������������������������������������������������������������������58
How it works��������������������������������������������������������������������������������������������������58
1 -15 Writing OpenCV Code in Kotlin ��������������������������������������������������������������������66
Problems �������������������������������������������������������������������������������������������������������66
Solutions ��������������������������������������������������������������������������������������������������������66
How it works��������������������������������������������������������������������������������������������������66
Chapter 2: OpenCV with Origami ��������������������������������������������������������83
2 -1 Starting to Code with Origami ����������������������������������������������������������������������84
Problem ���������������������������������������������������������������������������������������������������������84
Solution ���������������������������������������������������������������������������������������������������������84
How it works��������������������������������������������������������������������������������������������������85
v
TTaabbllee ooff CCoonnTTeennTTss
2 -2 Working with Mats ��������������������������������������������������������������������������������������106
Problem �������������������������������������������������������������������������������������������������������106
Solution �������������������������������������������������������������������������������������������������������106
How it works������������������������������������������������������������������������������������������������107
2 -3 Loading, Showing, Saving Mats ������������������������������������������������������������������117
Problem �������������������������������������������������������������������������������������������������������117
Solution �������������������������������������������������������������������������������������������������������117
How it works������������������������������������������������������������������������������������������������117
2 -4 Working with Colors, ColorMaps, and ColorSpaces ������������������������������������124
Problem �������������������������������������������������������������������������������������������������������124
Solution �������������������������������������������������������������������������������������������������������125
How it works������������������������������������������������������������������������������������������������125
2 -5 Rotating and Transforming Mats ����������������������������������������������������������������138
Problem �������������������������������������������������������������������������������������������������������138
Solution �������������������������������������������������������������������������������������������������������138
How it works������������������������������������������������������������������������������������������������139
2 -6 Filtering Mats ���������������������������������������������������������������������������������������������148
Problem �������������������������������������������������������������������������������������������������������148
Solution �������������������������������������������������������������������������������������������������������148
How it works������������������������������������������������������������������������������������������������148
2 -7 Applying Simple Masking Techniques ��������������������������������������������������������161
Problem �������������������������������������������������������������������������������������������������������161
Solution �������������������������������������������������������������������������������������������������������161
How it works������������������������������������������������������������������������������������������������161
vi
TTaabbllee ooff CCoonnTTeennTTss
2 -8 Blurring Images ������������������������������������������������������������������������������������������166
Problem �������������������������������������������������������������������������������������������������������166
Solution �������������������������������������������������������������������������������������������������������167
How it works������������������������������������������������������������������������������������������������167
Chapter 3: Imaging Techniques ��������������������������������������������������������175
3 -1 Playing with Colors �������������������������������������������������������������������������������������176
Problem �������������������������������������������������������������������������������������������������������176
Solution �������������������������������������������������������������������������������������������������������177
How it works������������������������������������������������������������������������������������������������177
3 -2 Creating Cartoons ���������������������������������������������������������������������������������������201
Problem �������������������������������������������������������������������������������������������������������201
Solution �������������������������������������������������������������������������������������������������������201
How it works������������������������������������������������������������������������������������������������201
3 -3 Creating Pencil Sketches ����������������������������������������������������������������������������211
Problem �������������������������������������������������������������������������������������������������������211
Solution �������������������������������������������������������������������������������������������������������211
How it works������������������������������������������������������������������������������������������������212
3 -4 Creating a Canvas Effect ����������������������������������������������������������������������������221
Problem �������������������������������������������������������������������������������������������������������221
Solution �������������������������������������������������������������������������������������������������������221
How it works������������������������������������������������������������������������������������������������221
3 -5 Highlighting Lines and Circles ��������������������������������������������������������������������226
Problem �������������������������������������������������������������������������������������������������������226
Solution �������������������������������������������������������������������������������������������������������226
How it works������������������������������������������������������������������������������������������������227
vii
TTaabbllee ooff CCoonnTTeennTTss
3 -6 Finding and Drawing Contours and Bounding Boxes ���������������������������������239
Problem �������������������������������������������������������������������������������������������������������239
Solution �������������������������������������������������������������������������������������������������������240
How it works������������������������������������������������������������������������������������������������240
3 -7 More on Contours: Playing with Shapes �����������������������������������������������������252
Problem �������������������������������������������������������������������������������������������������������252
Solution �������������������������������������������������������������������������������������������������������252
How it works������������������������������������������������������������������������������������������������252
3 -8 Moving Shapes �������������������������������������������������������������������������������������������260
Problem �������������������������������������������������������������������������������������������������������260
Solution �������������������������������������������������������������������������������������������������������261
How it works������������������������������������������������������������������������������������������������262
3 -9 Looking at Trees �����������������������������������������������������������������������������������������266
Problem �������������������������������������������������������������������������������������������������������266
Solution �������������������������������������������������������������������������������������������������������266
How it works������������������������������������������������������������������������������������������������266
3 -10 Detecting Blur �������������������������������������������������������������������������������������������269
Problem �������������������������������������������������������������������������������������������������������269
Solution �������������������������������������������������������������������������������������������������������269
How it works������������������������������������������������������������������������������������������������270
3- 11 Making Photomosaics ������������������������������������������������������������������������������272
Problem �������������������������������������������������������������������������������������������������������272
Solution �������������������������������������������������������������������������������������������������������272
How it works������������������������������������������������������������������������������������������������273
viii
TTaabbllee ooff CCoonnTTeennTTss
Chapter 4: Real-Time Video ��������������������������������������������������������������279
4 -1 Getting Started with Video Streaming ��������������������������������������������������������280
Problem �������������������������������������������������������������������������������������������������������280
Solution �������������������������������������������������������������������������������������������������������280
How it works������������������������������������������������������������������������������������������������281
4 -2 Combining Multiple Video Streams �������������������������������������������������������������288
Problem �������������������������������������������������������������������������������������������������������288
Solution �������������������������������������������������������������������������������������������������������289
How it works������������������������������������������������������������������������������������������������289
4 -3 Warping Video ���������������������������������������������������������������������������������������������290
Problem �������������������������������������������������������������������������������������������������������290
Solution �������������������������������������������������������������������������������������������������������290
How it works������������������������������������������������������������������������������������������������291
4 -4 Using Face Recognition ������������������������������������������������������������������������������293
Problem �������������������������������������������������������������������������������������������������������293
Solution �������������������������������������������������������������������������������������������������������293
How it works������������������������������������������������������������������������������������������������294
4 -5 Diffing with a Base Image ��������������������������������������������������������������������������297
Problem �������������������������������������������������������������������������������������������������������297
Solution �������������������������������������������������������������������������������������������������������297
How it works������������������������������������������������������������������������������������������������298
4- 6 Finding Movement ��������������������������������������������������������������������������������������302
Problem �������������������������������������������������������������������������������������������������������302
Solution �������������������������������������������������������������������������������������������������������302
How it works������������������������������������������������������������������������������������������������303
ix