예제 #1
0
def process_image(fn, cascade, extract_faces=True):
    img = cv2.imread(fn)
    h, w = img.shape[:2]
    scale = max(h, w) / 512.0
    small = cv2.resize(img, (int(w/scale), int(h/scale)), interpolation=cv2.INTER_AREA)
    rects = detect_turned(small, cascade)

    for i, (x1, y1, x2, y2, vx, vy) in enumerate(rects):
        cv2.rectangle(small, (x1, y1), (x2, y2), (0, 255, 0))
        cv2.circle(small, (x1, y1), 2, (0, 0, 255), -1)
        cv2.putText(small, str(i), ((x1+x2)/2, (y1+y2)/2), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 255, 0))

    rects = np.float32(rects).reshape(-1,6)
    rects[:,:4] = np.around(rects[:,:4]*scale)

    faces = []
    if extract_faces:
        path, name, ext = splitfn(fn)
        face_sz = 256
        for i, r in enumerate(rects):
            p1, p2, u = r.reshape(3, 2)
            v = np.float32( [-u[1], u[0]] )
            w = np.abs(p2-p1).max()
            fscale = w / face_sz
            p0 = 0.5*(p1+p2 - w*(u+v))
            M = np.float32([u*fscale, v*fscale, p0]).T
            face = cv2.warpAffine(img, M, (face_sz, face_sz), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_AREA)
            faces.append(face)

    return small, rects, faces
예제 #2
0
파일: calibrate.py 프로젝트: ArkaJU/opencv
    def processImage(fn):
        print('processing %s... ' % fn)
        img = cv.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            return None

        assert w == img.shape[1] and h == img.shape[0], ("size: %d x %d ... " % (img.shape[1], img.shape[0]))
        found, corners = cv.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1)
            cv.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if debug_dir:
            vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
            cv.drawChessboardCorners(vis, pattern_size, corners, found)
            _path, name, _ext = splitfn(fn)
            outfile = os.path.join(debug_dir, name + '_chess.png')
            cv.imwrite(outfile, vis)

        if not found:
            print('chessboard not found')
            return None

        print('           %s... OK' % fn)
        return (corners.reshape(-1, 2), pattern_points)
예제 #3
0
파일: demo.py 프로젝트: ArkaJU/opencv
    def __init__(self):
        root = tk.Tk()
        root.title('OpenCV Demo')

        self.win = win = tk.PanedWindow(root, orient=tk.HORIZONTAL, sashrelief=tk.RAISED, sashwidth=4)
        self.win.pack(fill=tk.BOTH, expand=1)

        left = tk.Frame(win)
        right = tk.Frame(win)
        win.add(left)
        win.add(right)

        scrollbar = tk.Scrollbar(left, orient=tk.VERTICAL)
        self.demos_lb = demos_lb = tk.Listbox(left, yscrollcommand=scrollbar.set)
        scrollbar.config(command=demos_lb.yview)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        demos_lb.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)

        self.samples = {}
        for fn in glob('*.py'):
            name = splitfn(fn)[1]
            if fn[0] != '_' and name not in exclude_list:
                self.samples[name] = fn

        for name in sorted(self.samples):
            demos_lb.insert(tk.END, name)

        demos_lb.bind('<<ListboxSelect>>', self.on_demo_select)

        self.cmd_entry = cmd_entry = tk.Entry(right)
        cmd_entry.bind('<Return>', self.on_run)
        run_btn = tk.Button(right, command=self.on_run, text='Run', width=8)

        self.text = text = ScrolledText(right, font=('arial', 12, 'normal'), width = 30, wrap='word')
        self.linker = _linker = LinkManager(text, self.on_link)
        self.text.tag_config("header1", font=('arial', 14, 'bold'))
        self.text.tag_config("header2", font=('arial', 12, 'bold'))
        text.config(state='disabled')

        text.pack(fill='both', expand=1, side=tk.BOTTOM)
        cmd_entry.pack(fill='x', side='left' , expand=1)
        run_btn.pack()
예제 #4
0
    def run(self):
        for fn in self.img_names:
            print('processing %s... ' % fn, end='')
            img = cv2.imread(fn, 0)
            if img is None:
                print("Failed to load", fn)
                continue

            self.h, self.w = img.shape[:2]
            found, corners = cv2.findChessboardCorners(img, self.pattern_size)
            if found:
                term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
                cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

            if self.debug_dir:
                vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
                cv2.drawChessboardCorners(vis, self.pattern_size, corners, found)
                path, name, ext = splitfn(fn)
                outfile = self.debug_dir + name + '_chess.png'
                cv2.imwrite(outfile, vis)
                if found:
                    self.img_names_undistort.append(outfile)

            if not found:
                print('chessboard not found')
                continue

            self.img_points.append(corners.reshape(-1, 2))
            self.obj_points.append(self.pattern_points)

            print('ok')

        # calculate camera distortion
        rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(
            self.obj_points, self.img_points, (self.w, self.h), None, None)

        print(self.img_names_undistort)
        print("\nRMS:", rms)
        print("camera matrix:\n", camera_matrix)
        print("distortion coefficients: ", dist_coefs.ravel())

        return([self.img_names_undistort, camera_matrix, dist_coefs])
    obj_points = []
    img_points = []
    h, w = 0, 0
    for fn in img_names:
        print 'processing %s...' % fn,
        img = cv2.imread(fn, 0)
        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
        if not found:
            print 'chessboard not found'
            continue
        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print 'ok'
    



        # Draw and display the corners
        cv2.drawChessboardCorners(img, (9,6), corners,found)
        cv2.imshow('img',img)
        print('processing %s... ' % fn, end='')
        img = cv2.imread(os.path.join(proj_root, fn), 0)
        if img is None:
            print("Failed to load", fn)
            continue

        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if out:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            outfile = os.path.join(out, name + '_chess.png')
            cv2.imwrite(outfile, vis)
            if found:
                img_names_undistort.append(outfile)

        if not found:
            print('chessboard not found')
            continue

        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print('ok')

    # calculate camera distortion
예제 #7
0
def run():
    trivim_dir= open(os.path.join(home,"Trivim.txt")).readline()
    count=0
    os.chdir(trivim_dir)
    with open('camera_calibration\\value.txt', 'w') as myFile:
            myFile.write("0")
    f = open("camera_calibration\\path.txt","r")
                #Read whole file into data
    photoDir=f.read()
    path=photoDir+"\\*.jpg"
    print path

    args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
    args = dict(args)
    try: img_mask = img_mask[0]
    except: img_mask = path
    img_names = glob(img_mask)
    debug_dir = args.get('--debug')
    square_size = float(args.get('--square_size', 1.0))

    pattern_size = (9, 6)
    pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
    pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    obj_points = []
    img_points = []
    h, w = 0, 0
    width=3872
    flag=False
    
    for fn in img_names:
        count=count+1
        print count
        with open('camera_calibration\\value.txt', 'w') as myFile:
            myFile.write(str(count))
        print 'processing %s...' % fn,
        img = cv2.imread(fn, 0)
        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            if flag==False:
                inputFileName = os.path.join(photoDir, fn)
                photoHandle = Image.open(inputFileName)
                # get EXIF information as a dictionary
                exif = _getExif(photoHandle)
                if 'ExifImageWidth' in exif:
                    width = float(exif['ExifImageWidth'])
                    Flag=True
            term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
        if not found:
            print 'chessboard not found'
            continue
        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)
        print 'ok'

    try:
        rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h))

        f = open(r"camera_calibration\sensor_value.txt","r")
                #Read whole file into data
        value=f.read()
        value=value.split('-')
        sensor_value=float(value[len(value)-1])
        f.close()
        
        focalPixels=camera_matrix[0][0]
        focalLength=(focalPixels*sensor_value)/width;

        with open('camera_calibration\\calib_temp.txt', 'w') as myFile:
            myFile.write("Camera Name : "+str(value[0])+"\n")
            myFile.write("Sensor Width : "+str(sensor_value)+"\n")
            myFile.write("Root Mean Square(RMS) value : "+str(rms)+"\n")
            myFile.write("Distortion Coefficients : ")
            for log in dist_coefs.ravel():
                myFile.write(str(log)+"\t")
            myFile.write("\n"+"Camera Matrix : "+"\n")
            for log in camera_matrix:
                myFile.write(str(log)+"\n")
            myFile.write("Focal Length(mm) : " + str( focalLength )+"\n")
        with open('camera_calibration\\finish.txt', 'w') as myFile:
            myFile.write("finish")
    except:
        with open('camera_calibration\\finish.txt', 'w') as myFile:
            myFile.write("failed")  
    
    cv2.destroyAllWindows()
    cv2.destroyAllWindows()
예제 #8
0
        print('processing %s... ' % fn, end='')
        img = cv2.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            continue

        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            outfile = debug_dir + name + '_chess.png'
            cv2.imwrite(outfile, vis)
            if found:
                img_names_undistort.append(outfile)

        if not found:
            print('chessboard not found')
            continue

        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print('ok')

    # calculate camera distortion
예제 #9
0
        print('processing %s... ' % fn, end='')
        img = cv2.imread(os.path.join(proj_root, fn), 0)
        if img is None:
            print("Failed to load", fn)
            continue

        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if out:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            outfile = os.path.join(out, name + '_chess.png')
            cv2.imwrite(outfile, vis)
            if found:
                img_names_undistort.append(outfile)

        if not found:
            print('chessboard not found')
            continue

        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)
        # write to matrix to be used as input
        # img_points_np = np.array(img_points)
        # print(corners)
        ori_points = corners.reshape(7, 9, 2)
예제 #10
0
def main():
    import sys
    import getopt
    from glob import glob

    args, img_mask = getopt.getopt(sys.argv[1:], '',
                                   ['debug=', 'square_size=', 'threads='])
    args = dict(args)
    args.setdefault('--debug', './output/')
    args.setdefault('--square_size', 0.875)
    args.setdefault('--threads', 4)
    if not img_mask:
        img_mask = "*.jpg"  # default
    else:
        img_mask = img_mask[0]

    img_names = glob(img_mask)
    print(img_names)
    debug_dir = args.get('--debug')
    if debug_dir and not os.path.isdir(debug_dir):
        os.mkdir(debug_dir)
    square_size = float(args.get('--square_size'))

    pattern_size = (9, 6)
    pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
    pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    obj_points = []
    img_points = []
    h, w = cv.imread(img_names[0], cv.IMREAD_GRAYSCALE
                     ).shape[:2]  # TODO: use imquery call to retrieve results

    def processImage(fn):
        print('processing %s... ' % fn)
        img = cv.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            return None

        assert w == img.shape[1] and h == img.shape[0], (
            "size: %d x %d ... " % (img.shape[1], img.shape[0]))
        found, corners = cv.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1)
            cv.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if debug_dir:
            vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
            cv.drawChessboardCorners(vis, pattern_size, corners, found)
            _path, name, _ext = splitfn(fn)
            outfile = os.path.join(debug_dir, name + '_chess.png')
            cv.imwrite(outfile, vis)

        if not found:
            print('chessboard not found')
            return None

        print('           %s... OK' % fn)
        return (corners.reshape(-1, 2), pattern_points)

    threads_num = int(args.get('--threads'))
    if threads_num <= 1:
        chessboards = [processImage(fn) for fn in img_names]
    else:
        print("Run with %d threads..." % threads_num)
        from multiprocessing.dummy import Pool as ThreadPool
        pool = ThreadPool(threads_num)
        chessboards = pool.map(processImage, img_names)

    chessboards = [x for x in chessboards if x is not None]
    for (corners, pattern_points) in chessboards:
        img_points.append(corners)
        obj_points.append(pattern_points)

    # calculate camera distortion
    rms, camera_matrix, dist_coefs, _rvecs, _tvecs = cv.calibrateCamera(
        obj_points, img_points, (w, h), None, None)

    print("\nRMS:", rms)
    print("camera matrix:\n", camera_matrix)
    print("distortion coefficients: ", dist_coefs.ravel())

    # undistort the image with the calibration
    print('')
    for fn in img_names if debug_dir else []:
        _path, name, _ext = splitfn(fn)
        img_found = os.path.join(debug_dir, name + '_chess.png')
        outfile = os.path.join(debug_dir, name + '_undistorted.png')

        img = cv.imread(img_found)
        if img is None:
            continue

        h, w = img.shape[:2]
        newcameramtx, roi = cv.getOptimalNewCameraMatrix(
            camera_matrix, dist_coefs, (w, h), 1, (w, h))

        dst = cv.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)

        # crop and save the image
        x, y, w, h = roi
        dst = dst[y:y + h, x:x + w]

        print('Undistorted image written to: %s' % outfile)
        cv.imwrite(outfile, dst)

    print('Done')
예제 #11
0
def calib():
    import sys, getopt
    from glob import glob

    args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
    args = dict(args)
    img_mask = 'calib1/left*.jpg'
    img_names = glob(img_mask)
    debug_dir = args.get('--debug')
    square_size = float(args.get('--square_size', 1.0))

    pattern_size = (9, 6)#10X7
    pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
    pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    obj_points = []
    img_points = []
    h, w = 0, 0
    print 'image names: ',img_names
    for fn in img_names:
        print 'processing %s...' % fn,
        img = cv2.imread(fn, 0)
        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
        vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        cv2.drawChessboardCorners(vis, pattern_size, corners, found)
        path, name, ext = splitfn(fn)
        #cv2.imshow('corners',vis)
        #cv2.waitKey()
        #cv2.destroyAllWindows()
        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
        cv2.imshow('corners',vis)
        cv2.waitKey(25)
        cv2.destroyAllWindows()
        cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
        if not found:
            print 'chessboard not found'
            continue
        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print 'ok'

    rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h),None,None)
    print 'dist coefs: ',dist_coefs,len(dist_coefs),len(dist_coefs[0])
    raw_input()
    print w/2-0.5,h/2-0.5
    # axis = np.float32([[3,0,0],[0,3,0],[0,0,-3]]).reshape(-1,3);
    axis = np.float32([[0,0,0], [0,3,0], [3,3,0], [3,0,0],[0,0,-3],[0,3,-3],[3,3,-3],[3,0,-3] ])
    for fn in img_names:
        print 'processing %s...' % fn,
        img = cv2.imread(fn, 0)
        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        print 'corners: ',len(corners),len(corners[0])
        if found:
            term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
        pattern_points = np.zeros((w*h, 1, 3), np.float32) 
        pattern_points[:,:,:2] = np.mgrid[0:w,  0:h].T.reshape(-1,1,2)
        # print 'patt points: ',len(pattern_points),len(pattern_points[0])
        # print 'corners points: ',len(corners),len(corners[0])
        # raw_input()
        # rv,tv,inliers = cv2.solvePnPRansac(pattern_points,corners,camera_matrix,dist_coefs)
        # img_points,jac = cv2.projectPoints(axis,rv,tv,camera_matrix,dist_coefs)
        # img = draw(img,corners,img_points)    
            
        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
            if not found:
                print 'chessboard not found'
        continue
       # img_points.append(corners.reshape(-1, 2))
       # obj_points.append(pattern_points)
       
        print 'ok'
    

    return camera_matrix,dist_coefs
예제 #12
0
def run():
    trivim_dir = open(os.path.join(home, "Trivim.txt")).readline()
    count = 0
    os.chdir(trivim_dir)
    with open('camera_calibration\\value.txt', 'w') as myFile:
        myFile.write("0")
    f = open("camera_calibration\\path.txt", "r")
    #Read whole file into data
    photoDir = f.read()
    path = photoDir + "\\*.jpg"
    print path

    args, img_mask = getopt.getopt(sys.argv[1:], '',
                                   ['save=', 'debug=', 'square_size='])
    args = dict(args)
    try:
        img_mask = img_mask[0]
    except:
        img_mask = path
    img_names = glob(img_mask)
    debug_dir = args.get('--debug')
    square_size = float(args.get('--square_size', 1.0))

    pattern_size = (9, 6)
    pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
    pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    obj_points = []
    img_points = []
    h, w = 0, 0
    width = 3872
    flag = False

    for fn in img_names:
        count = count + 1
        print count
        with open('camera_calibration\\value.txt', 'w') as myFile:
            myFile.write(str(count))
        print 'processing %s...' % fn,
        img = cv2.imread(fn, 0)
        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            if flag == False:
                inputFileName = os.path.join(photoDir, fn)
                photoHandle = Image.open(inputFileName)
                # get EXIF information as a dictionary
                exif = _getExif(photoHandle)
                if 'ExifImageWidth' in exif:
                    width = float(exif['ExifImageWidth'])
                    Flag = True
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
        if not found:
            print 'chessboard not found'
            continue
        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)
        print 'ok'

    try:
        rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(
            obj_points, img_points, (w, h))

        f = open(r"camera_calibration\sensor_value.txt", "r")
        #Read whole file into data
        value = f.read()
        value = value.split('-')
        sensor_value = float(value[len(value) - 1])
        f.close()

        focalPixels = camera_matrix[0][0]
        focalLength = (focalPixels * sensor_value) / width

        with open('camera_calibration\\calib_temp.txt', 'w') as myFile:
            myFile.write("Camera Name : " + str(value[0]) + "\n")
            myFile.write("Sensor Width : " + str(sensor_value) + "\n")
            myFile.write("Root Mean Square(RMS) value : " + str(rms) + "\n")
            myFile.write("Distortion Coefficients : ")
            for log in dist_coefs.ravel():
                myFile.write(str(log) + "\t")
            myFile.write("\n" + "Camera Matrix : " + "\n")
            for log in camera_matrix:
                myFile.write(str(log) + "\n")
            myFile.write("Focal Length(mm) : " + str(focalLength) + "\n")
        with open('camera_calibration\\finish.txt', 'w') as myFile:
            myFile.write("finish")
    except:
        with open('camera_calibration\\finish.txt', 'w') as myFile:
            myFile.write("failed")

    cv2.destroyAllWindows()
    cv2.destroyAllWindows()
예제 #13
0
    import getopt
    from glob import glob
    from common import splitfn, image_extensions

    args, img_args = getopt.getopt(sys.argv[1:], '', ['cascade=', 'outdir='])
    args = dict(args)
    cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
    outdir = args.get('--outdir')
    
    img_list = []
    if len(img_args) == 0:
        img_list = ['../cpp/lena.jpg']
    else:
        for mask in img_args:
            img_list.extend(glob(mask))
    img_list = [fn for fn in img_list if splitfn(fn)[-1].lower() in image_extensions]

    cascade = cv2.CascadeClassifier(cascade_fn)

    for i, fn in enumerate(img_list):
        print '%d / %d   %s' % (i+1, len(img_list), fn),
        vis, rects, faces = process_image(fn, cascade)
        if len(faces) > 0 and outdir is not None:
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_all.bmp' % (outdir, name), vis)
            for face_i, face in enumerate(faces):
                cv2.imwrite('%s/%s_obj%02d.bmp' % (outdir, name, face_i), face)
        print ' - %d object(s) found' % len(faces)
        cv2.imshow('img', vis)
        cv2.waitKey(50)
    cv2.waitKey()
예제 #14
0
def calibrate(board_width, board_height, square_size, rectified_dir, debug_dir, save_file, image_patterns):
    import glob
    from common import splitfn

    img_names = glob.glob(image_patterns)

    pattern_size = (board_width, board_height)
    pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
    pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    if debug_dir:
        if not os.path.exists(debug_dir):
            os.makedirs(debug_dir)

    if rectified_dir:
        if not os.path.exists(rectified_dir):
            os.makedirs(rectified_dir)

    obj_points = []
    img_points = []
    good_images = []
    h, w = 0, 0
    for fn in img_names:
        print 'processing %s...' % fn,
        img = cv2.imread(fn, 0)
        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size,
                                                   flags=cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_NORMALIZE_IMAGE)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_chess.png' % (debug_dir, name), vis)

        if not found:
            print 'chessboard not found in %s' % (fn, )
            continue

        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)
        good_images.append(fn)

        print 'ok'

    rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h))
    print "RMS:", rms
    print "camera matrix:\n", camera_matrix
    print "distortion coefficients: ", dist_coefs.ravel()

    if save_file:
        with open(save_file, 'wt') as f:
            m = camera_matrix
            v = [m[0][0], m[1][1], m[0][2], m[1][2]] + dist_coefs[0].tolist()
            f.write(' '.join(map(str, v)))
            f.write('\n')
            f.write(' '.join(map(str, [w, h])))

    if rectified_dir:
        # new_camera_matrix, valid_roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h),
        #                                                              alpha=0.55, centerPrincipalPoint=False)
        # print "new camera matrix:\n", new_camera_matrix
        # if new_camera_matrix[0, 0] < 0:
        #     new_camera_matrix[0, 0] *= -1
        # map1, map2 = cv2.initUndistortRectifyMap(camera_matrix, dist_coefs, None,
        #                                          new_camera_matrix, (w, h),
        #                                          m1type=cv2.CV_16SC2)
        # for i in xrange(len(good_images)):
        #     fn = good_images[i]
        #     img = cv2.imread(fn, 0)
        #     r_img = cv2.remap(img, map1,map2, cv2.INTER_LINEAR, borderMode=cv2.BORDER_TRANSPARENT)
        #     _, name, _ = splitfn(fn)
        #     cv2.imwrite('%s/%s_rectified.png' % (rectified_dir, name), r_img)

        for i in xrange(len(good_images)):
            fn = good_images[i]
            img = cv2.imread(fn, 0)
            _, name, _ = splitfn(fn)
            r_img2 = cv2.undistort(img, camera_matrix, dist_coefs)
            cv2.imwrite('%s/%s_rectified2.png' % (rectified_dir, name), r_img2)

    cv2.destroyAllWindows()
예제 #15
0
def calibrateCamera(DIR): 

    args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size='])
    args = dict(args)
    args.setdefault('--debug', './output/')
    args.setdefault('--square_size', 1.0)
    if not img_mask:
        img_mask =str(DIR + 'image*.jpg')  # default
    else:
        img_mask = img_mask[0]

    img_names = glob(img_mask)
    debug_dir = args.get('--debug')
    if not os.path.isdir(debug_dir):
        os.mkdir(debug_dir)
    square_size = float(args.get('--square_size'))

    pattern_size = (9, 6)
    pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
    pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    obj_points = []
    img_points = []
    h, w = 0, 0
    img_names_undistort = []
    for fn in img_names:
        print('processing %s... ' % fn)#, end='')
        img = cv2.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            continue

        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            outfile = debug_dir + name + '_chess.png'
            cv2.imwrite(outfile, vis)
            if found:
                img_names_undistort.append(outfile)

        if not found:
            print('chessboard not found')
            continue

        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print('ok')

    # calculate camera distortion
    rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None)

    print("\nRMS:", rms)
    print("camera matrix:\n", camera_matrix)
    print("distortion coefficients: ", dist_coefs.ravel())

    # undistort the image with the calibration
    print('')
    for img_found in img_names_undistort:
        img = cv2.imread(img_found)

        h,  w = img.shape[:2]
        newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))

        dst = cv2.undistort(img, camera_matrix, dist_coefs, None, newcameramtx)

        # crop and save the image
        x, y, w, h = roi
        dst = dst[y:y+h, x:x+w]
        outfile = img_found + '_undistorted.png'
        print('Undistorted image written to: %s' % outfile)
        cv2.imwrite(outfile, dst)

    cv2.destroyAllWindows()


    return camera_matrix, dist_coefs, (w,h), obj_points, img_points