2014-01-20 10 views
2

我在图像处理工作在C#和我有两个主要错误:命名参数规格必须出现后,所有固定参数都被指定

  1. 错误:命名参数规范都必须出现在所有固定参数后就一直指定
  2. 错误:System.Drawing.Size”是 '类型',但使用类似 '变量'

这是我的代码:

using System; 

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Emgu.CV; 
using Emgu.CV.Structure; 
using Emgu.Util; 
using Emgu.CV.CvEnum; 
using Emgu.CV.GPU; 
using Emgu.CV.UI; 


namespace SNAKE_C_Sharp 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void imageBox1_Click(object sender, EventArgs e) 
     { 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      using (OpenFileDialog dialog = new OpenFileDialog()) 
     { 
       dialog.Filter = "(*.*)|*.*"; 
       if (dialog.ShowDialog() == DialogResult.OK) 
       { 
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
        Image image = Image.FromFile(dialog.FileName); 

        pictureBox1.Image = image; 

       } 
      } 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      this.Close(); 

     } 
     struct parameter 
     { 
      public double alpha { get; set; } 
      public double beta { get; set; } 
      public double gamma { get; set; } 
     }; 
     unsafe private void button3_Click(object sender, EventArgs e) 
     { 
     { 

       int length = 1000; 
       MCvPoint2D64f* contour; 

       MCvPoint2D64f center = new MCvPoint2D64f(); 
       var snake_param = new List<parameter>(); 
       snake_param.Add(new parameter { alpha = 0.1, beta = 0.1, gamma = 0.1,   }); 
       //Image src_img = pictureBox1.Image; 
       IntPtr dst_img = new IntPtr(); 
       //IntPtr src_img = Emgu.CV.CvInvoke.cvLoadImage("pictureBox1.Image",  Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR); 
       Bitmap bitmapp = new Bitmap("pictureBox1.Image"); 

       Image<Bgr, byte> image = new Image<Bgr, byte>(bitmapp); 




       center.x = image.Width; 
       center.y = image.Height; 




       int i; 
       for (i = 0; i < length; i++) 
       { 
        contour[i].x = (int)(center.x * Math.Cos(2 * Math.PI * i/length) + center.x); 
        contour[i].y = (int)(center.y * Math.Sin(2 * Math.PI * i/length) + center.y); 
      } 




       for (i = 0; i < length - 1; i++) 
       { 
       CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED,0); 
      } 


       CvInvoke.cvLine(dst_img, contour[length - 1], contour[0], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0); 




       IntPtr src_img = image.Ptr; 

       CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true); 

       CvInvoke.cvCvtColor(src_img, dst_img, COLOR_CONVERSION.GRAY2RGB); 


       for (i = 0; i < length - 1; i++) 
       { 
        CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0); 
       } 
       CvInvoke.cvLine(dst_img, contour[length - 1], contour[0], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED, 0); 
       pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; 

       Bitmap bitmappbb = new Bitmap("dst_img"); 
       Image<Bgr, byte> imagee = new Image<Bgr, byte>(bitmapp); 
       pictureBox2.Image = bitmappbb; 
      } 

     } 

     private void imageBox1_Click_1(object sender, EventArgs e) 
     { 

     } 

     private void panAndZoomPictureBox1_Click(object sender, EventArgs e) 
     { 

     } 

     private void imageBox1_Click_2(object sender, EventArgs e) 
     { 

     } 


    } 
}  

我该如何调整上述错误?

回答

1

而不是

CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true); 

你需要这个:

CvInvoke.cvSnakeImage(src_img, contour, length, snake_param[1].alpha, snake_param[2].beta, snake_param[3].gamma, 1.0f, contour[i], new System.Drawing.Size(15, 15), new MCvTermCriteria(1, 0.0), true); 

这应该可以解决了第二个错误。没有新的关键字,您没有System.Drawing.Size实例。

编辑:

我不会对代码进行测试,也没有阅读它行由行,所以我希望你的第一个错误的详细信息,让您的解决方案。你能告诉我哪一行是抛出的异常吗?

此外,我可能会建议您应该更加关注代码的制表,因为如果以非结构化方式编写代码时很难阅读。阅读并不是不可能的,但我们大多数人(包括我自己)都不会阅读它。

+0

感谢您的答案,你的建议,这将有助于我要sure.I想你的建议,但仍然有一个错误 – user3213662

3

这是导致错误1

CvInvoke.cvLine(dst_img, contour[i], contour[i + 1], new MCvScalar(255, 0, 0), 2, lineType: LINE_TYPE.EIGHT_CONNECTED,0); 

我会让它更具可读性的问题之一......

CvInvoke.cvLine(
    dst_img, 
    contour[i], 
    contour[i + 1], 
    new MCvScalar(255, 0, 0), 
    2, 
    lineType: LINE_TYPE.EIGHT_CONNECTED, 
    0 
); 

见第二到最后一行使用命名参数(lineType:),但后跟一个非命名参数?编译器如何知道你的意思?

第二个错误是@LajosArpad所述,您需要在使用System.Drawing.Size(..)前添加一个new

+0

谢谢您的回答,但你可以更给我解释一下你非是什么意思命名的参数?对于(I = 0; I <长度 - 1;我++){ CvInvoke.cvLine( dst_img, 轮廓[I], 轮廓第[i + 1], 新MCvScalar(255,0,0), 2, lineType:Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0); }但仍然有同样的错误,你可以解释更多请和谢谢 – user3213662

+0

文本'lineType:'是一个命名的参数,当你用这样的名称给你的参数加前缀时,你可以把它们放在任何顺序,只要它们'在没有名字的所有参数之后(后面的参数'0'没有被命名,这就是你得到错误的原因)。 –

+0

非常有帮助谢谢 – user3213662

0

我固定的最后一个错误,这就是我的新代码:

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
     InitializeComponent(); 
     } 

     private void button1_Click(object sender, System.EventArgs e) 
     { 
     using (OpenFileDialog dialog = new OpenFileDialog()) 
     { 
      dialog.Filter = "JPEG|*.jpg|PNG|*.PNG"; 
      if (dialog.ShowDialog() == DialogResult.OK) 
      { 
       pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 

       Image image = Image.FromFile(dialog.FileName); 

       pictureBox1.Image = image; 

      } 
     } 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 


    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 
    struct parameter 
    { 
     public float alpha { get; set; } 
     public float beta { get; set; } 
     public float gamma { get; set; } 
    }; 




    unsafe private void button3_Click(object sender, EventArgs e) 
    { 
     { 

     int length = 1000; 

     Point *contour; 

     Point center = new Point(); 

     var snake_param = new List<parameter>(); 

      snake_param.Add(new parameter { alpha= 0.1f , beta = 0.1f, gamma= 0.1f, }); 

     IntPtr dst_img= new IntPtr(); 

     Bitmap bitmap = new Bitmap("pictureBox1.Image"); 

     Image<Bgr, byte> image = new Image<Bgr, byte>(bitmap); 




     center.X = image.Width; 
     center.Y = image.Height; 




     int i; 
     for (i = 0; i < length; i++) 
     { 
      contour[i].X = (int)(center.X * Math.Cos(2 * Math.PI * i/length) + center.X); 
      contour[i].Y = (int)(center.Y * Math.Sin(2 * Math.PI * i/length) + center.Y); 
     } 

    LINE_TYPE lignetype = new LINE_TYPE();   


     for (i = 0; i < length - 1; i++) 
     { 
      CvInvoke.cvLine(
       dst_img, 
       contour[i], 
       contour[i + 1], 
       new MCvScalar(255,0,0), 
       2, 
       LINE_TYPE.EIGHT_CONNECTED, 
       0 ); 
     } 


     CvInvoke.cvLine 
      (
      dst_img, 
      contour[length - 1], 
      contour[0], 
      new MCvScalar(255,0,0), 
      2, 
      LINE_TYPE.EIGHT_CONNECTED, 
      0 
      ); 


      IntPtr ctr =new IntPtr(); 
      //public void PixelToInkSpace(
      //IntPtr a 
      //ref Point contour 
      //);   



     IntPtr src_img = image.Ptr; 
     CvInvoke.cvSnakeImage(
      src_img, 
      contour[i], 
      length, 
      snake_param.[1].alfa, 
      snake_param[2].beta, 
      snake_param[3].gamma, 
      1, 
      new System.Drawing.Size(15, 15), 
      new MCvTermCriteria(1,0.0), 
      1); 



     CvInvoke.cvCvtColor(
      src_img, 
      dst_img, 
      COLOR_CONVERSION.GRAY2RGB); 


      for (i = 0; i < length - 1; i++) 
     { 
      CvInvoke.cvLine(
       dst_img, 
       contour[i], 
       contour[i + 1], 
       new MCvScalar(255,0,0), 
       2, 
       LINE_TYPE.EIGHT_CONNECTED, 
       0); 
     } 
      CvInvoke.cvLine(
       dst_img, 
       contour[length - 1], 
       contour[0], 
       new MCvScalar(255,0,0), 
        2, 
        LINE_TYPE.EIGHT_CONNECTED, 
        0); 
      pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; 

      Bitmap bitmappbb = new Bitmap("dst_img"); 
      Image<Bgr, byte> imagee = new Image<Bgr, byte>(bitmappbb); 
      pictureBox2.Image = bitmappbb; 
      } 


      } 
     } 
    } 

但现在我的错误是不同的,因为我是从C++转换我的代码到C#, 我发现蛇格式

public static void cvSnakeImage(
IntPtr image, 
IntPtr points, 
int length, 
float[] alpha, 
float[] beta, 
float[] gamma, 
int coeffUsage, 
Size win, 
MCvTermCriteria criteria, 
bool calcGradient 

  1. 我没有找到方法来转换变量“轮廓”,类型为“点”到“IntPtr的”。
  2. 还有一种方法可以调用alfa,beta et gamma作为float []; @Timothy沃尔特斯
相关问题