White-Border Removal Toolbox

When you add figures in your publication, you may need to remove the white border (probably generated by MATLAB) in order to save your valuable space. If there are a lot of them, it would be super tedious to crop images manually one by one. Yes, this happens to me, and I feel sick about it. So, I develop MATLAB code to remove the borders automatically, smartly and yet controllably. And this is the look of the function.

epsilon = 1e-2;

island_coor_row = 2; % row

island_coor_col = 1; % col

fn_remove_white_border(imagename,imageext, epsilon, island_coor_row, island_coor_col);

The code can be downloaded here.

The original image with undesired white background.

The figure is divided into 'islands', rectangular blocks containing non-white pixels. In this example, there are 10 islands; the row and column are divided into 2 and 5 respectively. (The 4th column is hard too see..sorry for that :-P). The user can pick which of the islands to output as a cropped image. For instance, if I pick row = 2, column = 1, the program will crop and output the mountain picture as the figure below.

The output image by picking row=2, and column = 1.

epsilon = 1e-2;

island_coor_row = 2; % row

island_coor_col = 1; % col

epsilon = [];

fn_remove_white_border(imagename,imageext, epsilon);

which gives the same thing as

fn_remove_white_border(imagename,imageext);

Using the smart option, the epsilon will be suggested automatically by the program according to the image. When row and column number are not specified, the program will smart enough to output the biggest island available.

epsilon = 1e-2;

island_coor_row = 0; % row

island_coor_col = 0; % col

When picking 0 for row and column, the program will eliminate only the outer-most white bounding box and keep the rest.

Tips

  1. The bigger the epsilon is, the more islands are produced by the program.
  2. If the cropped image still contains some white border --> increase the epsilon. If the image gets chopped too much and gets too small --> decrease the epsilon.
  3. epsilon = 1e-4; % recommended for gray-scale image with > 80% white pixel in the frame
  4. epsilon = 1e-2; % recommended for color image