GithubHelp home page GithubHelp logo

rchavezj / opencv_projects Goto Github PK

View Code? Open in Web Editor NEW
338.0 11.0 143.0 474.42 MB

List of OpenCV projects to further increase the computer vision community. Coding in Python & C++(In progress).

Python 0.32% Jupyter Notebook 99.68%

opencv_projects's Issues

Automatically Cropping image using OpenCV

Hi,

We are performing Auto Crop operations using OpenCV library in C# project, For few sample automatically cropping the image but for other samples It was not cropping properly.

Let us know anything is missing in source code.

Please find the below piece of code.

        `Mat image = new Mat();
        Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
        Nullable<bool> result = openFileDlg.ShowDialog();
        if (result == true)
        {
            image = Cv2.ImRead(openFileDlg.FileName);
        }
        
        Mat GrayImage = new Mat();
        Cv2.CvtColor(image, GrayImage, ColorConversionCodes.BGR2GRAY);

        Mat BlurImage = new Mat();
        Cv2.GaussianBlur(GrayImage,BlurImage, new OpenCvSharp.Size(5,5),0);

        Mat CannyImage = new Mat();
        OpenCvSharp.Point[][] contours;
        HierarchyIndex[] hierarchy;
        Cv2.Canny(BlurImage, CannyImage, 80, 150,7);

        Cv2.ImShow("Original Image", image);
        Cv2.ImShow("Canny Edged Image", CannyImage);
        Cv2.DestroyAllWindows();
       
        Cv2.FindContours(CannyImage, out contours, out hierarchy,RetrievalModes.External, ContourApproximationModes.ApproxSimple);

        image.SaveImage(@"C:\Users\TestAccount\Documents\AutoCrop_OpenCV\Output03012022\20220103.jpeg");

        var contourIndex = 0;
        var previousArea = 0;
        var biggestContourRect = Cv2.BoundingRect(contours[0]);
        while ((contourIndex >= 0))
        {
            var contour = contours[contourIndex];

            var boundingRect = Cv2.BoundingRect(contour); //Find bounding rect for each contour
            var boundingRectArea = boundingRect.Width * boundingRect.Height;
            if (boundingRectArea > previousArea)
            {
                biggestContourRect = boundingRect;
                previousArea = boundingRectArea;
            }
            
            contourIndex = hierarchy[contourIndex].Next;
        }
        

        var finalImage = new Mat(image, biggestContourRect); //Crop the image
        string outputBaseDirectory = @"C:\Users\TestAccount\Documents\AutoCrop_OpenCV\Output03012022";
        finalImage.SaveImage(string.Format("{0}{1}{2}", outputBaseDirectory, "\\Output", System.IO.Path.GetFileName(openFileDlg.FileName)));
        Cv2.ImShow("Final Image", finalImage);`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.