private void OnGlcmBackgroundDoWork(object sender, DoWorkEventArgs e) { using (var sourceBitmap = ConvertToGrayscale((Bitmap)Bitmap.FromFile(SourceImagePath))) { int imgWidth = sourceBitmap.Width; int imgHeight = sourceBitmap.Height; int stepX = StepXImage; int stepY = StepYImage; int iterationCounter = 1; double iterations = Math.Ceiling((float)imgWidth / stepX) * Math.Ceiling((float)imgHeight / stepY); UpdateParametersOfSelectedImage(new HaralickDescriptor(CalculateGLCM(sourceBitmap))); EntropyValues = new OrderedDictionary(); EnergyValues = new OrderedDictionary(); CorrelationValues = new OrderedDictionary(); ContrastValues = new OrderedDictionary(); for (var y = 0; y < imgHeight; y += stepY) { for (var x = 0; x < imgWidth; x += stepX) { var lenX = x + stepX; if (lenX > imgWidth) { lenX = imgWidth; } var lenY = y + stepY; if (lenY > imgHeight) { lenY = imgHeight; } using (var currentTile = new Bitmap(stepX, stepY)) { currentTile.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); using (var currentTileGraphics = Graphics.FromImage(currentTile)) { currentTileGraphics.Clear(Color.Black); var absentRectangleArea = new Rectangle(x, y, lenX, lenY); currentTileGraphics.DrawImage(sourceBitmap, 0, 0, absentRectangleArea, GraphicsUnit.Pixel); var haralick = new HaralickDescriptor(CalculateGLCM(currentTile)); EntropyValues.Add(new Tuple <int, int, int, int>(x, y, lenX, lenY), haralick.Entropy); EnergyValues.Add(new Tuple <int, int, int, int>(x, y, lenX, lenY), haralick.AngularSecondMomentum); CorrelationValues.Add(new Tuple <int, int, int, int>(x, y, lenX, lenY), haralick.Correlation); ContrastValues.Add(new Tuple <int, int, int, int>(x, y, lenX, lenY), haralick.Contrast); } } GlcmBackgroundWorker.ReportProgress((int)(100 / (iterations) * iterationCounter++)); } } } }
private void UpdateParametersOfSelectedImage(HaralickDescriptor haralick) { InvokeAction(() => { entropyLabel.Content = string.Format("Entropy: {0}", haralick.Entropy.ToString("N5")); energyLabel.Content = string.Format("Energy: {0}", haralick.AngularSecondMomentum.ToString("N5")); correlationLabel.Content = string.Format("Correlation: {0}", haralick.Correlation.ToString("N5")); contrast.Content = string.Format("Contrast: {0}", haralick.Contrast.ToString("N5")); }); }
internal Task computeFeatures() { return(Task.Run(delegate() { using (Image <Bgr, Byte> src = this.blob.Image.ToManagedImage().ToImage <Bgr, Byte>()){ VectorOfPoint contour = FeaturesUtilities.getMaxContour(src); if (contour != null && contour.Size > 5) { CircleF circle = CvInvoke.MinEnclosingCircle(contour); RotatedRect ellipse = CvInvoke.FitEllipse(contour); // extract shape features Rectangle bounds = CvInvoke.BoundingRectangle(contour); this.area = CvInvoke.ContourArea(contour, false); this.perimeter = CvInvoke.ArcLength(contour, true); this.shape = (4 * Math.PI * area) / Math.Pow(perimeter, 2); this.npoints = contour.Size; this.width = bounds.Width; this.height = bounds.Height; this.centroideY = circle.Center.Y; this.centroideX = circle.Center.X; this.radius = circle.Radius; this.aspectRatio = width / height; this.extent = area / (width * height); //extract texture features using (Image <Gray, Byte> gray = src.Convert <Gray, Byte>()){ var glcm = new GrayLevelCooccurrenceMatrix(distance: 1, degree: CooccurrenceDegree.Degree0, normalize: true); // Extract the matrix from the image double[,] matrix = glcm.Compute(gray.ToBitmap()); HaralickDescriptor haralick = new HaralickDescriptor(matrix); double[] haralickFeatures = haralick.GetVector(); this.homogeneity = haralick.AngularSecondMomentum; this.contrast = haralick.Contrast; this.haralickFeatures = haralickFeatures; } using (Image <Hsv, Byte> hsv = src.Convert <Hsv, Byte>()) { this.hsvColor = new MCvScalar() { V0 = hsv.Data[(int)circle.Center.Y, (int)circle.Center.X, 0], V1 = hsv.Data[(int)circle.Center.Y, (int)circle.Center.X, 1], V2 = hsv.Data[(int)circle.Center.Y, (int)circle.Center.X, 2] }; } this.bgrColor = new MCvScalar() { V0 = src.Data[(int)circle.Center.Y, (int)circle.Center.X, 0], V1 = src.Data[(int)circle.Center.Y, (int)circle.Center.X, 1], V2 = src.Data[(int)circle.Center.Y, (int)circle.Center.X, 2] }; } } })); }
public static double[] textureFeatures(Image <Bgr, Byte> src) { using (Image <Gray, Byte> gray = src.Convert <Gray, Byte>()){ var glcm = new GrayLevelCooccurrenceMatrix(distance: 1, degree: CooccurrenceDegree.Degree0, normalize: true); double[,] matrix = glcm.Compute(gray.ToBitmap()); HaralickDescriptor haralick = new HaralickDescriptor(matrix); //double[] haralickFeatures = haralick.GetVector(); double homogeneity = haralick.AngularSecondMomentum; double contrast = haralick.Contrast; return(new double[] { homogeneity, contrast }); } }
public double[] GlcmFeatures(Bitmap image) { var descriptor = new HaralickDescriptor(CreateGLCM(image)); var features = new double[5]; features[0] = descriptor.F01; //energy - tham số năng lượng features[1] = descriptor.F02; //contrast - độ tương phản features[2] = descriptor.F03; //correlation - độ tương đồng features[3] = descriptor.F09; //entropy features[4] = descriptor.TextureHomogeneity; //homogeneity- tính đồng nhất return(features); }
private double[] GlcmFeatures(Bitmap image) { var descriptor = new HaralickDescriptor(CreateGLCM(image)); var features = new double[13]; features[0] = descriptor.F01; features[1] = descriptor.F02; features[2] = descriptor.F03; features[3] = descriptor.F04; features[4] = descriptor.F05; features[5] = descriptor.F06; features[6] = descriptor.F07; features[7] = descriptor.F08; features[8] = descriptor.F09; features[9] = descriptor.F10; features[10] = descriptor.F11; features[11] = descriptor.F12; features[12] = descriptor.F13; return(features); }