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
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

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

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

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • A Systematic Approach for Java Software Upgrades
  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Building a Simple RAG Application With Java and Quarkus
  • Dust Actors and Large Language Models: An Application

Trending

  • Building an AI/ML Data Lake With Apache Iceberg
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  1. DZone
  2. Data Engineering
  3. Data
  4. How To Implement Image Segmentation

How To Implement Image Segmentation

Image segmentation is important for functions like background changing and cutout. Read on to check out a solution that helps easily implement this feature.

By 
Jackson Jiang user avatar
Jackson Jiang
DZone Core CORE ·
Jun. 16, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

Changing an image/video background has always been a hassle, whereby the most tricky part is extracting the element other than the background.

Traditionally, it requires us to use a PC image-editing program that allows us to select the element, add a mask, replace the canvas, and more. If the element has an extremely uneven border, then the whole process can be very time-consuming.

Luckily, ML Kit from HMS Core offers a solution that streamlines the process: the image segmentation service, which supports both images and videos. This service draws upon a deep learning framework, as well as detection and recognition technology. The service can automatically recognize — within seconds — the elements and scenario of an image or a video, delivering a pixel-level recognition accuracy. By using a novel framework of semantic segmentation, image segmentation labels each and every pixel in an image and supports 11 element categories, including humans, the sky, plants, food, buildings, and mountains.

Image segmentation

This service is a great choice for entertaining apps. For example, an image-editing app can use the service to realize swift background replacement. A photo-taking app can count on this service for the optimization of different elements (for example, the green plant) to make them appear more attractive.

Below is an example showing how the service works in an app.

ML Kit from HMS Core

Development Procedure

Before app development, there are some necessary preparations in AppGallery Connect. In addition, the Maven repository address should be configured for the SDK, and the SDK should be integrated into the app project.

The image segmentation service offers three capabilities: human body segmentation, multiclass segmentation, and hair segmentation.

  1. Human body segmentation: supports videos and images. The capability segments the human body from its background and is ideal for those who only need to segment the human body and background. The return value of this capability contains the coordinate array of the human body, the human body image with a transparent background, and the gray-scale image with a white human body and black background. Based on the return value, your app can further process an image to, for example, change the video background or cut out the human body.
  2. Multiclass segmentation: offers the return value of the coordinate array of each element. For example, when the image processing by the capability contains four elements (human body, sky, plant, and cat & dog), the return value is the coordinate array of the four elements. Your app can further process these elements, such as replacing the sky.
  3. Hair segmentation: segments hair from the background, with only images supported. The return value is a coordinate array of the hair element. For example, when the image processing by the capability is a selfie, the return value is the coordinate array of the hair element. Your app can then further process the element by, for example, changing the hair color.

Static Image Segmentation

1. Create an image segmentation analyzer.

  • Integrate the human body segmentation model package.
Java
 
// Method 1: Use default parameter settings to configure the image segmentation analyzer.
// The default mode is human body segmentation in fine mode. All segmentation results of human body segmentation are returned (pixel-level label information, human body image with a transparent background, gray-scale image with a white human body and black background, and an original image for segmentation).
MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(); 
// Method 2: Use MLImageSegmentationSetting to customize the image segmentation analyzer.
MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory() 
    // Set whether to use fine segmentation. true indicates yes, and false indicates no (fast segmentation).
    .setExact(false) 
    // Set the segmentation mode to human body segmentation.
    .setAnalyzerType(MLImageSegmentationSetting.BODY_SEG) 
    // Set the returned result types.
    // MLImageSegmentationScene.ALL: All segmentation results are returned (pixel-level label information, human body image with a transparent background, gray-scale image with a white human body and black background, and an original image for segmentation).
    // MLImageSegmentationScene.MASK_ONLY: Only pixel-level label information and an original image for segmentation are returned.
    // MLImageSegmentationScene.FOREGROUND_ONLY: A human body image with a transparent background and an original image for segmentation are returned.
    // MLImageSegmentationScene.GRAYSCALE_ONLY: A gray-scale image with a white human body and black background and an original image for segmentation are returned.
    .setScene(MLImageSegmentationScene.FOREGROUND_ONLY) 
    .create(); 
MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);


  • Integrate the multiclass segmentation model package.

When the multiclass segmentation model package is used for processing an image, an image segmentation analyzer can be created only by using MLImageSegmentationSetting.

Java
 
MLImageSegmentationSetting setting = new MLImageSegmentationSetting 
    .Factory()
    // Set whether to use fine segmentation. true indicates yes, and false indicates no (fast segmentation).
    .setExact(true) 
    // Set the segmentation mode to image segmentation.
    .setAnalyzerType(MLImageSegmentationSetting.IMAGE_SEG)
    .create(); 
MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);


  • Integrate the hair segmentation model package.

When the hair segmentation model package is used for processing an image, a hair segmentation analyzer can be created only by using MLImageSegmentationSetting.

Java
MLImageSegmentationSetting setting = new MLImageSegmentationSetting 
    .Factory()
    // Set the segmentation mode to hair segmentation.
    .setAnalyzerType(MLImageSegmentationSetting.HAIR_SEG)
    .create(); 
MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);


2. Create an MLFrame object by using android.graphics.Bitmap for the analyzer to detect images. JPG, JPEG, and PNG images are supported. It is recommended that the image size range from 224 x 224 px to 1280 x 1280 px.

Java
// Create an MLFrame object using the bitmap, which is the image data in bitmap format.
MLFrame frame = MLFrame.fromBitmap(bitmap);


3. Call asyncAnalyseFrame for image segmentation.

Java
// Create a task to process the result returned by the analyzer.
Task<MLImageSegmentation> task = analyzer.asyncAnalyseFrame(frame); 
// Asynchronously process the result returned by the analyzer.
task.addOnSuccessListener(new 
OnSuccessListener<MLImageSegmentation>() {
    @Override
    public void onSuccess(MLImageSegmentation segmentation) {
        // Callback when recognition is successful.
    }})
    .addOnFailureListener(new OnFailureListener() {
    @Override 
    public void onFailure(Exception e) {
        // Callback when recognition failed.
}});


4. Stop the analyzer and release the recognition resources when recognition ends.

Java
if (analyzer != null) {
    try {
        analyzer.stop(); 
    } catch (IOException e) {
        // Exception handling.
    }
}


The asynchronous call mode is used in the preceding example. Image segmentation also supports the synchronous call of the analyseFrame function to obtain the detection result:

Java
SparseArray<MLImageSegmentation> segmentations = analyzer.analyseFrame(frame);


Data structure application Java (programming language)

Published at DZone with permission of Jackson Jiang. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Systematic Approach for Java Software Upgrades
  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Building a Simple RAG Application With Java and Quarkus
  • Dust Actors and Large Language Models: An Application

Partner Resources

×

Comments

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: