Exemple #1
0
    def _button_fine_orient_fired(self):
        """
        fine tuning of ORI and ADDPAR

        """
        scale = 5000

        if self.need_reset:
            self.reset_show_images()
            self.need_reset = 0

        # backup the ORI/ADDPAR files first
        self.backup_ori_files()

        op = par.OrientParams()
        op.read()

        # recognized names for the flags:
        names = [
            'cc', 'xh', 'yh', 'k1', 'k2', 'k3', 'p1', 'p2', 'scale', 'shear'
        ]
        op_names = [
            op.cc, op.xh, op.yh, op.k1, op.k2, op.k3, op.p1, op.p2, op.scale,
            op.shear
        ]

        flags = []
        for name, op_name in zip(names, op_names):
            if (op_name == 1):
                flags.append(name)

        for i_cam in range(self.n_cams):  # iterate over all cameras

            if self.epar.Combine_Flag:

                self.status_text = "Multiplane calibration."
                """ Performs multiplane calibration, in which for all cameras the 
                pre-processed planes in multi_plane.par combined.
                Overwrites the ori and addpar files of the cameras specified 
                in cal_ori.par of the multiplane parameter folder
                """

                all_known = []
                all_detected = []

                for i in range(self.MultiParams.n_planes
                               ):  # combine all single planes

                    # c = self.calParams.img_ori[i_cam][-9] # Get camera id
                    c = re.findall('\\d+', self.calParams.img_ori[i_cam])[
                        0]  # not all ends with a number

                    file_known = self.MultiParams.plane_name[i] + c + '.tif.fix'
                    file_detected = self.MultiParams.plane_name[
                        i] + c + '.tif.crd'

                    # Load calibration point information from plane i
                    try:
                        known = np.loadtxt(file_known)
                        detected = np.loadtxt(file_detected)
                    except:
                        raise IOError("reading {} or {} failed".format(
                            file_known, file_detected))

                    if np.any(detected == -999):
                        raise ValueError(
                            ("Using undetected points in {} will cause " +
                             "silliness. Quitting.").format(file_detected))

                    num_known = len(known)
                    num_detect = len(detected)

                    if num_known != num_detect:
                        raise ValueError("Number of detected points (%d) does not match" +\
                        " number of known points (%d) for %s, %s" % \
                        (num_known, num_detect, file_known, file_detected))

                    if len(all_known) > 0:
                        detected[:,
                                 0] = all_detected[-1][-1, 0] + 1 + np.arange(
                                     len(detected))

                    # Append to list of total known and detected points
                    all_known.append(known)
                    all_detected.append(detected)

                # Make into the format needed for full_calibration.
                all_known = np.vstack(all_known)[:, 1:]
                all_detected = np.vstack(all_detected)

                # this is the main difference in the multiplane mode
                # that we fill the targs and cal_points by the
                # combined information

                targs = TargetArray(len(all_detected))
                for tix in range(len(all_detected)):
                    targ = targs[tix]
                    det = all_detected[tix]

                    targ.set_pnr(tix)
                    targ.set_pos(det[1:])

                self.cal_points = np.empty(
                    (all_known.shape[0], )).astype(dtype=[('id',
                                                           'i4'), ('pos',
                                                                   '3f8')])
                self.cal_points['pos'] = all_known
            else:
                targs = self.sorted_targs[i_cam]

            try:
                residuals, targ_ix, err_est = full_calibration(self.cals[i_cam], self.cal_points['pos'], \
                                                           targs, self.cpar, flags)
            except:
                raise ValueError("full calibration failed\n")
            # save the results
            self._write_ori(i_cam, addpar_flag=True)

            # Plot the output
            # self.reset_plots()

            x, y = [], []
            for r, t in zip(residuals, targ_ix):
                if t != -999:
                    pos = targs[t].pos()
                    x.append(pos[0])
                    y.append(pos[1])

            self.camera[i_cam]._plot.overlays = []
            self.drawcross("orient_x",
                           "orient_y",
                           x,
                           y,
                           'orange',
                           5,
                           i_cam=i_cam)

            # self.camera[i]._plot_data.set_data(
            #     'imagedata', self.ori_img[i].astype(np.float))
            # self.camera[i]._img_plot = self.camera[
            #     i]._plot.img_plot('imagedata', colormap=gray)[0]
            self.camera[i_cam].drawquiver(x, y,
                                          x + scale * residuals[:len(x), 0],
                                          y + scale * residuals[:len(x), 1],
                                          "red")
            # self.camera[i]._plot.index_mapper.range.set_bounds(0, self.h_pixel)
            # self.camera[i]._plot.value_mapper.range.set_bounds(0, self.v_pixel)

        self.status_text = "Orientation finished."
Exemple #2
0
    def _reload(self):
        # print("reloading")
        # self.__init__(self)
        # load ptv_par
        ptvParams = par.PtvParams(path=self.par_path)
        ptvParams.read()

        # read picture size parameters
        self.h_image_size = ptvParams.imx
        self.v_image_size = ptvParams.imy
        self.h_pixel_size = ptvParams.pix_x
        self.v_pixel_size = ptvParams.pix_y
        self.img_cal = ptvParams.img_cal
        if ptvParams.allCam_flag:
            self.pair_enable_flag = False
        else:
            self.pair_enable_flag = True

        # unesed parameters

        self.n_img = ptvParams.n_img
        self.img_name = ptvParams.img_name
        self.hp_flag = np.bool(ptvParams.hp_flag)
        self.allCam_flag = np.bool(ptvParams.allCam_flag)
        self.mmp_n1 = ptvParams.mmp_n1
        self.mmp_n2 = ptvParams.mmp_n2
        self.mmp_n3 = ptvParams.mmp_n3
        self.mmp_d = ptvParams.mmp_d

        # read_calibration parameters
        calOriParams = par.CalOriParams(self.n_img, path=self.par_path)
        calOriParams.read()
        (fixp_name, img_cal_name, img_ori, tiff_flag, pair_flag, chfield) = \
            (calOriParams.fixp_name, calOriParams.img_cal_name, calOriParams.img_ori,
             calOriParams.tiff_flag, calOriParams.pair_flag, calOriParams.chfield)

        for i in range(self.n_img):
            exec("self.cam_{0} = calOriParams.img_cal_name[{1}]".format(
                i + 1, i))
            exec("self.ori_cam_{0} = calOriParams.img_ori[{1}]".format(
                i + 1, i))

        self.tiff_head = np.bool(tiff_flag)
        self.pair_head = np.bool(pair_flag)
        self.fixp_name = fixp_name
        if chfield == 0:
            self.chfield = "Frame"
        elif chfield == 1:
            self.chfield = "Field odd"
        else:
            self.chfield = "Field even"

        # read detect plate parameters
        detectPlateParams = par.DetectPlateParams(path=self.par_path)
        detectPlateParams.read()

        (gv_th1, gv_th2, gv_th3, gv_th4, tolerable_discontinuity, min_npix, max_npix, min_npix_x,
            max_npix_x, min_npix_y, max_npix_y, sum_of_grey, size_of_crosses) = \
            (detectPlateParams.gvth_1, detectPlateParams.gvth_2, detectPlateParams.gvth_3, detectPlateParams.gvth_4,
             detectPlateParams.tol_dis, detectPlateParams.min_npix, detectPlateParams.max_npix, detectPlateParams.min_npix_x,
             detectPlateParams.max_npix_x, detectPlateParams.min_npix_y, detectPlateParams.max_npix_y, detectPlateParams.sum_grey,
             detectPlateParams.size_cross)

        for i in range(self.n_img):
            exec('self.grey_value_treshold_{0} = gv_th{0}'.format(i + 1))

        self.tolerable_discontinuity = tolerable_discontinuity
        self.min_npix = min_npix
        self.min_npix_x = min_npix_x
        self.min_npix_y = min_npix_y
        self.max_npix = max_npix
        self.max_npix_x = max_npix_x
        self.max_npix_y = max_npix_y
        self.sum_of_grey = sum_of_grey
        self.size_of_crosses = size_of_crosses

        # read manual orientaion parameters
        manOriParams = par.ManOriParams(self.n_img, [], path=self.par_path)
        manOriParams.read()

        for i in range(self.n_img):
            for j in range(4):  # 4 points per image
                exec(f"self.img_{i+1}_p{j+1} = manOriParams.nr[{i*4+j}]")

        # examine arameters
        examineParams = par.ExamineParams(path=self.par_path)
        examineParams.read()
        (self.Examine_Flag, self.Combine_Flag) = (examineParams.Examine_Flag,
                                                  examineParams.Combine_Flag)

        # orientation parameters
        orientParams = par.OrientParams(path=self.par_path)
        orientParams.read()
        (po_num_of_ori, cc, xh, yh, k1, k2, k3, p1, p2, scale, shear, interf) = \
            (orientParams.pnfo, orientParams.cc, orientParams.xh, orientParams.yh, orientParams.k1, orientParams.k2, orientParams.k3,
             orientParams.p1, orientParams.p2, orientParams.scale, orientParams.shear, orientParams.interf)

        self.point_number_of_orientation = po_num_of_ori
        self.cc = np.bool(cc)
        self.xh = np.bool(xh)
        self.yh = np.bool(yh)
        self.k1 = np.bool(k1)
        self.k2 = np.bool(k2)
        self.k3 = np.bool(k3)
        self.p1 = np.bool(p1)
        self.p2 = np.bool(p2)
        self.scale = np.bool(scale)
        self.shear = np.bool(shear)
        self.interf = np.bool(interf)

        dumbbellParams = par.DumbbellParams(path=self.par_path)
        dumbbellParams.read()
        (self.dumbbell_eps, self.dumbbell_scale, self.dumbbell_gradient_descent,
            self.dumbbell_penalty_weight, self.dumbbell_step, self.dumbbell_niter) = \
            (dumbbellParams.dumbbell_eps, dumbbellParams.dumbbell_scale,
             dumbbellParams.dumbbell_gradient_descent, dumbbellParams.dumbbell_penalty_weight,
             dumbbellParams.dumbbell_step, dumbbellParams.dumbbell_niter)

        shakingParams = par.ShakingParams(path=self.par_path)
        shakingParams.read()
        (self.shaking_first_frame, self.shaking_last_frame,
         self.shaking_max_num_points,
         self.shaking_max_num_frames) = (shakingParams.shaking_first_frame,
                                         shakingParams.shaking_last_frame,
                                         shakingParams.shaking_max_num_points,
                                         shakingParams.shaking_max_num_frames)
Exemple #3
0
def py_multiplanecalibration(exp):
    """ Performs multiplane calibration, in which for all cameras the pre-processed plane in multiplane.par al combined.
		Overwrites the ori and addpar files of the cameras specified in cal_ori.par of the multiplane parameter folder
        """

    for i_cam in range(exp.n_cams):  # iterate over all cameras
        all_known = []
        all_detected = []
        for i in range(exp.MultiParams.n_planes):  # combine all single planes

            c = exp.calParams.img_ori[i_cam][-9]  # Get camera id

            file_known = exp.MultiParams.plane_name[i] + str(c) + '.tif.fix'
            file_detected = exp.MultiParams.plane_name[i] + str(c) + '.tif.crd'

            # Load calibration point information from plane i
            known = np.loadtxt(file_known)
            detected = np.loadtxt(file_detected)

            if np.any(detected == -999):
                raise ValueError(
                    ("Using undetected points in {} will cause " +
                     "silliness. Quitting.").format(file_detected))

            num_known = len(known)
            num_detect = len(detected)

            if num_known != num_detect:
                raise ValueError("Number of detected points (%d) does not match" +\
                " number of known points (%d) for %s, %s" % \
                (num_known, num_detect, file_known, file_detected))

            if len(all_known) > 0:
                detected[:, 0] = all_detected[-1][-1, 0] + 1 + np.arange(
                    len(detected))

            # Append to list of total known and detected points
            all_known.append(known)
            all_detected.append(detected)

        # Make into the format needed for full_calibration.
        all_known = np.vstack(all_known)[:, 1:]
        all_detected = np.vstack(all_detected)

        targs = TargetArray(len(all_detected))
        for tix in range(len(all_detected)):
            targ = targs[tix]
            det = all_detected[tix]

            targ.set_pnr(tix)
            targ.set_pos(det[1:])

        # backup the ORI/ADDPAR files first
        exp.backup_ori_files()

        op = par.OrientParams()
        op.read()

        # recognized names for the flags:
        names = [
            'cc', 'xh', 'yh', 'k1', 'k2', 'k3', 'p1', 'p2', 'scale', 'shear'
        ]
        op_names = [
            op.cc, op.xh, op.yh, op.k1, op.k2, op.k3, op.p1, op.p2, op.scale,
            op.shear
        ]

        flags = []
        for name, op_name in zip(names, op_names):
            if (op_name == 1):
                flags.append(name)

        # Run the multiplane calibration
        residuals, targ_ix, err_est = full_calibration(exp.cals[0], all_known,
                                                       targs, exp.cpar, flags)

        #Save the results
        exp._write_ori(i_cam,
                       addpar_flag=True)  # addpar_flag to save addpar file
        print('End multiplane')
Exemple #4
0
    def closed(self, info, is_ok):
        calibParams = info.object
        par_path = calibParams.par_path
        print("inside CalHandler ", par_path)
        Handler.closed(self, info, is_ok)
        if is_ok:
            img_cal_name = [
                calibParams.cam_1, calibParams.cam_2, calibParams.cam_3,
                calibParams.cam_4
            ]
            img_ori = [
                calibParams.ori_cam_1, calibParams.ori_cam_2,
                calibParams.ori_cam_3, calibParams.ori_cam_4
            ]
            nr1 = [
                calibParams.img_1_p1, calibParams.img_1_p2,
                calibParams.img_1_p3, calibParams.img_1_p4
            ]
            nr2 = [
                calibParams.img_2_p1, calibParams.img_2_p2,
                calibParams.img_2_p3, calibParams.img_2_p4
            ]
            nr3 = [
                calibParams.img_3_p1, calibParams.img_3_p2,
                calibParams.img_3_p3, calibParams.img_3_p4
            ]
            nr4 = [
                calibParams.img_4_p1, calibParams.img_4_p2,
                calibParams.img_4_p3, calibParams.img_4_p4
            ]

            nr = [nr1, nr2, nr3, nr4]

            if (calibParams.chfield == "Frame"):
                chfield = 0
            elif (calibParams.chfield == "Field odd"):
                chfield = 1
            else:
                chfield = 2
            par.PtvParams(calibParams.n_img,
                          calibParams.img_name,
                          calibParams.img_cal,
                          calibParams.hp_flag,
                          calibParams.allCam_flag,
                          calibParams.tiff_head,
                          calibParams.h_image_size,
                          calibParams.v_image_size,
                          calibParams.h_pixel_size,
                          calibParams.v_pixel_size,
                          chfield,
                          calibParams.mmp_n1,
                          calibParams.mmp_n2,
                          calibParams.mmp_n3,
                          calibParams.mmp_d,
                          path=par_path).write()

            par.CalOriParams(calibParams.n_img,
                             calibParams.fixp_name,
                             img_cal_name,
                             img_ori,
                             calibParams.tiff_head,
                             calibParams.pair_head,
                             chfield,
                             path=par_path).write()

            par.DetectPlateParams(calibParams.grey_value_treshold_1,
                                  calibParams.grey_value_treshold_2,
                                  calibParams.grey_value_treshold_3,
                                  calibParams.grey_value_treshold_4,
                                  calibParams.tolerable_discontinuity,
                                  calibParams.min_npix,
                                  calibParams.max_npix,
                                  calibParams.min_npix_x,
                                  calibParams.max_npix_x,
                                  calibParams.min_npix_y,
                                  calibParams.max_npix_y,
                                  calibParams.sum_of_grey,
                                  calibParams.size_of_crosses,
                                  path=par_path).write()

            par.ManOriParams(calibParams.n_img, nr, path=par_path).write()
            par.ExamineParams(calibParams.Examine_Flag,
                              calibParams.Combine_Flag,
                              path=par_path).write()
            par.OrientParams(calibParams.point_number_of_orientation,
                             calibParams.cc,
                             calibParams.xh,
                             calibParams.yh,
                             calibParams.k1,
                             calibParams.k2,
                             calibParams.k3,
                             calibParams.p1,
                             calibParams.p2,
                             calibParams.scale,
                             calibParams.shear,
                             calibParams.interf,
                             path=par_path).write()
            par.ShakingParams(calibParams.shaking_first_frame,
                              calibParams.shaking_last_frame,
                              calibParams.shaking_max_num_points,
                              calibParams.shaking_max_num_frames,
                              path=par_path).write()

            par.DumbbellParams(calibParams.dumbbell_eps,
                               calibParams.dumbbell_scale,
                               calibParams.dumbbell_gradient_descent,
                               calibParams.dumbbell_penalty_weight,
                               calibParams.dumbbell_step,
                               calibParams.dumbbell_niter,
                               path=par_path).write()