예제 #1
0
        private void Contours()
        {
            var          originalBitmap = new Bitmap(ImageFile.GetInstance().Image);
            var          contoursBitmap = ImageFilter.PrewittFilter(originalBitmap, false);
            ContoursForm contoursForm   = new ContoursForm();

            contoursForm.Image = contoursBitmap;
            contoursForm.Show();
        }
예제 #2
0
        private void FragmentContours()
        {
            var imageBitmap = new Bitmap(ImageFile.GetInstance().Image);

            if (!coordinatesAnalyze.IsEmpty)
            {
                var contoursBitmap = FragmentBitmap(imageBitmap);

                contoursBitmap = ImageFilter.PrewittFilter(contoursBitmap, false);
                FilteredImageForm filteredImageForm = new FilteredImageForm();
                filteredImageForm.Image = contoursBitmap;
                filteredImageForm.Show();
            }
            else
            {
                MessageBox.Show("Выберите участок карты!", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #3
0
        private Bitmap HoughTransform(Bitmap originalImage)
        {
            var imgBitmap     = new Bitmap(originalImage);
            var lineTransform = new Accord.Imaging.HoughLineTransformation();
            // Convert it to binary and mark the possible lines
            // in white so it can be processed by the transform
            var sequence = new FiltersSequence(
                Grayscale.CommonAlgorithms.BT709,
                new NiblackThreshold(),
                new Invert()
                );
            var contoursBitmap = ImageFilter.PrewittFilter(imgBitmap, true);
            // Apply the sequence of filters above:
            Bitmap binaryImage = sequence.Apply(contoursBitmap);

            lineTransform.ProcessImage(binaryImage);
            // Now, let's say we would like to retrieve the lines and use them
            // for further processing. First, the lines can be ordered by their
            // relative intensity using
            HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(1);

            // Then, let's plot them on top of the input image. Since we will
            // apply many operations to a single image, it is better to first
            // convert it to an UnmanagedImage object to avoid having to lock
            // the image into memory multiple times.

            UnmanagedImage unmanagedImage = UnmanagedImage.FromManagedImage(binaryImage);

            // Finally, plot them in order:
            foreach (HoughLine line in lines)
            {
                line.Draw(unmanagedImage, color: Color.Red);
            }

            return(unmanagedImage.ToManagedImage());
        }
예제 #4
0
        private void DetectColors_Click(object sender, EventArgs e)
        {
            var imageFile      = ImageFile.GetInstance();
            var imageBitmap    = new Bitmap(imageFile.Image);
            var contoursBitmap = new Bitmap(imageBitmap);

            if (!coordinatesAnalyze.IsEmpty)
            {
                var xstart    = coordinatesAnalyze.X - RectSide / 2;
                var ystart    = coordinatesAnalyze.Y - RectSide / 2;
                var xend      = coordinatesAnalyze.X + RectSide / 2;
                var yend      = coordinatesAnalyze.Y + RectSide / 2;
                var xmin      = xstart > 0 ? xstart : 0;
                var ymin      = ystart > 0 ? ystart : 0;
                var xmax      = xend < Width ? xend : Width;
                var ymax      = yend < Height ? yend : Height;
                var newWidth  = xmax - xmin;
                var newHeight = ymax - ymin;
                for (int i = 0; i < newWidth; i++)
                {
                    for (int j = 0; j < newHeight; j++)
                    {
                        if (!(i == 0 || j == 0 || i == newWidth - 1 || j == newHeight - 1))
                        {
                            var medium = imageBitmap.GetPixel(i + xmin - 1, j + ymin - 1);
                            contoursBitmap.SetPixel(i, j, medium);
                        }
                    }
                }


                contoursBitmap = ImageFilter.PrewittFilter(contoursBitmap, false);
                var colorDictionary = new Dictionary <string, ColorInfo>();

                for (int i = 0; i < newWidth; i++)
                {
                    for (int j = 0; j < newHeight; j++)
                    {
                        if (!(i == 0 || j == 0 || i == newWidth - 1 || j == newHeight - 1))
                        {
                            var medium = contoursBitmap.GetPixel(i - 1, j - 1);
                            if (medium.ToArgb() != 0)
                            {
                                var colorValue = medium.ToArgb().ToString();

                                var near = ColorHelper.GetNearestColorName(ColorHelper.GetSystemDrawingColorFromHexString("#" + medium.Name.Substring(2)));
                                if (near != "Black")
                                {
                                    if (!colorDictionary.ContainsKey(colorValue))
                                    {
                                        colorDictionary.Add(colorValue, new ColorInfo {
                                            Color = medium, ColorCount = 1, NearColor = near
                                        });
                                    }
                                    else
                                    {
                                        colorDictionary[colorValue].ColorCount += 1;
                                    }
                                }
                            }
                        }
                    }
                }

                using (StreamWriter writer = new StreamWriter($@"{ProjectDir}\fragmentColors.txt"))
                {
                    foreach (KeyValuePair <string, ColorInfo> keyValue in colorDictionary.OrderByDescending(elem => elem.Value.ColorCount))
                    {
                        Color col  = Color.FromArgb(Convert.ToInt32(keyValue.Key));
                        var   near = ColorHelper.GetNearestColorName(ColorHelper.GetSystemDrawingColorFromHexString("#" + col.Name.Substring(2)));

                        writer.WriteLine($"{keyValue.Key} : {keyValue.Value.ColorCount} , near color = {keyValue.Value.NearColor}");
                        writer.WriteLine(near);
                    }
                }

                FilteredImageForm filteredImageForm = new FilteredImageForm();
                filteredImageForm.Image = contoursBitmap;
                filteredImageForm.Show();
            }
        }
예제 #5
0
        private Mat FindContoursAndDraw(Bitmap originalMap, string objectName, int minArea = 500, int maxArea = 10000)
        {
            //var houghBitmap = HoughTransform(originalMap);
            //var invertedHoughBitmap = InvertImage(houghBitmap);
            Mat originalMat = BitmapConverter.ToMat(originalMap);
            //Mat invertedHoughMat = BitmapConverter.ToMat(invertedHoughBitmap);
            Mat blackWhiteMat = new Mat();
            Mat edgesMat      = new Mat();

            Cv2.CvtColor(originalMat, blackWhiteMat, ColorConversionCodes.BGRA2GRAY);
            if (MapObjectsColors.GetInstance().Tight.Contains(objectName))
            {
                Bitmap edgesMap = BitmapConverter.ToBitmap(blackWhiteMat);
                edgesMap = ImageFilter.SobelFilter(edgesMap, grayscale: true);
                edgesMat = BitmapConverter.ToMat(edgesMap);
                Cv2.CvtColor(edgesMat, edgesMat, ColorConversionCodes.BGRA2GRAY);
            }
            else
            {
                Cv2.Canny(blackWhiteMat, edgesMat, 50, 100);
            }


            OpenCvSharp.Point[][] contours;
            HierarchyIndex[]      hierarchyIndexes;
            Cv2.FindContours(
                edgesMat,
                out contours,
                out hierarchyIndexes,
                mode: RetrievalModes.CComp,
                method: ContourApproximationModes.ApproxSimple);



            var componentCount = 0;
            var contourIndex   = 0;
            var objectDict     = mapObjects.getObjectDictionary();

            if (contours.Length != 0)
            {
                if (objectDict.ContainsKey(objectName))
                {
                    objectDict[objectName] = contours;
                }
                else
                {
                    objectDict.Add(objectName, contours);
                }
                while ((contourIndex >= 0))
                {
                    var contour          = contours[contourIndex];
                    var boundingRect     = Cv2.BoundingRect(contour);
                    var boundingRectArea = boundingRect.Width * boundingRect.Height;
                    var ca  = Cv2.ContourArea(contour) * Convert.ToDouble(scaleBox.SelectedItem) / 100;
                    var cal = Cv2.ArcLength(contour, closed: true) * Convert.ToDouble(scaleBox.SelectedItem) / 100;

                    //if (boundingRectArea > minArea)
                    //{

                    Cv2.PutText(originalMat, $"A:{ca.ToString("#.##")} km2", new OpenCvSharp.Point(boundingRect.X, boundingRect.Y + 10), HersheyFonts.HersheyPlain, 1, Scalar.White, 1);
                    Cv2.PutText(originalMat, $"L:{cal.ToString("#.##")} km", new OpenCvSharp.Point(boundingRect.X, boundingRect.Y + 25), HersheyFonts.HersheyPlain, 1, Scalar.White, 1);


                    //}


                    //Cv2.DrawContours(
                    //    originalMat,
                    //    contours,
                    //    contourIndex,
                    //    color: Scalar.All(componentCount + 1),
                    //    thickness: -1,
                    //    lineType: LineTypes.Link8,
                    //    hierarchy: hierarchyIndexes,
                    //    maxLevel: int.MaxValue);

                    componentCount++;


                    contourIndex = hierarchyIndexes[contourIndex].Next;
                }
            }

            return(originalMat);
        }