Median Filter a bi-level image with JAI
- by Mark
I'd like to apply a Median Filter to a bi-level image and output a bi-level image. The JAI median filter seems to output an RGB image, which I'm having trouble downconverting back to bi-level.
Currently I can't even get the image back into gray color-space, my code looks like this:
BufferedImage src; // contains a bi-level image
ParameterBlock pb = new ParameterBlock();
pb.addSource(src);
pb.add(MedianFilterDescriptor.MEDIAN_MASK_SQUARE);
pb.add(3);
RenderedOp result = JAI.create("MedianFilter", pb);
ParameterBlock pb2 = new ParameterBlock();
pb2.addSource(result);
pb2.add(new double[][]{{0.33, 0.34, 0.33, 0}});
RenderedOp grayResult = JAI.create("BandCombine", pb2);
BufferedImage foo = grayResult.getAsBufferedImage();
This code hangs on the grayResult line and appears not to return. I assume that I'll eventually need to call the "Binarize" operation in JAI.
Edit: Actually, the code appears to be stalling once I call getAsBufferedImage(), but returns nearly instantly when the second operation ("BandCombine") is removed.
Is there a better way to keep the Median Filtering in the source color domain? If not, how do I downconvert back to binary?