Tuesday, November 5, 2024

Thresholding An Image In Java

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.

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

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles