Using Java to achieve image segmentation

Source: Internet
Author: User

To reduce IO operations during animation, we can use continuous animation to improve the efficiency of animation playback.
If we separate each small image of the image into separate files, we have to read the image information from the file every time we want to use these small images.

In fact, we can use Java to separate the entire large image in the application of each small image, and then use, directly from the segmented image to get the small image needed to use, so that you do not have to read from the file every time the small image information, the efficiency will naturally be relatively high.

So how to achieve image segmentation operation?

Example:
Import java.awt.Component;
Import Java.awt.Image;
Import Java.awt.image.CropImageFilter;
Import Java.awt.image.FilteredImageSource;
Import Java.awt.image.ImageFilter;

/**
* Image processing Helper class
*
* @author
*
*/
public class Imageutil {

/**
* Split Image
*
* @param image
* Incoming Picture object
* @param rows
* Number of pictures to be cropped vertically-line
* @param cols * Number of pictures to be cropped horizontally-column
* @param x
* Start clipping position x coordinate
* @param y
* Start clipping position y coordinates
* @param width
* Picture width per clip Degrees
* @param height
* Picture height per crop
* @param changex
* Number of x-coordinates to change each time
* @param changey
* Each need to change the number of y-coordinates
* @param component
* Container object, which is used to create a cropped picture object
* @return A two-dimensional picture array that has been cropped and loaded into memory
*/
public static image[][] Cutimag E (image image, int rows, int cols, int x,
int y, int width, int height, int changex, int changey,
Component compon ENT) {
image[][] images = new Image[rows][cols];
for (int i = 0, i < rows; i++) {
for (int j = 0; J < Co ls J + +) {
ImageFilter filter = new CropImageFilter (x + j * Changex, y + i
* changey, width, height);
Images[i][j] = Component.createimage (New FilteredImageSource (
Image.getsource (), filter));
}
}

return images;
}
}


To write a special method of segmenting the image Cutimage (), the method used in the CropImageFilter class, FilteredImageSource class details, you can refer to the JAVAAPI document.

Test:
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Java.awt.MediaTracker;
Import Java.awt.Toolkit;

Import Javax.swing.JFrame;

Import Com.utils.ImageUtil;

/**
* Image Segmentation Test
*
* @author
*
*/
public class Cutimagedemo extends JFrame {

Private static final long serialversionuid = 1140239462766935667L;
Private Mediatracker Mediatracker;
Private image[][] images;

Public Cutimagedemo () {
SetSize (800, 600);
Setlocationrelativeto (NULL);
Setdefaultcloseoperation (Exit_on_close);

Create a Media Tracker object
Mediatracker = new Mediatracker (this);
Get Source image
Image image = Toolkit.getdefaulttoolkit (). GetImage ("images/cg0430.jpg");
Split image
Images = Imageutil.cutimage (image, 5, 5, 0, 3,----------);
int index = 0;
Add all the segmented images to the Mediatracker tracking list
For (image[] images2:images) {
for (Image image2:images2) {
Mediatracker.addimage (Image2, index++);
}
}

SetVisible (TRUE);
}

@Override
public void Paint (Graphics g) {

try {
Load all images
Mediatracker.waitforall ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}

if (Mediatracker.checkall ()) {//All images loaded complete
Loops draw each segmented image into a form
for (int row = 0, len = images.length; row < Len; row++) {
for (int col = 0, length = images[row].length; col < length; col++) {
Image img = Images[row][col];
int imgwidth = Img.getwidth (null);
int imgheight = Img.getheight (null);
int x = col * (ImgWidth + 10) + 15;
int y = row * (ImgHeight + 15) + 40;
G.drawimage (IMG, x, y, null);
}
}
}
}

public static void Main (string[] args) {
New Cutimagedemo ();
}
}

Using Java to achieve image segmentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.