now uses bfsave instead of imwrite to circumvent memory problems