porting code to OpenCV