本文整理汇总了C#中System.Matrix.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.GetType方法的具体用法?C# Matrix.GetType怎么用?C# Matrix.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Matrix
的用法示例。
在下文中一共展示了Matrix.GetType方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCreateMatrix
public void TestCreateMatrix()
{
var matrix = new Matrix<int>(2, 2);
var other_matrix = matrix.CreateMatrix(3, 3);
Assert.IsInstanceOfType(matrix.GetType(), other_matrix);
}
示例2: Main
public static void Main()
{
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.InvariantCulture;
try
{
int[,] mat1 = new int[,]
{
{ 1, -2, -3 },
{ 2, 0, 1 },
{ 1, 1, 1 }
};
Matrix<int> matrix1 = new Matrix<int>(mat1);
int[,] mat2 = new int[3, 3];
Matrix<int> matrix2 = new Matrix<int>(mat2);
Console.WriteLine(matrix1 ? "True" : "False");
Console.WriteLine();
Console.WriteLine(matrix2 ? "True" : "False");
Type type = matrix1.GetType();
foreach (var attr in type.GetCustomAttributes(false))
{
if (attr is VersionAttribute)
{
Console.WriteLine("This is version {0} of the {1} class.",
(attr as VersionAttribute).Version, type.FullName);
}
}
Console.WriteLine();
foreach (var attr in type.GetMethod("ToString").GetCustomAttributes(false))
{
if (attr is VersionAttribute)
{
Console.WriteLine("This is version {0} of the {1} method.",
(attr as VersionAttribute).Version, type.GetMethod("ToString").Name);
}
}
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
}
示例3: btnOpenTestImage_Click
///////////////////////////////////////////////////////////////////////////////////////////
private void btnOpenTestImage_Click(object sender, EventArgs e)
{
// note: we effectively have to read the first XML file twice
// first, we read the file to get the number of rows (which is the same as the number of samples).
// the first time reading the file we can't get the data yet, since we don't know how many rows of data there are
// next, reinstantiate our classifications Matrix and training images Matrix with the correct number of rows
// then, read the file again and this time read the data into our resized classifications Matrix and training images Matrix
Matrix<Single> mtxClassifications = new Matrix<Single>(1, 1); // for the first time through, declare these to be 1 row by 1 column
Matrix<Single> mtxTrainingImages = new Matrix<Single>(1, 1); // we will resize these when we know the number of rows (i.e. number of training samples)
// possible chars we are interested in are digits 0 through 9
List<int> intValidChars = new List<int> { (int)'0', (int)'1', (int)'2', (int)'3', (int)'4', (int)'5', (int)'6', (int)'7', (int)'8', (int)'9' };
XmlSerializer xmlSerializer = new XmlSerializer(mtxClassifications.GetType()); // these variables are for
StreamReader streamReader; // reading from the XML files
try {
streamReader = new StreamReader("classifications.xml"); // attempt to open classifications file
} catch(Exception ex) { // if error is encountered, show error and return
txtInfo.Text = Environment.NewLine + txtInfo.Text + "unable to open 'classifications.xml', error:" + Environment.NewLine;
txtInfo.Text = txtInfo.Text + ex.Message + Environment.NewLine + Environment.NewLine;
return;
}
// read from the classifications file the 1st time, this is only to get the number of rows, not the actual data
mtxClassifications = (Matrix<Single>)xmlSerializer.Deserialize(streamReader);
streamReader.Close(); // close the classifications XML file
intNumberOfTrainingSamples = mtxClassifications.Rows; // get the number of rows, i.e. the number of training samples
// now that we know the number of rows, reinstantiate classifications Matrix and training images Matrix with the actual number of rows
mtxClassifications = new Matrix<Single>(intNumberOfTrainingSamples, 1);
mtxTrainingImages = new Matrix<Single>(intNumberOfTrainingSamples, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT);
try {
streamReader = new StreamReader("classifications.xml"); // attempt to reinitialize the stream reader
} catch (Exception ex) { // if error is encountered, show error and return
txtInfo.Text = Environment.NewLine + txtInfo.Text + "unable to open 'classifications.xml', error:" + Environment.NewLine;
txtInfo.Text = txtInfo.Text + ex.Message + Environment.NewLine + Environment.NewLine;
return;
}
// read from the classifications file again, this time we can get the actual data
mtxClassifications = (Matrix<Single>)xmlSerializer.Deserialize(streamReader);
streamReader.Close(); // close the classifications XML file
xmlSerializer = new XmlSerializer(mtxTrainingImages.GetType()); // reinstantiate file reading variables
try {
streamReader = new StreamReader("images.xml");
} catch (Exception ex) { // if error is encountered, show error and return
txtInfo.Text = Environment.NewLine + txtInfo.Text + "unable to open 'images.xml', error:" + Environment.NewLine;
txtInfo.Text = txtInfo.Text + ex.Message + Environment.NewLine + Environment.NewLine;
return;
}
mtxTrainingImages = (Matrix<Single>)xmlSerializer.Deserialize(streamReader); // read from training images file
streamReader.Close(); // close the training images XML file
// train //////////////////////////////////////////////////////////
KNearest kNearest = new KNearest(); // instantiate KNN object
kNearest.Train(mtxTrainingImages, mtxClassifications, null, false, 1,false); // call to train
// test ///////////////////////////////////////////////////////////////////////
DialogResult drChosenFile;
drChosenFile = ofdOpenFile.ShowDialog(); // open file dialog
if (drChosenFile != DialogResult.OK || ofdOpenFile.FileName == "") { // if user chose Cancel or filename is blank . . .
lblChosenFile.Text = "file not chosen"; // show error message on label
return; // and exit function
}
Image<Bgr, Byte> imgTestingNumbers; // this is the main input image
try {
imgTestingNumbers = new Image<Bgr, Byte>(ofdOpenFile.FileName); // open image
} catch(Exception ex) { // if error occurred
lblChosenFile.Text = "unable to open image, error: " + ex.Message; // show error message on label
return; // and exit function
}
if(imgTestingNumbers == null) { //if image could not be opened
lblChosenFile.Text = "unable to open image"; // show error message on label
return; // and exit function
}
lblChosenFile.Text = ofdOpenFile.FileName; // update label with file name
Image<Gray, Byte> imgGrayscale; //
Image<Gray, Byte> imgBlurred; // declare various images
Image<Gray, Byte> imgThresh; //
Image<Gray, Byte> imgThreshCopy; //
//.........这里部分代码省略.........
开发者ID:MicrocontrollersAndMore,项目名称:OpenCV_KNN_Character_Recognition_Machine_Learning_Visual_Basic_C_Sharp,代码行数:101,代码来源:TrainAndTest.cs
示例4: btnOpenTrainingImage_Click
//.........这里部分代码省略.........
contours = imgThreshCopy.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL); // get contours again to go back to beginning
imgContours = new Image<Gray, Byte>(imgThresh.Size); // instantiate contours image
// draw contours onto contours image
CvInvoke.cvDrawContours(imgContours, contours, new MCvScalar(255), new MCvScalar(255), 100, 1, LINE_TYPE.CV_AA, new Point(0, 0));
CvInvoke.cvShowImage("imgContours", imgContours); // show contours image for reference
// this is our classifications data structure
Matrix<Single> mtxClassifications = new Matrix<Single>(intNumberOfTrainingSamples, 1);
// this is our training images data structure, note we will have to perform some conversions to write to this later
Matrix<Single> mtxTrainingImages = new Matrix<Single>(intNumberOfTrainingSamples, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT);
// this keeps track of which row we are on in both classifications and training images,
int intTrainingDataRowToAdd = 0; // note that each sample will correspond to one row in
// both the classifications XML file and the training images XML file
while (contours != null) {
Contour<Point> contour = contours.ApproxPoly(contours.Perimeter * 0.0001); //get the current contour, note that the lower the multiplier, the higher the precision
if (ContourIsValid(contour)) { // if contour is big enough to consider
Rectangle rect = contour.BoundingRectangle; // get the bounding rect
imgTrainingNumbers.Draw(rect, new Bgr(Color.Red), 2); // draw red rectangle around each contour as we ask user for input
Image<Gray, Byte> imgROI = imgThresh.Copy(rect); // get ROI image of current char
// resize image, this is necessary for recognition and storage
Image<Gray, Byte> imgROIResized = imgROI.Resize(RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT, INTER.CV_INTER_LINEAR);
CvInvoke.cvShowImage("imgROI", imgROI); // show ROI image for reference
CvInvoke.cvShowImage("imgROIResized", imgROIResized); // show resized ROI image for reference
CvInvoke.cvShowImage("imgTrainingNumbers", imgTrainingNumbers); // show training numbers image, this will now have red rectangles drawn on it
int intChar = CvInvoke.cvWaitKey(0); // get key press
if (intChar == 27) { // if esc key was pressed
return; // exit the function
} else if (intValidChars.Contains(intChar)) { // else if the char is in the list of chars we are looking for . . .
mtxClassifications[intTrainingDataRowToAdd, 0] = Convert.ToSingle(intChar); // write classification char to classifications Matrix
// now add the training image (some conversion is necessary first) . . .
// note that we have to covert the images to Matrix(Of Single) type, this is necessary to pass into the KNearest object call to train
Matrix<Single> mtxTemp = new Matrix<Single>(imgROIResized.Size); // declare a Matrix of the same dimensions as the Image we are adding to the data structure of training images
Matrix<Single> mtxTempReshaped = new Matrix<Single>(1, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT); // declare a flattened (only 1 row) matrix of the same total size
CvInvoke.cvConvert(imgROIResized, mtxTemp); // convert Image to a Matrix of Singles with the same dimensions
for (int intRow = 0; intRow < RESIZED_IMAGE_HEIGHT; intRow++) { // flatten Matrix into one row by RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT number of columns
for (int intCol = 0; intCol < RESIZED_IMAGE_WIDTH; intCol++) {
mtxTempReshaped[0, (intRow * RESIZED_IMAGE_WIDTH) + intCol] = mtxTemp[intRow, intCol];
}
}
for (int intCol = 0; intCol < RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT; intCol++) { // write flattened Matrix into one row of training images Matrix
mtxTrainingImages[intTrainingDataRowToAdd, intCol] = mtxTempReshaped[0, intCol];
}
intTrainingDataRowToAdd = intTrainingDataRowToAdd + 1; // increment which row, i.e. sample we are on
} // end else if
} // end if
contours = contours.HNext; // move on to next contour
} // end while
txtInfo.Text = txtInfo.Text + "training complete !!" + Environment.NewLine + Environment.NewLine;
// save classifications to file /////////////////////////////////////
XmlSerializer xmlSerializer = new XmlSerializer(mtxClassifications.GetType());
StreamWriter streamWriter;
try {
streamWriter = new StreamWriter("classifications.xml"); // attempt to open classifications file
} catch (Exception ex) { // if error is encountered, show error and return
txtInfo.Text = Environment.NewLine + txtInfo.Text + "unable to open 'classifications.xml', error:" + Environment.NewLine;
txtInfo.Text = txtInfo.Text + ex.Message + Environment.NewLine + Environment.NewLine;
return;
}
xmlSerializer.Serialize(streamWriter, mtxClassifications);
streamWriter.Close();
// save training images to file /////////////////////////////////////
xmlSerializer = new XmlSerializer(mtxTrainingImages.GetType());
try {
streamWriter = new StreamWriter("images.xml"); // attempt to open classifications file
} catch (Exception ex) { // if error is encountered, show error and return
txtInfo.Text = Environment.NewLine + txtInfo.Text + "unable to open 'images.xml', error:" + Environment.NewLine;
txtInfo.Text = txtInfo.Text + ex.Message + Environment.NewLine + Environment.NewLine;
return;
}
xmlSerializer.Serialize(streamWriter, mtxTrainingImages);
streamWriter.Close();
txtInfo.Text = Environment.NewLine + txtInfo.Text + "file writing done" + Environment.NewLine;
}
开发者ID:MicrocontrollersAndMore,项目名称:OpenCV_KNN_Character_Recognition_Machine_Learning_Visual_Basic_C_Sharp,代码行数:101,代码来源:GenerateData.cs