Open wangm23456 opened 1 year ago
The first thing to check is whether opencv is using the GPU in this case or not.
OpenCV may well be doing something smarter, but one difference here is that you're using a 7x7 filter for OpenCV and a 15x15 filter for imageproc. OpenCV takes a width parameter that it requires to be odd, whereas imageproc takes a radius parameter and uses a filter of size 2*radius + 1 to ensure it's always odd. This is possibly a confusing API!
Having reminded myself of what the code does, the imageproc implementation is linear in kernel width so your accidental difference in parameters shouldn't account for much of the difference between the two libraries!
Quick empirical sanity check for this:
use std::time::SystemTime;
use image::{GrayImage, Luma};
use imageproc::filter::median_filter;
fn main() {
let image = GrayImage::from_fn(9720, 11340, |_, _| Luma([7]));
let t0 = SystemTime::now();
let _f1 = median_filter(&image, 3, 3);
let t1 = SystemTime::now();
let _f2 = median_filter(&image, 7, 7);
let t2 = SystemTime::now();
println!("{:?}", t1.duration_since(t0));
println!("{:?}", t2.duration_since(t1));
}
Ok(3.232656s) Ok(6.872397s)
That's still slower than your OpenCV run time, but far faster than your imageproc run time. I suspect you might be running your code in debug mode and so comparing a debug build of imageproc against a release build of OpenCV.
From a quick glance at the source code for OpenCV's implementation, it looks to be doing something roughly similar to what we do in imageproc, but with SIMD acceleration. I wouldn't be too surprised by a 5-10x difference in run time, but 200x looks wrong.
@wangm23456 can you check if you're running imageproc in debug or release mode.