In the case it receives grayscale images, check if the clone of the incoming image is actually necessary. Probably not. Line: 140 *cvTempImage = toCVMat(inImage).clone();