RSS Archive Contact Us Advertise

IT Management Begins With Security
SecurityProNews > Articles > Application Development > Thresholding An Image In Java
Search:
[ articles_application_development ]

Thresholding An Image In Java



IT Writer
Contributing Writer
2002-04-09

SecurityProNews RSS Feed SecurityProNews RSS Feed


Thresholding is the process of transforming a set of data that encompasses any range of values to a new set with only two possible values. This process is used often in digital image processing. Thresholding is a form of segmentation, which can be described as grouping pixels together that have like attributes. In most examples of image thresholding, the RGB value (or the Grey Level value for greyscale images) is the attribute of interest.

Thresholding can be contextual or noncontextual. Contextual means that the thresholding operation takes into consideration the relationship between image features. For instance, a pixel will be segmented not only by its RGB value but also with respect to its neighboring pixels. Noncontextual ignores those types of relationships and groups pixels exclusively by some global attribute. Noncontextual thresholding of RGB values is what this article will focus on.

We will take a buffered image and perform RGB thresholding according to values set by the user. Although you can set your high and low values any way you want, including gathering them from the user, for the purpose of this article, we will use 0 for the low and 255 for high. From the user, we will need the three separate threshold values, one for R, G, and B. A good way to retrieve them is via three JSliders, with the limits set at 0 and 255:

JSlider Rslider = new JSlider(VERTICAL, 0, 255, 127);
JSlider Gslider = new JSlider(VERTICAL, 0, 255, 127);
JSlider Bslider = new JSlider(VERTICAL, 0, 255, 127);

The above code declares three JSliders with the appropriate limits and sets the initial value to 127.

For the actual thresholding, you will need to retrieve a WritableRaster object from the BufferedImage:

WritableRaster wraster = buffimage.getRaster();

This assumes your buffered image is named ‘buffimage’ and uses the getRaster() method to retrieve the WritableRaster. In many cases, you may want to only apply the threshold to a specified region of interest in the image, in which case you can use BufferedImage’s getSubimage() method to return a rectangular sub section of the image specified by coordinates given as parameters. It returns the sub image as a buffered image, so the following code would be appropriate:

WritableRaster wraster = buffimage.getSubimage(x0, y0, x1, y1).getRaster();

Now to perform the threshold - assuming the threshold values were retrieved from the JSliders and stored in an array called rgbval[].

for(int band = 0; band
< 3; band++) {
 if(wraster.getSample(x, y, band) < rgbval[band])
  
  wraster.setSample(x, y, band, 0);
 else wraster.setSample(x, y, band, 255); }

This code loops through each color band, checking to see if the specified sample is above or below the respective threshold value and sets the sample to either 0 or 255 depending on the conditional. The method getSample() is inherited from WritableRaster’s parent class, Raster. The code above is to be executed for each pixel (x,y) in the WritableRaster object. You will need to write a loop bounded by the image heighth and width that takes care of incrementing the x and y variables and executing the above code.

That is the meat of the code for performing a threshold on an RGB image. It should be noted that the Java Advanced Imaging(JAI) API contains many helpful classes that perform various imaging operations, including thresholding, but it is still important to know how to code such operations yourself, instead of merely using the supplied API classes. There are many instances where you can optimize a task to fit your specifications but only if you write it yourself. It will also give you a better understanding of how the API code works, so you will be able to use it more quickly and effectively.



About the Author:
iEntry provides free highly informative newsletters for web developers, IT professionals and small business owners. We deliver 50 million email newsletters per month and have over 4,000,000 unique opt-in subscribers. From our extensive range of email newsletters we can provide you with a selection of newsletters that best meet your interests.

More articles_application_development Articles

SecurityProNews RSS Feed SecurityProNews RSS Feed


Get Your Site Submitted for Free in the World's Largest B2B Directory!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

iEntry Featured Services: Jayde Member Services | Forums | Freeware | Advertise with Us

Virus Warnings

Subscribe to
SecurityProNews FREE!



[ more newsletters ]

article resources
Search Articles:
[advanced search]

WebProWorld.com
Get in-touch with industry experts and leaders
Post your site for review by expert and peers
Ask Security, IT, Development and Design questions

Free Membership: Join Now!

Visit WebProWorld.com

Titan Quest Forum
The #1 Titan Quest forum
Halo 3 Forum
The best Halo, Halo 2, Halo 3 forum
Nintendo Wii
Nintendo Wii news and views
Mac Software
The best in OS X freeware
Graphics Forum
Your source for graphic tutorials
SecurityProNews.com | Breaking eBusiness News Get Your IT Questions Answered - Click Here SecurityProNews News Feeds