Q:

Removing Small Regions from a Binary Image

I need to remove all small contiguous regions that are smaller than a specified size in a binary image, equivalent to matlab's bwareaopen function. I have tried installing some libraries to do it but I didn't find a suitable solution.

A:

ImageJ's ParticleAnalyzer can segment contigious regions (= particles) according to their size. 
In order to get the necessary code, just run Plugins > Macros > Record... in the ImageJ GUI and switch the Record: mode to Java. Then run Analyze > Analyze Particles... and the recorder will show you the Java code necessary to reproduce the particle analysis.
import ij.IJ;
import ij.ImagePlus;

[...]

ImagePlus imp = // you have to know how to get your image
IJ.run(imp, "Analyze Particles...", "size=500-Infinity circularity=0.00-1.00 show=Masks in_situ");

This will replace the image in imp by the result image, equivalent to removing those particles outside the range specified by size=500-Infinity.
On a lower level, you can also use the ParticleAnalyzer class directly as follows:
import ij.ImagePlus;
import ij.measure.ResultsTable;
import ij.plugin.filter.ParticleAnalyzer;

[...]

ImagePlus imp = // you have to know how to get your image
ResultsTable rt = new ResultsTable();
Double min_size = 50.0;
Double max_size = Double.POSITIVE_INFINITY;
ParticleAnalyzer pa = new ParticleAnalyzer(ParticleAnalyzer.SHOW_MASKS + ParticleAnalyzer.IN_SITU_SHOW, 0, rt, min_size, max_size);
pa.analyze(imp);

You might need to set a threshold on your binary image before doing this.

