Monday, April 17, 2017

Local Thresholding from Zxing Library (Java)

Although OpenCV has its own local threshold method, such as adaptiveThreshold, I was looking for something a bit sophisticated and better. After some search, I came across Zxing's HybridBinarizer class that does the job much better than simple adaptiveThreshold from OpenCV. So, below is a very rough code to make use of this excellent library in Java. If you want to do this in C++, refer to this tutorial.


import com.google.zxing.Binarizer;
import com.google.zxing.LuminanceSource;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import java.io.InputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayInputStream;
public class Test {
public static void main( String[] args ) {
File imgPath = new File( args[0] );
try {
BufferedImage bufferedImage = ImageIO.read(imgPath);
byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
LuminanceSource source = new RGBLuminanceSource(bufferedImage.getWidth(), bufferedImage.getHeight(), getPixelArray(bufferedImage));
BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer( source ));
BitMatrix bitMatrix = bitmap.getBlackMatrix();
for (int row=0; row<bitMatrix.getHeight(); row++) {
for (int col=0; col<bitMatrix.getWidth(); col++) {
for (int channels=0; channels<3; channels++) {
int idx = (col + bitMatrix.getWidth() * row)*3 + channels;
if (bitMatrix.get(col, row)) {
pixels[idx] = 0;
} else {
pixels[idx] = -1;
}
}
}
}
ImageIO.write(bufferedImage, "jpg", new File("binary_output.jpg"));
} catch (Throwable t) {
System.out.println(t.getMessage());
}
}
private static int[] getPixelArray(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
int[] result = new int[height*width];
if (hasAlphaChannel) {
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int idx = col + row*width;
int argb = 0;
argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
argb += ((int) pixels[pixel + 1] & 0xff); // blue
argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
result[idx] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
} else {
final int pixelLength = 3;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int argb = 0;
int idx = col + row*width;
argb += -16777216; // 255 alpha
argb += ((int) pixels[pixel] & 0xff); // blue
argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
result[idx] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
}
return result;
}
}
view raw Test.java hosted with ❤ by GitHub

I admit that the code above is a bit messy, but it is just for testing out to see this actually works. In writing the code, I would like to acknowledge the following especially helpful references.

To compile the file and run it, first, create Test.java with the code above. Then, download Zxing's core library file (most recent version at the time of writing this is core-3.3.0.jar) form here. Then, run the following, assuming the jar file is in the same directory.

$ javac Test.java -cp ".:core-3.3.0.jar"
$ java -cp ".:core-3.3.0.jar" Test image_to_test.jpg

This will output the local thresholded image binary_output.jpg to the same folder. If you want to know more about java and javac -cp option, refer to this tutorial.

No comments:

Post a Comment