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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Designing Frontend Applications for Failure, Not Just Success
  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • AI Agents and Frontier Models: Insights From The Neuron

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Nov. 05, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook