More accurate implementation of cv::format