//更改参数再次分割图片
 private void button4_Click(object sender, EventArgs e)
 {
     if (comboBox1.Text == "grabcut" && value1.Text == "迭代次数:" && value2.Text == "分割算子:")
     {
         try
         {
             int          iter_count  = int.Parse(textBox5.Text);
             GrabCutModes grabcutmode = (GrabCutModes)(Enum.Parse(typeof(GrabCutModes), comboBox4.Text));
             int          x           = int.Parse(textBox7.Text);
             int          y           = int.Parse(textBox8.Text);
             int          w           = int.Parse(textBox9.Text);
             int          h           = int.Parse(textBox10.Text);
             if (iter_count > 20 || iter_count < 0)
             {
                 MessageBox.Show("迭代次数于0-20次之内较为合适");
             }
             else
             {
                 System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)pictureBox1.Image;
                 Mat    image     = BitmapConverter.ToMat(bitmap);
                 double startTime = Cv2.GetTickCount();
                 Mat    res       = grabCut(image, iter_count, grabcutmode, x, y, w, h);
                 double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                 showImage(res, duration);
                 showParameters("迭代次数:", iter_count.ToString(), "分割算子:", grabcutmode.ToString());
                 textBox6.Hide();
                 showRect(x, y, w, h);
             }
         }
         catch (Exception err)
         {
             if (err.ToString().Contains("totalSampleCount"))
             {
                 MessageBox.Show("您选择的区域无法分割出前景,无法分割,请重新选择区域大小");
             }
             else if (err.ToString().Contains("mask"))
             {
                 MessageBox.Show("掩模mask为空,无法分割");
             }
             else if (err.ToString().Contains("Format"))
             {
                 MessageBox.Show("输入的字符串格式不正确,请重新输入");
             }
         }
     }
     if (comboBox1.Text == "watershed" && value1.Text == "中值滤波内核:" && value2.Text == "形态学卷积核:")
     {
         try
         {
             if (int.Parse(textBox5.Text) % 2 == 0)
             {
                 MessageBox.Show("中值滤波内核应为奇数,请重新输入。");
             }
             else
             {
                 int meadianblur_ksize = int.Parse(textBox5.Text);
                 if (meadianblur_ksize > 1000 || meadianblur_ksize < 0)
                 {
                     MessageBox.Show("数值不合理,请重新输入适当的数值");
                 }
                 else
                 {
                     string   text                = textBox6.Text;
                     string   diff11              = text.Replace("(", string.Empty);
                     string   diff12              = diff11.Replace(")", string.Empty);
                     string[] str                 = diff12.Split(',');
                     Size     element_size        = new Size(int.Parse(str[0]), int.Parse(str[1]));
                     System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)pictureBox1.Image;
                     Mat    image                 = BitmapConverter.ToMat(bitmap);
                     double startTime             = Cv2.GetTickCount();
                     Mat    result                = waterShed(image, meadianblur_ksize, element_size);
                     double duration              = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                     showImage(result, duration);
                     showParameters("中值滤波内核:", meadianblur_ksize.ToString(), "形态学卷积核:", "(" + element_size.Width.ToString() + "," + element_size.Height.ToString() + ")");
                 }
             }
         }
         catch (Exception err)
         {
             if (err.ToString().Contains("Format"))
             {
                 MessageBox.Show("输入的字符串格式不正确,请重新输入");
             }
             else if (err.ToString().Contains("索引超出了数组界限"))
             {
                 MessageBox.Show("形态学卷积内核应用英文括号隔开");
             }
             else
             {
                 MessageBox.Show(err.ToString());
             }
         }
     }
     if (comboBox1.Text == "meanshift" && value1.Text == "颜色域半径:" && value2.Text == "空间域半径:")
     {
         try
         {
             int meanshift_sp = int.Parse(textBox5.Text);
             int meanshift_sr = int.Parse(textBox6.Text);
             if (meanshift_sp < 0 || meanshift_sr < 0 || meanshift_sp > 1000 || meanshift_sr > 1000)
             {
                 MessageBox.Show("参数有误或数值过大,请重新输入数据");
             }
             else
             {
                 System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)pictureBox1.Image;
                 Mat    image     = BitmapConverter.ToMat(bitmap);
                 double startTime = Cv2.GetTickCount();
                 Mat    res       = meanShift(image, meanshift_sp, meanshift_sr);
                 double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                 showImage(res, duration);
                 showParameters("颜色域半径:", meanshift_sp.ToString(), "空间域半径:", meanshift_sr.ToString());
             }
         }
         catch (Exception err)
         {
             if (err.ToString().Contains("Format"))
             {
                 MessageBox.Show("输入的字符串格式不正确,请重新输入");
             }
             else
             {
                 MessageBox.Show(err.ToString());
             }
         }
     }
     if (comboBox1.Text == "floodfill" && value1.Text == "像素最大下行差异值:" && value2.Text == "像素最大上行差异值:")
     {
         try
         {
             string   diff1  = textBox5.Text;
             string   diff11 = diff1.Replace("[", string.Empty);
             string   diff12 = diff11.Replace("]", string.Empty);
             string[] str1   = diff12.Split(',');
             Scalar   lodiff = new Scalar(int.Parse(str1[0]), int.Parse(str1[1]), int.Parse(str1[2]), int.Parse(str1[3]));
             string   diff2  = textBox6.Text;
             string   diff21 = diff2.Replace("[", string.Empty);
             string   diff22 = diff21.Replace("]", string.Empty);
             string[] str2   = diff22.Split(',');
             int      x      = int.Parse(textBox7.Text);
             int      y      = int.Parse(textBox8.Text);
             int      w      = int.Parse(textBox9.Text);
             int      h      = int.Parse(textBox10.Text);
             Scalar   updiff = new Scalar(int.Parse(str2[0]), int.Parse(str2[1]), int.Parse(str2[2]), int.Parse(str2[3]));
             System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)pictureBox1.Image;
             Mat    image     = BitmapConverter.ToMat(bitmap);
             double startTime = Cv2.GetTickCount();
             Mat    res       = floodFill(image, lodiff, updiff, x, y, w, h);
             double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
             showImage(res, duration);
             showRect(x, y, w, h);
             showParameters("像素最大下行差异值:", lodiff.ToString(), "像素最大上行差异值:", updiff.ToString());
         }
         catch (Exception err)
         {
             if (err.ToString().Contains("Format"))
             {
                 MessageBox.Show("输入的字符串格式不正确,请重新输入(应输入英文逗号)");
             }
             else if (err.ToString().Contains("索引超出了数组界限"))
             {
                 MessageBox.Show("像素最大下(上)行差异值应用英文括号隔开");
             }
             else
             {
                 MessageBox.Show(err.ToString());
             }
         }
     }
     if (comboBox1.Text == "contour" && value1.Text == "高斯核x方向标准差:" && value2.Text == "二值化算子:")
     {
         try
         {
             ThresholdTypes        contour_type = (ThresholdTypes)(Enum.Parse(typeof(ThresholdTypes), comboBox4.Text));
             int                   sigmax       = int.Parse(textBox5.Text);
             System.Drawing.Bitmap bitmap       = (System.Drawing.Bitmap)pictureBox1.Image;
             Mat                   image        = BitmapConverter.ToMat(bitmap);
             double                startTime    = Cv2.GetTickCount();
             Mat                   res          = contourSeg(image, contour_type, sigmax);
             double                duration     = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
             showImage(res, duration);
             showParameters("高斯核x方向标准差:", sigmax.ToString(), "二值化算子:", contour_type.ToString());
             textBox6.Hide();
         }
         catch (Exception err)
         {
             if (err.ToString().Contains("Format"))
             {
                 MessageBox.Show("输入的字符串格式不正确,请重新输入");
             }
             else if (err.ToString().Contains("索引超出了数组界限"))
             {
                 MessageBox.Show("索引超出了数组界限,无法分割");
             }
             else
             {
                 MessageBox.Show(err.ToString());
             }
         }
     }
 }
 //图像分割操作
 private void button2_Click(object sender, EventArgs e)
 {
     panel1.Hide();
     value1.Hide();
     value2.Hide();
     textBox5.Hide();
     textBox6.Hide();
     button4.Hide();
     comboBox4.Hide();
     textBox1.Text = string.Empty;
     textBox2.Text = string.Empty;
     textBox3.Text = string.Empty;
     textBox4.Text = string.Empty;
     if (comboBox1.Text == string.Empty)
     {
         MessageBox.Show("请先选择您所需要的算法。");
     }
     else
     {
         OpenFileDialog openFileDialog = new OpenFileDialog();
         openFileDialog.Filter = "图片文件|*.bmp;*.ico;*.jpeg;*.jpg;*.png;*.tif;*.tiff";
         openFileDialog.ShowDialog();
         string fileName1 = openFileDialog.FileName;
         if (fileName1 != string.Empty)
         {
             button4.Show();
             pictureBox1.ImageLocation = fileName1;
             Mat image     = Cv2.ImRead(fileName1);
             Mat grayImage = new Mat();
             Mat binImage  = new Mat();
             //自适应阈值分割函数
             if (comboBox1.Text == "adaptiveThreshold")
             {
                 if (comboBox2.Text == "THRESH_BINARY")
                 {
                     if (comboBox3.Text == "ADAPTIVE_THRESH_MEAN_C")
                     {
                         double startTime = Cv2.GetTickCount();
                         AdaptiveThreshold(image, grayImage, binImage, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary);//自适应阈值分割函数
                         double duration = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                         showImage(binImage, duration);
                     }
                     if (comboBox3.Text == "ADAPTIVE_THRESH_GAUSSIAN_C")
                     {
                         double startTime = Cv2.GetTickCount();
                         AdaptiveThreshold(image, grayImage, binImage, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary);//自适应阈值分割函数
                         double duration = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                         showImage(binImage, duration);
                     }
                 }
                 if (comboBox2.Text == "THRESH_BINARY_INV")
                 {
                     if (comboBox3.Text == "ADAPTIVE_THRESH_MEAN_C")
                     {
                         double startTime = Cv2.GetTickCount();
                         AdaptiveThreshold(image, grayImage, binImage, AdaptiveThresholdTypes.MeanC, ThresholdTypes.BinaryInv);//自适应阈值分割函数
                         double duration = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                         showImage(binImage, duration);
                     }
                     if (comboBox3.Text == "ADAPTIVE_THRESH_GAUSSIAN_C")
                     {
                         double startTime = Cv2.GetTickCount();
                         AdaptiveThreshold(image, grayImage, binImage, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.BinaryInv);//自适应阈值分割函数                                                                                                    //算法结束
                         double duration = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                         showImage(binImage, duration);
                     }
                 }
                 else if (comboBox2.Text == string.Empty || comboBox3.Text == string.Empty)
                 {
                     button4.Hide();
                     MessageBox.Show("请选择参数一和参数二再进行分割");
                 }
                 panel1.Hide();
                 value1.Hide();
                 value2.Hide();
                 textBox5.Hide();
                 textBox6.Hide();
                 button4.Hide();
             }
             //grabcut函数迭代五次
             else if (comboBox1.Text == "grabcut")
             {
                 comboBox2.Text = string.Empty;
                 comboBox3.Text = string.Empty;
                 try
                 {
                     double startTime = Cv2.GetTickCount();
                     Mat    res       = grabCut(image, ITER_COUNT, GRABCUTMODE, 20, 30, image.Cols - (20 << 1), image.Rows - 30);
                     double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                     showImage(res, duration);
                     showParameters("迭代次数:", ITER_COUNT.ToString(), "分割算子:", GRABCUTMODE.ToString());
                     textBox6.Hide();
                     comboBox4.Show();
                     comboBox4.Items.Clear();
                     comboBox4.Text = GRABCUTMODE.ToString();
                     comboBox4.Items.Add("InitWithRect");
                     comboBox4.Items.Add("InitWithMask");
                     comboBox4.Items.Add("Eval");
                 }
                 catch (Exception err)
                 {
                     if (err.ToString().Contains("totalSampleCount"))
                     {
                         MessageBox.Show("您选择的区域无法分割出前景,无法分割,请重新选择区域大小");
                     }
                     else
                     {
                         MessageBox.Show(err.ToString());
                     }
                 }
             }
             //分水岭
             else if (comboBox1.Text == "watershed")
             {
                 comboBox2.Text = string.Empty;
                 comboBox3.Text = string.Empty;
                 try
                 {
                     double startTime = Cv2.GetTickCount();
                     Mat    result    = waterShed(image, MEADIANBlUR_KSIZE, ELEMENT_SIZE);
                     double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                     showImage(result, duration);
                     showParameters("中值滤波内核:", MEADIANBlUR_KSIZE.ToString(), "形态学卷积核:", "(" + ELEMENT_SIZE.Width.ToString() + "," + ELEMENT_SIZE.Height.ToString() + ")");
                 }
                 catch (Exception err)
                 {
                     MessageBox.Show(err.ToString());
                 }
             }
             //均值漂移
             else if (comboBox1.Text == "meanshift")
             {
                 comboBox2.Text = string.Empty;
                 comboBox3.Text = string.Empty;
                 try
                 {
                     double startTime = Cv2.GetTickCount();
                     Mat    res       = meanShift(image, MEANSHIFT_SP, MEANSHIFT_SR);
                     double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                     showImage(res, duration);
                     showParameters("颜色域半径:", MEANSHIFT_SP.ToString(), "空间域半径:", MEANSHIFT_SR.ToString());
                 }
                 catch (Exception err)
                 {
                     MessageBox.Show(err.ToString());
                 }
             }
             //漫水填充分割
             else if (comboBox1.Text == "floodfill")
             {
                 comboBox2.Text = string.Empty;
                 comboBox3.Text = string.Empty;
                 try
                 {
                     double startTime = Cv2.GetTickCount();
                     Mat    res       = floodFill(image, LODIFF, UPDIFF, 20, 30, image.Cols - (20 << 1), image.Rows - 30);
                     double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                     showImage(res, duration);
                     showParameters("像素最大下行差异值:", LODIFF.ToString(), "像素最大上行差异值:", UPDIFF.ToString());
                 }
                 catch (Exception err)
                 {
                     MessageBox.Show(err.ToString());
                 }
             }
             //通过轮廓分割图像
             else if (comboBox1.Text == "contour")
             {
                 comboBox2.Text = string.Empty;
                 comboBox3.Text = string.Empty;
                 try
                 {
                     double startTime = Cv2.GetTickCount();
                     Mat    res       = contourSeg(image, CONTOUR_TYPE, SIGMAX);
                     double duration  = (Cv2.GetTickCount() - startTime) / (Cv2.GetTickFrequency());
                     showImage(res, duration);
                     showParameters("高斯核x方向标准差:", SIGMAX.ToString(), "二值化算子:", CONTOUR_TYPE.ToString());
                     textBox6.Hide();
                     comboBox4.Show();
                     comboBox4.Items.Clear();
                     comboBox4.Text = CONTOUR_TYPE.ToString();
                     comboBox4.Items.Add("Binary");
                     comboBox4.Items.Add("BinaryInv");
                     comboBox4.Items.Add("Trunc");
                     comboBox4.Items.Add("Tozero");
                     comboBox4.Items.Add("TozeroInv");
                     comboBox4.Items.Add("Mask");
                     comboBox4.Items.Add("Otsu");
                     comboBox4.Items.Add("Triangle");
                 }
                 catch (Exception err)
                 {
                     MessageBox.Show(err.ToString());
                 }
             }
             //异常处理
             else
             {
                 MessageBox.Show("算法选择有误,请重新选择算法和正确的参数");
             }
         }
     }
 }