waylonion
waylonion

Reputation: 6976

Android Bitmap Downscaling

What is the best method to downscale a bitmap image on Android using Java if the goal is to preserve the quality of lines/edges, as much as possible? The intent is to use these downscaled images to train a neural network.

Each image will contain a single character (in this case Chinese). The picture below shows the original image produced on a View object, drawn by the user. My goal is to shrink this bitmap image to a resolution of 30 by 30 pixels.

I am currently using the Bitmap createScaledBitmap method at the moment but am wondering if there are any algorithms/methods that optimize for this kind of character downscaling.

drawView.buildDrawingCache();
Bitmap b = Bitmap.createScaledBitmap(drawView.getDrawingCache(), 30, 30, false);
saveImage(b);  // Saves image to disk.
drawView.destroyDrawingCache();

Another factor could be the relation between line width and image size overall. The line width is currently 20f. I've found that at 30f, I start to get large dark clumps and at 10f, I lose a lot of information when I downscale.

Yet another factor could be resulting image size. However, if I go too big, then the amount of computation it takes to train the network might become too large.

Original image.

Original Image

Here's the processed image - it doesn't look too bad, but I'm worried with other characters, there might not be enough variation for the neural network to learn.

Processed Image

Upvotes: 2

Views: 386

Answers (1)

pawel-schmidt
pawel-schmidt

Reputation: 1165

A first solution, coming into my mind was to vectorize this image and then redraw it again. You wrote that image is drawn by a user, so you even don't need to vectorize the image. All you need to do is save user input as a list of points and reproduce (draw) it in the desired scale.

User, drawing a line from (10, 10) to (20, 10) on canvas with size 100 x 100 px, will see line from (3, 3) to (6, 3) on thumbnail with size 30 x 30 px. Then you can also easily apply the line thickness depending on the final image scale.

Of course, for better performance, you should generate and cache some bitmaps instead of drawing them again and again when a user displays them.

Upvotes: 1

Related Questions