我正在尝试使用c#和emgucv运行此代码来绘制图像的直方图,但它一直给我一个错误,即'名称' InitializeComponent()'在当前的背景下不存在'。我试图添加引用,但它不起作用。
using ZedGraph;
using System;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using Emgu.Util;
using Emgu.CV.Structure;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Windows.Markup;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xaml;
using System.Xml;
;
namespace My_EMGU_Program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog Openfile = new OpenFileDialog();
if (Openfile.ShowDialog() == DialogResult.OK)
{
Image<Bgr, byte> img = new Image<Bgr, byte>(Openfile.FileName);
float[] BlueHist;
float[] GreenHist;
float[] RedHist;
DenseHistogram Histo = new DenseHistogram(255, new RangeF(0, 255));
Image<Gray, Byte> img2Blue = img[0];
Image<Gray, Byte> img2Green = img[1];
Image<Gray, Byte> img2Red = img[2];
Histo.Calculate(new Image<Gray, Byte>[] { img2Blue }, true, null);
//The data is here
//Histo.MatND.ManagedArray
BlueHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(BlueHist, 0);
Histo.Clear();
Histo.Calculate(new Image<Gray, Byte>[] { img2Green }, true, null);
GreenHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(GreenHist, 0);
Histo.Clear();
Histo.Calculate(new Image<Gray, Byte>[] { img2Red }, true, null);
RedHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(RedHist, 0);
float[] GrayHist;
Image<Gray, Byte> img_gray = new Image<Gray, byte> (Openfile.FileName);
Histo.Calculate(new Image<Gray, Byte>[] { img_gray }, true, null);
//The data is here
//Histo.MatND.ManagedArray
GrayHist = new float[256];
Histo.MatND.ManagedArray.CopyTo(GrayHist, 0);
/*This Methof call will produced the Histograms for you*/
Add_Histogram(GrayHist, "Gray Histogram");
Add_Histogram(BlueHist, "Blue Histogram");
Add_Histogram(GreenHist, "Green Histogram");
Add_Histogram(RedHist, "Red Histogram");
}
}
/* You can extend this method to set line colour x and y titles quite easily */
//Global X Y locations to stack Histograms
int X = 10, Y = 10;
Panel panel1 = new Panel();
void Add_Histogram(float[] Histo_dat, string Histo_Title = "Histogram Title")
{
//Create the Control
ZedGraphControl zgc = new ZedGraphControl();
zgc.Location = new Point(Y, X);
zgc.Size = new Size(panel1.Width - 20, 200);
X += (zgc.Size.Height + 20); //increas X for next Graph
GraphPane myPane = zgc.GraphPane;
myPane.Title.Text = Histo_Title;
myPane.XAxis.Title.Text = "X Axis - Pixel Bin Values";
myPane.YAxis.Title.Text = "Y Axis - Total Number of Pixels";
//Create an array that Zedgraph can use
PointPairList list1 = new PointPairList();
for (int i = 0; i < Histo_dat.Length; i++)
{
list1.Add(new PointPair(i, Histo_dat[i]));
}
//Add the data to the control
LineItem myCurve = myPane.AddCurve("Title", list1, Color.Blue, SymbolType.Circle);
zgc.AxisChange();
//Add the controll and refresh form
panel1.Controls.Add(zgc);
this.Refresh();
}
//In case you wish to restart and draw newly calculated histograms
void Clear_Histograms()
{
panel1.Controls.Clear();
X = 10;
}
}
}
答案 0 :(得分:1)
Form1
是一个部分类
这意味着有另一个文件Form1.designer.cs包含缺少的方法
您不能只复制和粘贴该代码 - 您需要随附的文件:
Form1.cs
,Form1.designer.cs
以及某些资源文件,例如Form1.resx
我建议在尝试复杂的代码之前先阅读基础知识。