//trích xuất đặc trưng kết cấu của ảnh dùng pp Haralick ----- texture features public double[,] CreateGLCM(Bitmap image) //gray-level-cooccurrence-matrix - tạo ảnh { Bitmap grayImage = Grayscale.CommonAlgorithms.BT709.Apply(image); //chuyển ảnh màu đã segment vùng bệnh sang ảnh xám bằng thuật toán BT709 var grayLevelCooccurrenceMatrix = new GrayLevelCooccurrenceMatrix(); //ma trận đồng hiện mức xám return(grayLevelCooccurrenceMatrix.Compute(grayImage)); }
private double[,] CreateGLCM(Bitmap image) { var grayLevelCooccurrenceMatrix = new GrayLevelCooccurrenceMatrix(); image = grayscaleBT.Apply(image); return(grayLevelCooccurrenceMatrix.Compute(image)); }
public void apply_test() { #region doc_apply // Ensure results can be reproduced: Accord.Math.Random.Generator.Seed = 0; // Let's generate a square image containing a wooden texture: Bitmap wood = new WoodTexture().Generate(512, 512).ToBitmap(); // Show the image or save it to disk so we can visualize it later: // ImageBox.Show(wood); // visualizes it // wood.Save("wood.png"); // saves to disk // Create a new Haralick extractor: Haralick haralick = new Haralick() { Mode = HaralickMode.AverageWithRange, CellSize = 0 // use the entire image }; // Extract the descriptors from the texture image List <double[]> descriptors = haralick.ProcessImage(wood); // If desired, we can obtain the GLCM for the image: GrayLevelCooccurrenceMatrix glcm = haralick.Matrix; // Since we specified CellSize = 0 when we created the Haralick object // above, the Haralick extractor should have been computed considering // the entire image and therefore it will return just one descriptor: double[] descriptor = descriptors[0]; // should be similar to // { // 0.000452798780435224, 0.000176452252541844, 2185.7835571369, 1055.78890910489, // 1819136356.0869, 31272466.1144943, 162.811942113822, 0.0294747289650559, // 0.0600645989906271, 0.014234218481406, 325.617817549069, 0.0288571168872522, // 124520.583178736, 1051.39892825528, 6.04466262360143, 0.0166538281740083, // 9.9387182389777, 0.188948901162149, 4.21006097507467E-05, 1.50197212765552E-05, // 4.51859838204172, 0.250994550822876, -0.127589342042208, 0.0356360581349288, // 0.858214424241827, 0.0572031826003976 // } #endregion string str = descriptor.ToCSharp(); double[] expected = new double[] { 0.000452798780435224, 0.000176452252541844, 2185.7835571369, 1055.78890910489, 1819136356.0869, 31272466.1144943, 162.811942113822, 0.0294747289650559, 0.0600645989906271, 0.014234218481406, 325.617817549069, 0.0288571168872522, 124520.583178736, 1051.39892825528, 6.04466262360143, 0.0166538281740083, 9.9387182389777, 0.188948901162149, 4.21006097507467E-05, 1.50197212765552E-05, 4.51859838204172, 0.250994550822876, -0.127589342042208, 0.0356360581349288, 0.858214424241827, 0.0572031826003976 }; Assert.IsTrue(expected.IsEqual(descriptor, rtol: 1e-6)); Assert.AreEqual(1, descriptors.Count); Assert.AreEqual(new HashSet <CooccurrenceDegree> { CooccurrenceDegree.Degree0, CooccurrenceDegree.Degree45, CooccurrenceDegree.Degree90, CooccurrenceDegree.Degree135 }, haralick.Degrees); }
public void ComputeTest2() { int size = 255; UnmanagedImage output = createGradient(size); Haralick haralick = new Haralick() { Mode = HaralickMode.Combine }; Assert.AreEqual(13, haralick.Features); Assert.AreEqual(4, haralick.Degrees.Count); List <double[]> result = haralick.ProcessImage(output); GrayLevelCooccurrenceMatrix glcm = haralick.Matrix; HaralickDescriptorDictionary[,] features = haralick.Descriptors; Assert.AreEqual(1, features.GetLength(0)); Assert.AreEqual(1, features.GetLength(1)); Assert.AreEqual(1, result.Count); double[] actual = result[0]; double[] expected = { 0.00393700787401572, 0.999999999999998, 353791227646.996, 126.499984621299, 0.499999999999999, 254, 83238.6962631393, 5.53733426701852, 5.53733426701852, 0.003921568627451, 1.66533453693773E-15, -1, 0.999992249954468, 0.00393700787401572, 0.999999999999998, 353791227646.996, 126.499984621299, 0.499999999999999, 254, 83238.6962631393, 5.53733426701852, 5.53733426701852, 0.003921568627451, 1.66533453693773E-15, -1, 0.999992249954468, 0.00392156862745099, 0,1.45558373073433E+38, 126.999984621299, 0.999999999999997, 254, 83406.410387403, 5.54126354515845, 5.54126354515845, 0.00392156862745098, 3.10862446895043E-15, -1, 0.999992310620187, 0.00393700787401572, 0.999999999999998, 353791227646.996, 127.499984621299, 0.499999999999999, 254, 83238.6962631393, 5.53733426701852, 5.53733426701852, 0.003921568627451, 1.66533453693773E-15, -1, 0.999992249954468 }; string str = actual.ToString(Accord.Math.CSharpArrayFormatProvider.InvariantCulture); Assert.IsNotNull(str); Assert.AreEqual(52, actual.Length); for (int i = 0; i < actual.Length; i++) { Assert.AreEqual(expected[i], actual[i], System.Math.Abs(expected[i]) * 1e-10); } }
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 }); } }
private double[,] CalculateGLCM(Bitmap bitmap) { using (var unmanagedImage = UnmanagedImage.FromManagedImage(bitmap)) { GrayLevelCooccurrenceMatrix glcm = new GrayLevelCooccurrenceMatrix { Normalize = true, Distance = this.Distance, Degree = (CooccurrenceDegree)Enum.Parse(typeof(CooccurrenceDegree), Degree) }; return(glcm.Compute(unmanagedImage)); } }
public void ComputeTest() { int size = 255; UnmanagedImage output = createGradient(size); Haralick haralick = new Haralick() { Mode = HaralickMode.AverageWithRange }; Assert.AreEqual(13, haralick.Features); Assert.AreEqual(4, haralick.Degrees.Count); List <double[]> result = haralick.ProcessImage(output); GrayLevelCooccurrenceMatrix glcm = haralick.Matrix; HaralickDescriptorDictionary[,] features = haralick.Descriptors; Assert.AreEqual(1, features.GetLength(0)); Assert.AreEqual(1, features.GetLength(1)); Assert.AreEqual(1, result.Count); double[] actual = result[0]; double[] expected = { 0.00393314806237454, 1.54392465647286E-05, 0.749999999999999, 0.999999999999998, 3.63895932683582E+37, 1.45558373073433E+38, 126.87498462129943, 1, 0.624999999999999, 0.499999999999998, 254, 3.12638803734444E-13, 83280.6247942052, 167.714124263744, 5.5383165865535, 0.00392927813992738, 5.5383165865535, 0.00392927813992738, 0.00392156862745099, 1.12757025938492E-17, 2.02615701994091E-15, 1.4432899320127E-15, -1, 0, 0.999992265120898, 6.06657187818271E-08 }; // string str = actual.ToString(Math.Formats.CSharpArrayFormatProvider.InvariantCulture); Assert.AreEqual(26, actual.Length); for (int i = 0; i < actual.Length; i++) { Assert.AreEqual(expected[i], actual[i], System.Math.Abs(expected[i]) * 1e-10); } }