private Collection <ClassifierReport> IVA_Classify(VisionImage image, Roi roi, string classifierFilePath) { //retval = new VisGrainTypeCollection(); // Create a binary image that will contain the segmented image. using (VisionImage binaryImage = new VisionImage(ImageType.U8, 7)) { ParticleClassifierSession vaClassifier = new ParticleClassifierSession(); bool fileExists = System.IO.File.Exists(classifierFilePath); vaClassifier.ReadFile(classifierFilePath); // Segments the image. Functions.IVA_Classification_Segmentation(image, binaryImage, roi, vaClassifier.PreprocessingOptions); // Get the ROIs of all individual particles. Collection <Roi> rois = Functions.IVA_Classification_Extract_Particles(image, binaryImage); // Allocates the classifier reports for all objects in the image. Collection <ClassifierReport> classifierReports = new Collection <ClassifierReport>(); List <VisGrainType> listGrainType = new List <VisGrainType>(); ListShape = new List <VisRectangleContour>(); // Classifies the object located in the given ROIs. for (int i = 0; i < rois.Count; ++i) { //RectangleContour rect = binaryImage=new VisionImage() object obj1 = rois[i][0].Shape; RectangleContour rc = (RectangleContour)rois[i][0].Shape; VisRectangleContour rect = new VisRectangleContour(rc); ClassifierReport report = vaClassifier.Classify(image, rois[i]); VisGrainType grainType = new VisGrainType { Name = report.BestClassName, ScoreClassification = report.ClassificationScore, ScoreIdentification = report.IdentificationScore }; listGrainType.Add(grainType); classifierReports.Add(report); ListShape.Add(rect); } GrainResultsType = new VisGrainTypeCollection(); GrainResultsType.Items = listGrainType; return(classifierReports); } }
public PaletteType ProcessImage_OLD(VisionImage image) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(7, 0); // Extract Color Plane using (VisionImage plane = new VisionImage(ImageType.U8, 7)) { // Extract the red color plane and copy it to the main image. Algorithms.ExtractColorPlanes(image, ColorMode.Rgb, plane, null, null); Algorithms.Copy(plane, image); } // Filters: Convolution - Applies a linear filter to an image by convolving the image with a filtering kernel. double[] vaCoefficients = { 1, 2, 4, 2, 1, 2, 4, 8, 4, 2, 4, 8, 16, 8, 4, 2, 4, 8, 4, 2, 1, 2, 4, 2, 1 }; Algorithms.Convolute(image, image, new Kernel(5, 5, vaCoefficients)); // Filters: Convolution - Applies a linear filter to an image by convolving the image with a filtering kernel. double[] vaCoefficients2 = { -1, -1, -1, -1, 10, -1, -1, -1, -1 }; Algorithms.Convolute(image, image, new Kernel(3, 3, vaCoefficients2)); // Automatic Threshold Algorithms.AutoThreshold(image, image, 2, ThresholdMethod.Clustering); // Basic Morphology - Applies morphological transformations to binary images. int[] vaCoefficients3 = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; StructuringElement vaStructElem = new StructuringElement(7, 7, vaCoefficients3); vaStructElem.Shape = StructuringElementShape.Hexagon; // Applies morphological transformations Algorithms.Morphology(image, image, MorphologyMethod.Open, vaStructElem); // Lookup Table: Equalize // Calculates the histogram of the image and redistributes pixel values // accross the desired range to maintain the same pixel value distribution. Range equalizeRange = new Range(0, 255); if (image.Type != ImageType.U8) { equalizeRange.Maximum = 0; } Algorithms.Equalize(image, image, null, equalizeRange, null); // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RectangleContour using the given values. //20200715 //RectangleContour vaRect = new RectangleContour(49, 33, 667, 163); RectangleContour vaRect = new RectangleContour(0, 0, image.Width, image.Height); roi.Add(vaRect); // Classifies all the objects located in the given ROI. string vaClassifierFilePath = "C:\\DATA\\#hasilscan\\Particle Classifier.clf"; vaClassifierReports = IVA_Classify(image, roi, vaClassifierFilePath); VisGrainTypeCollection colType = new VisGrainTypeCollection(vaClassifierReports); string vaClassifierFilePathSize = "C:\\DATA\\#hasilscan2\\Ukuran Classifier.clf"; vaClassifierReports2 = IVA_ClassifySize(image, roi, vaClassifierFilePathSize); VisGrainSizeCollection colSize = new VisGrainSizeCollection(vaClassifierReports2); GrainResults = new VisGrainDataCollection(vaClassifierReports, vaClassifierReports2, ListShape); roi.Dispose(); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Binary); }
public PaletteType ProcessImage(VisionImage image) { //vaParticleReportCalibrated = new ParticleMeasurementsReport(); // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(15, 0); // Image Buffer: Push Functions.IVA_PushBuffer(ivaData, image, 0); // Operators: NOR Image Algorithms.Nor(image, Functions.IVA_GetBuffer(ivaData, 0), image); // Extract Color Plane using (VisionImage plane = new VisionImage(ImageType.U8, 7)) { // Extract the red color plane and copy it to the main image. Algorithms.ExtractColorPlanes(image, ColorMode.Rgb, plane, null, null); Algorithms.Copy(plane, image); } // Thresholds an image into 2 classes by using local thresholding. LocalThresholdOptions vaLocalThresholdOptions = new LocalThresholdOptions(); vaLocalThresholdOptions.DeviationWeight = 1; vaLocalThresholdOptions.Method = LocalThresholdMethod.BackgroundCorrection; vaLocalThresholdOptions.ParticleType = ParticleType.Dark; vaLocalThresholdOptions.ReplaceValue = 1; vaLocalThresholdOptions.WindowHeight = 35; vaLocalThresholdOptions.WindowWidth = 35; Algorithms.LocalThreshold(image, image, vaLocalThresholdOptions); // Basic Morphology - Applies morphological transformations to binary images. int[] vaCoefficients = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; StructuringElement vaStructElem = new StructuringElement(3, 3, vaCoefficients); vaStructElem.Shape = StructuringElementShape.Square; // Applies morphological transformations Algorithms.Morphology(image, image, MorphologyMethod.GradientOut, vaStructElem); // Advanced Morphology: Fill Holes Algorithms.FillHoles(image, image, Connectivity.Connectivity8); // Advanced Morphology: Remove Objects int[] vaCoefficients2 = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; StructuringElement vaStructElem2 = new StructuringElement(3, 3, vaCoefficients2); vaStructElem.Shape = StructuringElementShape.Hexagon; // Filters particles based on their size. Algorithms.RemoveParticle(image, image, 6, SizeToKeep.KeepLarge, Connectivity.Connectivity8, vaStructElem2); // Advanced Morphology: Remove Border Objects - Eliminates particles touching the border of the image. Algorithms.RejectBorder(image, image, Connectivity.Connectivity8); // Lookup Table: Equalize // Calculates the histogram of the image and redistributes pixel values // accross the desired range to maintain the same pixel value distribution. Range equalizeRange = new Range(0, 255); if (image.Type != ImageType.U8) { equalizeRange.Maximum = 0; } Algorithms.Equalize(image, image, null, equalizeRange, null); // Image Buffer: Push Functions.IVA_PushBuffer(ivaData, image, 1); // Particle Analysis - Computes the number of particles detected in a binary image and // returns the requested measurements about the particles. Collection <MeasurementType> vaPixelMeasurements = new Collection <MeasurementType>(new MeasurementType[] { MeasurementType.BoundingRectLeft, MeasurementType.BoundingRectTop, MeasurementType.BoundingRectRight, MeasurementType.BoundingRectBottom, MeasurementType.MaxFeretDiameter }); Collection <MeasurementType> vaCalibratedMeasurements = new Collection <MeasurementType>(new MeasurementType[] {}); //IVA_Particle(image, Connectivity.Connectivity4, null, null, null, 10, out vaParticleReport, out vaParticleReportCalibrated); IVA_Particle(image, Connectivity.Connectivity4, vaPixelMeasurements, vaCalibratedMeasurements, ivaData, 10, out vaParticleReport, out vaParticleReportCalibrated); // Image Buffer: Pop Algorithms.Copy(Functions.IVA_GetBuffer(ivaData, 1), image); // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect = new RectangleContour(0, 0, image.Width, image.Height); roi.Add(vaRect); // Classifies all the objects located in the given ROI. string vaClassifierFilePath = "C:\\DATA\\#hasilscan3\\Particle Classifier.clf"; vaClassifierReports = IVA_Classify(image, roi, vaClassifierFilePath); VisGrainTypeCollection colType = new VisGrainTypeCollection(vaClassifierReports); roi.Dispose(); // Image Buffer: Pop Algorithms.Copy(Functions.IVA_GetBuffer(ivaData, 1), image); // Creates a new, empty region of interest. Roi roi2 = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect2 = new RectangleContour(0, 0, image.Width, image.Height); roi2.Add(vaRect2); // Classifies all the objects located in the given ROI. string vaClassifierFilePath2 = "C:\\DATA\\#hasilscan3\\Ukuran Classifier.clf"; vaClassifierReports2 = IVA_Classify(image, roi2, vaClassifierFilePath2); VisGrainSizeCollection colSize = new VisGrainSizeCollection(vaClassifierReports2); GrainResults = new VisGrainDataCollection(vaClassifierReports, vaClassifierReports2, ListShape); roi2.Dispose(); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Binary); }
public HttpResponseMessage Post() { HttpResponseMessage result = null; var httpRequest = HttpContext.Current.Request; var form = httpRequest.Form; VisCalculation calc = new VisCalculation(); //VisCalculationSize calcSize = new VisCalculationSize(); string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GrainConnection"].ConnectionString; if (httpRequest.Files.Count > 0) { var docfiles = new List <string>(); string fileName = ""; foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; //string myFile = Server.MapPath("images") + "\" + FileUpload1.PostedFile.FileName; fileName = Path.GetFileName(postedFile.FileName); string foderDate = DateTime.Now.ToString("yyyyMMdd"); var filePath = HttpContext.Current.Server.MapPath("~/images/" + foderDate + "/" + fileName); //postedFile.FileName); string fullPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } postedFile.SaveAs(filePath); //Image image = Image.FromFile(HttpContext.Current.Server.MapPath("~/images/" + foderDate + "/" + fileName)); FileInformation fileinfo = Algorithms.GetFileInformation(filePath); VisionImage image = new VisionImage(); image.Type = fileinfo.ImageType; image.ReadFile(filePath); //calculation grain type calc = new VisCalculation(image); //calculation grain size //calcSize = new VisCalculationSize(image); docfiles.Add(filePath); } //save to database string user = ""; double lat = 1000; double lng = 1000; if (form.Count > 0) { var items = form.AllKeys.SelectMany(form.GetValues, (k, v) => new { key = k, value = v }); foreach (var item in items) { //Console.WriteLine("{0} {1}", item.key, item.value); if (item.key.Equals("USER_ID")) { user = item.value; } else if (item.key.Equals("LATITUDE")) { Double.TryParse(item.value, out lat); } else if (item.key.Equals("LONGITUDE")) { Double.TryParse(item.value, out lng); } } /* * int pos = 0; * string param1 = ""; * string param2 = ""; * string param3 = ""; * foreach (var item in items) * { * //Console.WriteLine("{0} {1}", item.key, item.value); * if (pos == 0) * { * //param1 = item.key; * param1 = item.value; * } * else if (pos == 1) * { * //param2 = item.key; * param2 = item.value; * } * else if (pos == 2) * { * //param3 = item.key; * param3 = item.value; * } * pos = pos + 1; * } */ //string param1 = "form.Get(0)"; //string param2 = "form.Get(1)"; //string param3 = "form.Get(2)"; //if (!user.Equals("") && lat != 1000 && lng !=1000) //{ SqlConnection con = new SqlConnection(connectionString); try { con.Open(); string json = JsonConvert.SerializeObject(calc.GrainResults); string sql = "INSERT INTO G_RESULT (DATE_STAMP, USER_ID, LATITUDE, LONGITUDE, FILE_NAME, RESULT) VALUES('" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "','" + user + "','" + Convert.ToString(lat) + "','" + Convert.ToString(lng) + "','" + fileName + "','" + json + "');"; //string sql = "INSERT INTO G_TEST1 (PARAM1, PARAM2, PARAM3) VALUES('" + param1 + "','" + param2 + "','" + param3 + "');"; SqlCommand cmd = new SqlCommand(sql, con); cmd.CommandType = System.Data.CommandType.Text; int ret = cmd.ExecuteNonQuery(); //msg = "Success"; } catch (Exception ex) { Console.WriteLine(ex.Message); //msg = ex.Message; } //} } result = Request.CreateResponse(HttpStatusCode.OK, calc.GrainResults); //result = Request.CreateResponse(HttpStatusCode.OK, user + ", " + Convert.ToString(lat) + ", " + Convert.ToString(lng)); } else { //result = Request.CreateResponse(HttpStatusCode.BadRequest); VisGrainTypeCollection cols = new VisGrainTypeCollection(); cols.Message = "BadRequest"; //string msg = ""; //cols.Message = msg; // connectionString; result = Request.CreateResponse(HttpStatusCode.OK, cols); } return(result); }