def __init__ (self):
		self.li_m1=list()
		self.li_0=list()
		self.li_1=list()
		self.li_2=list()
		#path to the directory of the num's directories
		pathname = os.path.dirname(sys.argv[0])
		fullpath = os.path.abspath(pathname)
		dirs=glob.glob(fullpath+'/number_example/'+'*')
		if len(dirs)==0:
			raise TreatmentError('number_example folder not found, cannot proceed to recognition', 'recognition_number')

		for folder in dirs:

			im=cv2.imread(folder+'/-1.jpg',0)
			im=toBinary(im)
			self.li_m1.append(im)

			im=cv2.imread(folder+'/0.jpg', 0)
			im=toBinary(im)
			self.li_0.append(im)

			im=cv2.imread(folder+'/1.jpg', 0)
			im=toBinary(im)
			self.li_1.append(im)

			im=cv2.imread(folder+'/2.jpg', 0)
			im=toBinary(im)
			self.li_2.append(im)
	def track_number_callback(self, im, arg):

		pub, msg = arg
		target_pos=(400, 210)
		try:
			bridge = CvBridge()
		        #convert ROS image to opencv matrix
			cv_image = bridge.imgmsg_to_cv2(im, "bgr8")

			hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
		
			lowredl = np.array([0, 0, 255])
			lowredu = np.array([0, 255, 255])

			upredl = np.array([150, 0, 255])
			upredu = np.array([179, 255, 255])	
			
			mlow = cv2.inRange(hsv, lowredl, lowredu)
			mup = cv2.inRange(hsv, upredl, upredu)
			m=cv2.bitwise_or(mlow,mup)
		
			res=cv2.bitwise_and(cv_image,cv_image, mask= m)
		
			res=cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
			retval, binary=cv2.threshold(res,127,255,cv2.THRESH_BINARY)
			binary=toBinary(binary)
		
			red_pos=self.average_pixel_pos(binary)
			print red_pos
		
		except CvBridgeError,  e:
			rospy.loginfo(e)
			raise rospy.ServiceException("Tracking Failed")
	def execute(self, userdata):
	
		im=userdata.im_input
		
		#extracted the interessting part of the image (the one with the number)
		box, boderless=extraction(im)
		extracted=im[box[0]:box[2], box[1]:box[3]]
		
		#resizing the image to make an image with an area of 30000 pixels^2
		area=30000
		y, x=extracted.shape
		print x*y
		try:
			r=float(x)/y
			ny=int(np.sqrt(area/r))
		except ZeroDivisionError:
			print 'Wrong dimension of extraction: x=%d, y=%d'%(x,y)
			return 'fail'
		if x*y<400 and x*2>y:
			print 'Wrong area: x=%d, y=%d'%(x,y)
			return 'fail'

		nx=int(r*ny)

		extracted=cv2.resize(extracted, (nx, ny))
		
		#binary thresholding requested after resize
		extracted=toBinary(extracted)

		#Debuging print
		#cv2.imshow('Extracted', extracted)
		#cv2.waitKey(5)

		userdata.im_output=extracted
		return 'succeed'
	def execute(self, userdata):
	
		im=userdata.im_input
		#Debug visualization
		#cv2.imshow('Input', im)
		#cv2.waitKey(5)

		#working in the HSV color model to extract the red pixel
		hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
		
		#first threshold to red extraction
		lowredl = np.array([0, 0, 255])
		lowredu = np.array([0, 255, 255])

		#second threshold to red extraction
		upredl = np.array([150, 0, 255])
		upredu = np.array([179, 255, 255])		
		
		mlow = cv2.inRange(hsv, lowredl, lowredu)
		mup = cv2.inRange(hsv, upredl, upredu)


		#trying diffrent type of extraction, if the process failed three time, call the luminosity correction process
		if self.extraction_step==3:
			m=cv2.bitwise_or(mlow,mup)
			res=cv2.bitwise_and(im,im, mask= m)
		elif self.extraction_step==2:
			res=cv2.bitwise_and(im,im, mask= mup)
		elif self.extraction_step==1:
			res=cv2.bitwise_and(im,im, mask= mlow)
		elif self.extraction_step==0:
			if self.isGPRed:
				return 'fail_after_LC'
			else:
				self.extraction_step=3
				return 'fail'
		
		#make the thresholded image binary
		res=cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
		retval, res=cv2.threshold(res,127,255,cv2.THRESH_BINARY)
		res=toBinary(res)

		
		self.extraction_step-=1
		userdata.im_output=res
		return 'extracted'
def image_compare(im1, im2):
	try:
		y1, x1=im1.shape
		y2, x2=im2.shape
		
	except ValueError:
		print "Not a binary image"
		raise ValueError("Not a binary image")
		
	else:
		img2=cv2.resize(im2,( x1, y1))
		img2=toBinary(img2)

		hist = cv2.calcHist([im1],[0],None,[256],[0,256])

		img2[im1==img2]=255
		img2[im1!=img2]=0

		hist2 = cv2.calcHist([img2],[0],None,[256],[0,256])
		return 	float(hist2[255])/hist[255]*100
		hist2 = cv2.calcHist([img2],[0],None,[256],[0,256])
		return 	float(hist2[255])/hist[255]*100
	



#compare each image in l with im
def mult_image_compare(l, im):
	average=0.;
	for i in l:
		try:
			average+=image_compare(i, im)
			
		except ValueError, e:
			print 'Error in mult_image_compare: ', e
			raise ValueError(e)
			

	return average/len(l)

        

if __name__=='__main__':

	f='resultCV/final_test2.jpg'
	im=cv2.imread(f, 0)
	im=toBinary(im)

	ic=ImageComparator()
	print ic.identify(im)