Update to 2.0.0 tree from current Fremantle build
[opencv] / tests / swig_python / highgui / match.py
1 """
2 This script will compare tho images and decides with a threshold
3 if these to images are "equal enough"
4 """
5
6 # import the necessary things for OpenCV
7 from cv import *
8 from highgui import *
9
10 import frames
11 import sys
12 import os
13
14 PREFIX=os.path.join(os.environ["srcdir"],"../../opencv_extra/testdata/python/images/")
15
16
17 DisplayImages=False
18
19 if DisplayImages:
20         videowindow="video"
21         referencewindow="reference"
22         cvNamedWindow(videowindow,CV_WINDOW_AUTOSIZE)
23         cvNamedWindow(referencewindow,CV_WINDOW_AUTOSIZE)
24
25 # returns True/False if match/non-match
26 def match( image, index, thres ):
27
28 # load image from comparison set
29         QCIFcompare=cvLoadImage(PREFIX+frames.QCIF[index])
30
31         if QCIFcompare is None:
32                 print "Couldn't open image "+PREFIX+frames.QCIF[index]+" for comparison!"
33                 sys.exit(1)
34
35 # resize comparison image to input image dimensions
36         size=cvSize(image.width,image.height)
37         compare=cvCreateImage(size,IPL_DEPTH_8U,image.nChannels)
38         cvResize(QCIFcompare,compare)
39
40 # compare images
41         diff=cvNorm( image, compare, CV_RELATIVE_L2 )
42
43         if DisplayImages:
44                 cvShowImage(videowindow,image)
45                 cvShowImage(referencewindow,compare)
46                 if diff<=thres:
47                         cvWaitKey(200)
48                 else:
49                         print "index==",index,": max==",thres," is==",diff
50                         cvWaitKey(5000)
51
52         cvReleaseImage(QCIFcompare)
53         cvReleaseImage(compare)
54
55         if diff<=thres:
56                 return True
57         else:
58                 return False
59
60