def cut_rects(img, rects):
    global img_idx

    label_img = op.get_subimage(img, rects[0][0],rects[0][1])
    if not op.same_img(sub_imgs[0], label_img):
        return 
    for i, rect in enumerate(rects):
        if i==0:
            continue
        img2 = op.get_subimage(img, rect[0], rect[1])
#        dist = mc.diff(sub_imgs[i], img2)
#        if mc.average(dist) > 20:
        imgs = nc.get_single_numbers(img2)
        for number in imgs:
            cv2.imwrite('data/train5/'+str(img_idx)+'.jpg', number)
            img_idx += 1
def cut_rects(img, rects):
    global img_idx

    label_img = op.get_subimage(img, rects[0][0], rects[0][1])
    if not op.same_img(sub_imgs[0], label_img):
        return
    for i, rect in enumerate(rects):
        if i == 0:
            continue
        img2 = op.get_subimage(img, rect[0], rect[1])
        #        dist = mc.diff(sub_imgs[i], img2)
        #        if mc.average(dist) > 20:
        imgs = nc.get_single_numbers(img2)
        for number in imgs:
            cv2.imwrite('data/train5/' + str(img_idx) + '.jpg', number)
            img_idx += 1
def on_mouse(event, x, y, flags, param):
    global point_s
    global img
    global rects
    green = [0x0, 0xFF, 0x0]
    read = [0x0, 0x0, 0xFF]
    if event == cv2.cv.CV_EVENT_LBUTTONDOWN:
        img_cpy = img.copy()  # copy image to draw rectangle
        point_s = (x, y)
        cv2.circle(img_cpy, point_s, 1, green)
        cv2.imshow(win_name, img_cpy)
    elif event == cv2.cv.CV_EVENT_RBUTTONDOWN:
        print 'cancel last region'
        if len(rects) != 0:
            rects.pop()
            sub_imgs.pop()
    elif event == cv2.cv.CV_EVENT_MOUSEMOVE and (
            flags & cv2.cv.CV_EVENT_FLAG_LBUTTON):
        img_cpy = img.copy()  # copy image to draw rectangle
        point_e = (x, y)
        if point_s != point_e:
            cv2.circle(img_cpy, point_e, 2, green)  # draw point
            cv2.rectangle(img_cpy, point_s, point_e, read)
            cv2.imshow(win_name, img_cpy)
    elif event == cv2.cv.CV_EVENT_LBUTTONUP:
        point_e = (x, y)
        img_cpy = img.copy()  # copy image to draw rectangle
        if point_s != point_e:
            cv2.circle(img_cpy, point_e, 2, green)
            cv2.rectangle(img_cpy, point_s, point_e, read)
            cv2.imshow(win_name, img_cpy)
            img2 = op.get_subimage(img, point_s, point_e)
            #            img2 = cv2.resize(img2,(26,26))
            #            cv2.imwrite('heihei.jpg',img2)
            rects.append((point_s, point_e))
            sub_imgs.append(img2)
            print 'add region', point_s, point_e
def on_mouse(event, x, y, flags, param):
    global point_s
    global img
    global rects
    green = [0x0,0xFF,0x0]
    read =  [0x0,0x0,0xFF]
    if event == cv2.cv.CV_EVENT_LBUTTONDOWN:
        img_cpy = img.copy() # copy image to draw rectangle
        point_s = (x,y)
        cv2.circle(img_cpy, point_s, 1, green)
        cv2.imshow(win_name,img_cpy)
    elif event == cv2.cv.CV_EVENT_RBUTTONDOWN:
        print 'cancel last region'
        if len(rects) != 0:
            rects.pop()
            sub_imgs.pop()
    elif event == cv2.cv.CV_EVENT_MOUSEMOVE and (flags & cv2.cv.CV_EVENT_FLAG_LBUTTON):
        img_cpy = img.copy() # copy image to draw rectangle
        point_e = (x,y)
        if point_s != point_e:
            cv2.circle(img_cpy, point_e, 2, green) # draw point
            cv2.rectangle(img_cpy, point_s, point_e, read)
            cv2.imshow(win_name,img_cpy)
    elif event == cv2.cv.CV_EVENT_LBUTTONUP:
        point_e = (x,y)
        img_cpy = img.copy() # copy image to draw rectangle
        if point_s != point_e:
            cv2.circle(img_cpy, point_e, 2, green)
            cv2.rectangle(img_cpy, point_s, point_e, read)
            cv2.imshow(win_name,img_cpy)
            img2 = op.get_subimage(img, point_s, point_e)
#            img2 = cv2.resize(img2,(26,26))
#            cv2.imwrite('heihei.jpg',img2)
            rects.append((point_s,point_e))
            sub_imgs.append(img2)
            print 'add region', point_s, point_e