Image segmentation and synthesis controlled by Java threads

Source: Internet
Author: User
Tags image filter
Image segmentation and synthesis controlled by Java threads-Linux general technology-Linux programming and kernel information. The following is a detailed description. Abstract: The WEB static images are segmented and reorganized using Java, and then displayed based on user interaction operations. The concurrency of the program is controlled using threads, in this way, the dynamic and interactive effects of image display are achieved.
Keywords: Java language Class Thread

Java is an object-oriented programming language. It has platform-independent, object-oriented, dynamic, and secure features. It allows direct multi-threaded programming and concurrency control of programs. Java also supports distributed network operations to facilitate access to network file objects. Using the drawing function provided by the Java language itself, you can draw some simple graphics. For complex graphics, images are generally made using the drawing software, and then downloaded and processed and controlled using the methods provided by Java to achieve dynamic display of static images.
1 Java image processing and concurrent thread control
1.1 Java Image Processing
The Java language provides a wide range of classes, interfaces, and corresponding call methods ). Using these classes or interfaces, you can define your own classes or subclasses and fully utilize the Java object-oriented features for programming. In the java. awt package, a special Image class is provided. It is an abstract class that provides abstract methods to depict some common features of images. In the Applet Class and Tookit class, two getImage () methods are provided to download images, and the images to be downloaded are queried based on the absolute address and relative address. The download method of the relative address is usually used. Its syntax and functions are as follows:
1. public Image getImage (URL url, String name)
Function: Download images from a specific address.
Parameter: url ?? URL (unified resource positioning) base address,
Name ?? Image File Name.
After obtaining the image, you can call the drawImage () method provided by the Graphics class to display the image.
2. public drawImage (Image img, int x, int y, observer)
Function: displays an image at a specified position.
Parameter: img ?? Image to be displayed, x ?? X coordinate, y ?? Ordinate, observer ?? The image monitor is used to monitor image downloads and accept image loading information (True is returned when the image is fully loaded; otherwise False is returned ).
When displaying an image, you usually want to control the image and play the media in your favorite ways. To this end, Java provides the ImageObserver class and MediaTracker class for tracking multimedia objects such as images and sounds. In this article, the program mainly uses the MediaTracker class for tracking the status of multiple images.
1.2 Java multi-thread concurrency
Currently, threads have been used by many operating systems and application development systems. A thread is a single control flow of a program and has the characteristics of an ordered program. However, a thread is not a program, but only an execution sequence of the program. The thread has a strong concurrency function, and multiple threads may be in the execution status at the same time. The thread is dynamic and has a certain life cycle, which goes through the process from creation, execution, blocking to extinction. Java provides special Thread classes to support direct multi-threaded programming. The Thread class provides Thread control methods, such as Start (), Stop (), Run (), Suspend (), resume (), Sleep (), and Run () methods and so on. They can control the thread status. You can also use SetPriority () to set the thread running priority. The Thread class is defined as follows:
Thread (ThreadGroup group, Runable target, String name)
SetPriority () is used to set the thread priority. The thread priority is an integer between MINPRIORITY (defined as 1 in the class) and MAXPRIORITY (defined as 10 in the class. Different thread priorities determine the switching between different threads.
2 Java image segmentation and synthesis algorithm and implementation
The Java program first downloads a complete image and splits it into 20 unit puzzles, which are divided into five rows and four columns. In this example, 20th image units are set to one blank image for user interaction. These parameters are defined in the corresponding variables.
Final int XCELLS = 5; // Number of puzzles in each row
Final int YCELLS = 4; // Number of puzzles in each column
Final int ALLCELLS = 20; // Number of split Elements
Final int EMPTY = 19; // set unit 20th, that is, cells [19]
// Blank image
Then, these image units are stored in the Cell array cells []. Here, the Cell class contains the image, its starting position, and its current position. The specific definitions are as follows:
Class Cell
{Int sx, sy; // start position
Int cx, cy; // current location
Image img; // unit Image
Public Cell (Image img, int x, int y) // Cell class Constructor
{This. img = img;
Sx = x; sy = y;} // assign the starting position x, y
}
To track the status of each image segmentation unit, you also need to create a MediaTracker class entity (instance) and then call the addImage () method, specify a unique identifier for each image to be tracked. The identifier determines the priority of image acquisition and allows the image to be processed independently and completely.
MediaTracker tracker = new MediaTracker (this)
// Create a MediaTracker object for the current class used for tracking the class
// The image on
Cells [EMPTY] = new Cell (createEmpty (), toPoint (EMPTY). x, toPoint (EMPTY). y );
Tracker. addImage (cells [EMPTY]. img, 0 );
// Call the createEmpty () method to generate a blank image and add it to the tracked Image
// 20 units in the cells array
Void setPosition (int x, int y) // you can specify the current position of the unit image.
{Cx = x; cy = y ;}

The positions of each image unit are stored in the position array:
Int position [] [] = new int [XCELLs] [YCELLS]
In this way, Cell array cells [] can be used to operate on each unit of the image, so as to synthesize and display each unit of the image, and change the position of each unit through the position [] [] array. In order to perform concurrent operations on each unit, the thread needs to be controlled, and the start, sleep, and status changes of the process need to be controlled through mouse events and key events, the implementation method is as follows (the run () method is used as an example ):
Thread imageThread = null; // defines the Thread imageThread, initial
// The value is null.
Public void run ()
{ImageThread. setPriority (Thread. MINPRIORITY); // sets the line
// Process execution priority

Try
{ImageThread. sleep (2000); // The thread waits for 2000 ms of sleep.
} Catch (InterruptedException e ){}
First = changeArray (); // call the changeArray () method to randomly change the Graph
// Image unit position
While (! Loaded) // determines whether an image is not tracked and loaded.
// Method tracking and image loading
{Repaint ();
Try
{ImageThread. sleep (100 );
} Catch (InterruptedException e) {System. out. println (e );}
}
}
The changeArray () method is used to randomly change the position of an image unit. The implementation method is as follows:
Boolean changeArray ()
{
Int source [] = new int [20];
Int full [] = new int [20];
For (int I = 0; I {
Int r = (int) (Math. random () * 20 );
While (full [r]! = 0)
R = (r + (int) (Math. random () * 20) % 20;
Source [I] = r;
Full [r] = 1;
}
Int pos = 0;
For (int I = 0; I {
Point p = toPoint (source [pos]);
Cells [pos]. setPosition (p. x, p. y );
Position [p. x] [p. y] = pos;
}
X = cells [EMPTY]. cx;
Y = cells [EMPTY]. cy;
Return (false );
}
After the applet is executed, click the mouse, and the thread starts to load the image and execute changeArray () to randomly select one position to move the image unit, in this case, you can use the keyboard to move any unit on the image to any position. Another important thing here is how to divide an image into many units. We can use the CropImageFilter method to separate the image. It is a segmentation image filter. The implementation method is as follows:
Image crop (int pos)
{// The pos parameter is the image unit location number provided by the call function.
Point p = toPoint (pos); // convert the position number to the coordinate form
ImageFilter filter = new CropImageFilter (xside * p. x, yside * p. y, xside, yside );
// Create a segmented image in the absolute rectangle of the given coordinate and length/width
// Filter entity filter
ImageProducer producer = new FilteredImageSource (baseImage. getSource (), filter ;)

// Create a new image from the original image and the split image filter entity

// Producer
Return createImage (producer); // generated by the image generator producer
// Image and return
}

After performing the preceding steps, the entire image is split and displayed in a compositing manner. This program mainly uses crop (), changeArray (), mousedown (), Thread () and a few Boolean variables that indicate the running status of the program to implement a process of image processing combining thread control and dynamic images.

【Author】: Computer Science, Dongying University of Petroleum (257062)
Related Article

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.