Is there a coder-supported alternative to imsubtract?

9 views (last 30 days)
Hello,
I am moving some Matlab code from working in Matlab to working in C through the Matlab Coder. I use the function imsubtract to see the difference between two photos. I was curious if anyone knows the best way to develop an alternative to imsubtract that is coder-supported? Also, I am working with Matlab 2014b
Thanks

Accepted Answer

Cam Salzberger
Cam Salzberger on 31 Oct 2018
Hello Blaine,
imsubtract is a fairly simple function, though robust to deal with the different types of images it may be fed. If you already know information on your images, you can simplify its use to apply to just your images. This basic code should be coder-compatible.
For example, if you know that you'll have only uint8 integer images, you can just do:
Z = X-Y;
If you have a double image, then you may have to deal with out of bounds issues (since double images are supposed to be between 0 and 1):
Z = X-Y;
Z(Z < 0) = 0;
Of course, all this also assumes valid input, and that X and Y are the same size (or scalar), which imsubtract would normally take care of. So if you're concerned about the input, you may need to write your own error checking.
  2 Comments
Blaine Minden
Blaine Minden on 1 Nov 2018
Thanks. It worked. I have noticed a slightly under 10% increase in run times. Any ideas as to why just changing that line of code would cause that?
Cam Salzberger
Cam Salzberger on 15 Nov 2018
If you are simply doing the subtraction, I would not expect an increase in on-going runtime. If you have to do the out-of-bounds checking afterwards, it's an additional operation that may be done in a more efficient way in the internal code.
Make sure that you are checking the runtime of your code in the correct way. The first time you run code after changing it, there will be an additional time for the compilation. Subsequent calls (assuming functions are cached) should take slightly less time. The timeit function is kind of nice in how it takes that into account. I'd also recommend using the profiler on non-first time runs too for a more fine-grained look at what is going on.

Sign in to comment.

More Answers (0)

Products


Release

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!