make the code compile on OpenCV 2.4; fixed random crash in the beginning