我正在使用C#进行图像处理,我有两个主要错误:
这是我的代码:
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)
{
}
}
}
我如何调整上述错误?
答案 0 :(得分: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所说,您需要在使用new
之前添加System.Drawing.Size(..)
。
答案 1 :(得分: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
个实例。
编辑:
我不会测试您的代码,也不会逐行阅读,所以我希望有关您的第一个错误的更多信息可以为您提供解决方案。你能告诉我抛出的异常是哪一行吗?
此外,我可能建议您应该更加关注代码的列表,因为如果以非结构化的方式编写代码,则很难阅读。阅读并非不可能,但我们大多数人(包括我自己)都不会阅读它。
答案 2 :(得分: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
)