DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023
  • IntelliJ and Java Spring Microservices: Productivity Tips With GitHub Copilot
  • How To Approach Dependency Management in Java [Video]
  • NullPointerException in Java: Causes and Ways to Avoid It

Trending

  • Using Java Stream Gatherers To Improve Stateful Operations
  • Implementing Explainable AI in CRM Using Stream Processing
  • Designing a Java Connector for Software Integrations
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  1. DZone
  2. Coding
  3. Java
  4. Cartoonize Your Image With Java and OpenCV

Cartoonize Your Image With Java and OpenCV

We will learn using OpenCV and Java. OpenCV is a very popular framework developed using C++ and got bindings for Python, Java, R languages, etc.

By 
Unni Mana user avatar
Unni Mana
·
Updated Nov. 05, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.2K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In this tutorial, we will learn some cool stuff using OpenCV and Java. OpenCV is a very popular framework developed using C++ and got bindings for Python, Java, R languages, etc.

So what we are going to do is to generate a 'cartoon' type image from an original image using Java OpenCV binding

Download and Install

Go to 'https://opencv.org/releases/' and select the proper version. I am using Windows 10 for development. After installing the framework, navigate to the folder and check for its binaries (.DLL). Add these binaries to your path. I have used IntelliJ IDEA  for developing the code. You can choose any Java supported IDE.

Develop the Code

Create a file with a name called 'Cartoon.java' and add the following code to it:

Java
xxxxxxxxxx
1
 
1
static {
2
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
3
}


We are loading the OpenCV system libraries by name NATIVE_LIBRARY_NAME using Core API.

Next, we need to load the image which you want to convert into the cartoon image.

Mat img = Imgcodecs.imread("E:\\unni\\rose.jpeg");

//you can change the location 

We are loading an image using ImgCodecs API. This API has a similar method name called 'imread'.

In Python, we use 

img = cv2.imread("rose.jpeg")

But in the Java version, it is returning a 'Matrix' not an image. Mat class is a matrix. Matrix is a 2D array that will store image details in this case.

The Process to Convert to Cartoon

Java
xxxxxxxxxx
1
 
1
Mat dest = new Mat();
2
Imgproc.cvtColor(img, dest, Imgproc.COLOR_BGR2GRAY);


These are the following steps that are required to convert to cartoon:

1. Convert the image into Gray color

Java
xxxxxxxxxx
1
 
1
Mat gray = new Mat();
2
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);

2. Apply a Blur filter, in this case, a 'medianBlur'

   This is a smoothing the image by taking the median value at the neighborhood of each pixel.

Java
xxxxxxxxxx
1
 
1
Mat blur = new Mat();
2
Imgproc.medianBlur(gray, blur, 5);

3. Apply an algorithm called 'adaptiveThreshold' is the algorithm used to calculate the threshold value  for smaller regions and therefore, there will be different threshold values for different regions.

Java
xxxxxxxxxx
1
 
1
Mat edges = new Mat();
2
Imgproc.adaptiveThreshold(blur,edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
3
        Imgproc.THRESH_BINARY, 9, 9);

4. Apply a 'Bilateral filter' which is used to smoothing the image by preserving its edges.

Java
xxxxxxxxxx
1
 
1
Mat colorImg = new Mat();
2
Imgproc.bilateralFilter(edges, colorImg, 9, 250, 250);

5.  In this step, we combine the images generated in the previous step using  a 'bitwise_and' operation.

Java
xxxxxxxxxx
1
 
1
Mat cartoon = new Mat();
2
Core.bitwise_and(colorImg, edges, cartoon);


Finally, we save the new image into the disk

Java
xxxxxxxxxx
1
 
1
Imgcodecs.imwrite("rose_cartoon.jpg", cartoon);


This is the below cartoon image generated after executing the previous step.

cartoon rose image

This is the original image

rose image


We have successfully generated a cartoon image from its original image.

Java (programming language) OpenCV intellij

Opinions expressed by DZone contributors are their own.

Related

  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023
  • IntelliJ and Java Spring Microservices: Productivity Tips With GitHub Copilot
  • How To Approach Dependency Management in Java [Video]
  • NullPointerException in Java: Causes and Ways to Avoid It

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!