Exemplo n.º 1
0
        public bool NinePointCalMake(SourceBuffer _sourceBuffer, ExecuteBuffer _executeBuffer, out SourceBuffer outsourcebuffer, out ExecuteBuffer outexecutebuffer)
        {
            outsourcebuffer  = _sourceBuffer;
            outexecutebuffer = _executeBuffer;



            return(true);
        }
Exemplo n.º 2
0
        public bool NinePointCalMake(SourceBuffer _sourceBuffer, ExecuteBuffer _executeBuffer, out SourceBuffer outsourcebuffer, out ExecuteBuffer outexecutebuffer)
        {
            outsourcebuffer  = _sourceBuffer;
            outexecutebuffer = _executeBuffer;
            m      = Convert.ToDouble(txt_m.Text.ToString());
            n      = Convert.ToDouble(txt_n.Text.ToString());
            WorldX = new HTuple(0, m, m, 0, -m, -m, -m, 0, m);
            WorldY = new HTuple(0, 0, -n, -n, -n, 0, n, n, n);
            for (int i = 0; i < x_point.Count; i++)
            {
                HOperatorSet.TupleConcat(temp_row, (HTuple)x_point[i], out temp_row_last);
                temp_row = temp_row_last;
                HOperatorSet.TupleConcat(temp_column, (HTuple)y_point[i], out temp_column_last);
                temp_column = temp_column_last;
            }
            try
            {
                HOperatorSet.VectorToHomMat2d(temp_column, temp_row, WorldY, WorldX, out PixToWorld);
                HOperatorSet.VectorToHomMat2d(WorldY, WorldX, temp_column, temp_row, out WorldToPixel);
            }
            catch (Exception)
            {
                MessageBox.Show("点位错误,请确认匹配是否正确");
                return(false);
            }
            //保存PixelToWorld到本地
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Title            = "PixelToWorld保存";
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string strPath = sfd.FileName;
                if (!strPath.EndsWith(".tup"))
                {
                    strPath += ".tup";
                }
                Match match = Regex.Match(strPath, "(\\w+)(.tup)$");
                HOperatorSet.WriteTuple(PixToWorld, strPath);
                bool cPixToWorld_add = true;
                foreach (Info_Source keybuffcontrol in _sourceBuffer._s_ControlBuffer.Keys)
                {
                    if (keybuffcontrol.Name == match.Groups[1].ToString())
                    {
                        cPixToWorld_add = false;
                        break;
                    }
                }
                Info_Source control_add_key = new Info_Source();
                control_add_key.Name = match.Groups[1].ToString() + ".tuple";
                control_add_key.Path = strPath;
                control_add_key.Type = "tuple";
                if (cPixToWorld_add)
                {
                    _sourceBuffer._s_ControlBuffer.Add(control_add_key, PixToWorld);
                }
                else
                {
                    _sourceBuffer._s_ControlBuffer[control_add_key] = PixToWorld;
                }



                if (_executeBuffer.controlBuffer.ContainsKey(match.Groups[1].ToString()))
                {
                    _executeBuffer.controlBuffer[match.Groups[1].ToString()] = PixToWorld;
                }
                else
                {
                    _executeBuffer.controlBuffer.Add(match.Groups[1].ToString(), PixToWorld);
                }
            }

            //保存WorldToPixel到本地
            SaveFileDialog sfd2 = new SaveFileDialog();

            sfd2.Title            = "WorldToPixel保存";
            sfd2.RestoreDirectory = true;
            if (sfd2.ShowDialog() == DialogResult.OK)
            {
                string strPath = sfd2.FileName;
                if (!strPath.EndsWith(".tup"))
                {
                    strPath += ".tup";
                }
                Match match = Regex.Match(strPath, "(\\w+)(.tup)$");
                HOperatorSet.WriteTuple(WorldToPixel, strPath);
                bool WorldToPixel_add = true;
                foreach (Info_Source keybuffcontrol in _sourceBuffer._s_ControlBuffer.Keys)
                {
                    if (keybuffcontrol.Name == match.Groups[1].ToString())
                    {
                        WorldToPixel_add = false;
                        break;
                    }
                }
                Info_Source control_add_key = new Info_Source();
                control_add_key.Name = match.Groups[1].ToString() + ".tuple";
                control_add_key.Path = strPath;
                control_add_key.Type = "tuple";
                if (WorldToPixel_add)
                {
                    _sourceBuffer._s_ControlBuffer.Add(control_add_key, WorldToPixel);
                }
                else
                {
                    _sourceBuffer._s_ControlBuffer[control_add_key] = WorldToPixel;
                }



                if (_executeBuffer.controlBuffer.ContainsKey(match.Groups[1].ToString()))
                {
                    _executeBuffer.controlBuffer[match.Groups[1].ToString()] = WorldToPixel;
                }
                else
                {
                    _executeBuffer.controlBuffer.Add(match.Groups[1].ToString(), WorldToPixel);
                }
            }
            outsourcebuffer  = _sourceBuffer;
            outexecutebuffer = _executeBuffer;
            MessageBox.Show("9点标定成功");

            return(true);
        }
Exemplo n.º 3
0
        public bool CalibrationMake(SourceBuffer _sourceBuffer, ExecuteBuffer _executeBuffer, out SourceBuffer outsourcebuffer, out ExecuteBuffer outexecutebuffer, HWndCtrl hWndCtrl, out string result_info)
        {
            outsourcebuffer  = _sourceBuffer;
            outexecutebuffer = _executeBuffer;
            result_info      = "";
            HTuple  hv_PixelSize = 0.00001;
            HTuple  hv_Message = new HTuple(), hv_Row = new HTuple();
            HTuple  hv_Column = new HTuple(), hv_Index = new HTuple();
            HTuple  hv_Error = null;
            HObject ho_Contours = null, ho_Cross = null, hv_outimage = null;
            HTuple  hv_HomMat3DIdentity = null;
            HTuple  hv_cp1Hur1 = null, hv_cam2Hcp2 = null, hv_cam2Hul2 = null;
            HTuple  hv_PoseNewOrigin2 = null, hv_ULX = null, hv_ULY = null, hv_PoseNewOrigin = null;

            double[] Get_actual_pixel = new double[7];
            HTuple   WorldPixelx, WorldPixely;
            HTuple   Heightx = new HTuple(), Widthx = new HTuple();

            Heightx.Append(Height / 2);
            Heightx.Append(Height / 2);
            Heightx.Append(Height / 2 + 1);
            Widthx.Append(Width / 2);
            Widthx.Append(Width / 2 + 1);
            Widthx.Append(Width / 2);
            HTuple WorldLength1, WorldLength2;
            HTuple ScaleForSimilarPixelSize = 0;

            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out hv_outimage);

            if (!_executeBuffer.imageBuffer.ContainsKey(this.Input_image.SelectedItem.ToString() + ".img"))
            {
                MessageBox.Show("二值化处理: 输入图像已经不存在,请重置设置输入图像");
                result_info = " 二值化处理: 输入图像已经不存在,请重置设置输入图像";
                return(false);
            }
            if (_executeBuffer.imageBuffer[this.Input_image.SelectedItem.ToString() + ".img"] == null)
            {
                MessageBox.Show("二值化处理: image参数为空或者未赋值");
                result_info = " 二值化处理: 输入图像已经不存在,请重置设置输入图像";
                return(false);
            }
            string comboBox_imageoutname = this.Output_image.Text.ToString() + ".img";

            if (_executeBuffer.imageBuffer[comboBox_imageoutname] != null)
            {
                if (_executeBuffer.imageBuffer[comboBox_imageoutname].IsInitialized())
                {
                    _executeBuffer.imageBuffer[comboBox_imageoutname].Dispose();
                }
            }
            HTuple width, height;
            int    m = _executeBuffer.imageBuffer[this.Input_image.SelectedItem.ToString() + ".img"].CountObj();

            HOperatorSet.GetImageSize(_executeBuffer.imageBuffer[this.Input_image.SelectedItem.ToString() + ".img"], out width, out height);
            int number = Convert.ToInt32(this.textBox1.Text.ToString());

            if (!this.checkBox1.Checked)
            {
                if (Get_Cal_pixel)
                {
                    Get_Cal_pixel = false;
                }
                if (number == 0)
                {
                    hv_StartCamPar = new HTuple();
                    if (this.checkBox_focalize.Checked)
                    {
                        hv_StartCamPar.Append(0);
                    }
                    else
                    {
                        hv_StartCamPar.Append(Convert.ToDouble(this.textbox_lens.Text.ToString()) / 100);
                    }
                    hv_StartCamPar.Append(0);
                    hv_StartCamPar.Append(Convert.ToDouble(this.textBox_width.Text.ToString()) / 10000000);
                    hv_StartCamPar.Append(Convert.ToDouble(this.textBox_height.Text.ToString()) / 10000000);
                    hv_StartCamPar.Append(width / 2);
                    hv_StartCamPar.Append(height / 2);
                    hv_StartCamPar.Append(width);
                    hv_StartCamPar.Append(height);
                    hv_CalTabDescrFile = this.textBox_file.Text;
                    HOperatorSet.CreateCalibData("calibration_object", 1, 1, out CalibDataID);
                    if (this.checkBox_focalize.Checked)
                    {
                        HOperatorSet.SetCalibDataCamParam(CalibDataID, 0, "area_scan_telecentric_division", hv_StartCamPar);
                    }
                    else
                    {
                        HOperatorSet.SetCalibDataCamParam(CalibDataID, 0, "area_scan_division", hv_StartCamPar);
                    }
                    HOperatorSet.SetCalibDataCalibObject(CalibDataID, 0, hv_CalTabDescrFile);
                }
                try
                {
                    HOperatorSet.FindCalibObject(_executeBuffer.imageBuffer[this.Input_image.SelectedItem.ToString() + ".img"], CalibDataID, 0, 0, number, new HTuple(), new HTuple());
                    HOperatorSet.GetCalibData(CalibDataID, "camera", 0, "init_params", out hv_StartCamPar);
                    HOperatorSet.GetCalibDataObservPoints(CalibDataID, 0, 0, number, out hv_Row, out hv_Column, out hv_Index, out hv_Pose);
                    HOperatorSet.GetCalibDataObservContours(out ho_Contours, CalibDataID, "caltab", 0, 0, number);
                    HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row, hv_Column, 6, 0.785398);
                    hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, "orange");
                    hWndCtrl.addIconicVar(ho_Cross);
                    hWndCtrl.addIconicVar(ho_Contours);
                    hWndCtrl.repaint();
                }
                catch
                {
                    MessageBox.Show("找不到标定板");
                    ho_Contours.Dispose();
                    ho_Cross.Dispose();

                    return(false);
                }

                if (number == hv_NumImages - 1)
                {
                    Read_file = false;
                    HOperatorSet.CalibrateCameras(CalibDataID, out hv_Error);
                    HOperatorSet.GetCalibData(CalibDataID, "camera", 0, "params", out hv_CamParam);
                    HOperatorSet.GetCalibData(CalibDataID, "calib_obj_pose", (new HTuple(0)).TupleConcat(6), "pose", out hv_Pose);
                    HOperatorSet.HomMat3dIdentity(out hv_HomMat3DIdentity);
                    HOperatorSet.HomMat3dRotateLocal(hv_HomMat3DIdentity, ((-(hv_Pose.TupleSelect(5)))).TupleRad(), "z", out hv_cp1Hur1);
                    HOperatorSet.PoseToHomMat3d(hv_Pose, out hv_cam2Hcp2);
                    HOperatorSet.HomMat3dCompose(hv_cam2Hcp2, hv_cp1Hur1, out hv_cam2Hul2);
                    HOperatorSet.HomMat3dToPose(hv_cam2Hul2, out hv_PoseNewOrigin2);
                    HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_PoseNewOrigin2, 0, 0, "m", out hv_ULX, out hv_ULY);
                    HOperatorSet.SetOriginPose(hv_PoseNewOrigin2, hv_ULX, hv_ULY, 0, out hv_PoseNewOrigin);

                    HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_Pose, Heightx, Widthx, 1, out WorldPixelx, out WorldPixely);
                    HOperatorSet.DistancePp(WorldPixely[0], WorldPixelx[0], WorldPixely[1], WorldPixelx[1], out WorldLength1);
                    HOperatorSet.DistancePp(WorldPixely[0], WorldPixelx[0], WorldPixely[2], WorldPixelx[2], out WorldLength2);
                    ScaleForSimilarPixelSize = (WorldLength1 + WorldLength2) / 2;
                    HOperatorSet.GenImageToWorldPlaneMap(out ho_MapSingle1, hv_CamParam,
                                                         hv_PoseNewOrigin, width, height, width, height, ScaleForSimilarPixelSize, "bilinear");
                    HOperatorSet.WriteObject(ho_MapSingle1, this.textBox2.Text.ToString());
                    Read_file              = true;
                    Get_Calibration        = true;
                    this.checkBox1.Checked = true;
                    result_info            = " 校正参数: 设置完成";
                }

                this.textBox1.Text = (number + 1).ToString();
            }

            if (this.checkBox1.Checked)
            {
                if (Read_file)
                {
                    if (File.Exists(this.textBox2.Text))
                    {
                        HOperatorSet.ReadObject(out ho_MapSingle1, this.textBox2.Text);
                    }
                    else
                    {
                        MessageBox.Show("校正文件不存在 ,请检查");
                        return(false);
                    }
                }

                HOperatorSet.MapImage(_executeBuffer.imageBuffer[this.Input_image.SelectedItem.ToString() + ".img"], ho_MapSingle1, out hv_outimage);
                _executeBuffer.imageBuffer[comboBox_imageoutname] = hv_outimage;
                outexecutebuffer = _executeBuffer;
                result_info      = " 校正: 完成";
                HTuple Distance;
                if (!Get_Cal_pixel)
                {
                    try
                    {
                        double all_number = 0;
                        HOperatorSet.FindCalibObject(hv_outimage, CalibDataID, 0, 0, 0, new HTuple(), new HTuple());
                        HOperatorSet.GetCalibDataObservPoints(CalibDataID, 0, 0, 0, out hv_Row, out hv_Column, out hv_Index, out hv_Pose);
                        for (int i = 0; i < 7; i++)
                        {
                            HOperatorSet.DistancePp(hv_Row[i * 7], hv_Column[i * 7], hv_Row[i * 7 + 6], hv_Column[i * 7 + 6], out Distance);
                            Get_actual_pixel[i] = Distance.D;
                            all_number          = all_number + Get_actual_pixel[i];
                        }
                        Pixel_number          = (Convert.ToDouble(this.textBox_pixel.Text)) / (all_number / 7);
                        Get_Cal_pixel         = true;
                        this.label_pixel.Text = Pixel_number.ToString();
                        Pixel_Trans           = Convert.ToDouble(Pixel_number.ToString());
                    }
                    catch
                    {
                        MessageBox.Show("请再次添加图像寻找计算像素比");
                    }
                }
                if (Read_file)
                {
                    ho_MapSingle1.Dispose();
                }
            }



            return(true);
        }