I've changed the getMatRef to be called only once (I though the method was just a cast of the data). 
I don't see why the .at() operator must be used, as it's slower + it's a per pixel operation. If you insist, I'll change it.