Giter Site home page Giter Site logo

curvetopia's Introduction

Polygon Simplification and Regularization

This project focuses on the simplification and regularization of hand-drawn shapes, particularly polygons, by processing the input paths and reducing complex paths into simpler geometric forms. The goal is to retain the original shape's orientation and approximate vertex positions while transforming it into a more regular form.

Features

  • Path Extraction: Extracts paths from CSV files where each path is represented as a series of (x, y) coordinates.
  • Line Simplification: Simplifies hand-drawn lines into straight segments while preserving the original form when the total deviation is minimal.
  • Polygon Regularization: Converts hand-drawn polygons into regular polygons by adjusting vertex positions while retaining the overall orientation and relative placement of vertices.
  • Visualization: Compares original and processed paths using Matplotlib, providing clear before-and-after views of the shape transformations.

Usage

Preprocess input

  1. Convert PNG to SVG using ImageMagick.
  2. Extract polylines from SVG using Python's svgpathtools library or by manually parsing the SVG file.

Convert PNG to SVG

Using ImageMagick:

  1. Install ImageMagick:

    • You can install it via Homebrew by running:
      brew install imagemagick
  2. Convert PNG to SVG:

    • Use the following command to convert:
      magick input.png output.svg

Extract Polylines from SVG

Once you have the SVG, you can extract the polylines using Python with the svgpathtools library or manually parse the SVG.

Using svgpathtools (Python):

  1. Install svgpathtools:
    pip3 install svgpathtools
  2. Navigate to generate_polylines folder:
    python3 svg2pl.py

1. Path Extraction

Paths are extracted from a CSV file containing x and y coordinates:

2. Line Simplification

The isLine function reduces a series of points to a straight line if the deviation from linearity is below a threshold.

3. Circle Detection and Contour Approximation Using OpenCV

This project leverages OpenCV's powerful image processing capabilities to detect and approximate shapes within an image. The focus is on identifying contours and fitting geometric primitives, particularly circles, to these contours.

Concepts:

  • Contour Detection: Contours are curves joining all continuous points along a boundary with the same color or intensity. This is a fundamental concept in image analysis, used for shape detection, object recognition, and more.

  • Gaussian Blur: Applied to smooth the image and reduce noise, Gaussian blur is crucial for improving the accuracy of edge detection algorithms.

  • Canny Edge Detection: A multi-stage algorithm used to detect a wide range of edges in images. It is based on finding the intensity gradients of the image, leading to more defined and detectable edges.

  • Min Enclosing Circle: This is a mathematical approach to find the smallest circle that can completely enclose a given set of points. In the context of contours, it helps approximate the contour with the most suitable circle.

Code Overview:

  • Grayscale Conversion & Blurring: The image is converted to grayscale and smoothed using Gaussian blur to enhance edge detection.

  • Edge Detection: Canny edge detection is used to find the contours of the shapes within the image.

  • Contour Approximation: Among the detected contours, the largest one is selected, and the minimal enclosing circle is calculated to fit around it.

  • Visualization: The original image is then annotated with the detected contour and the best-fit circle, illustrating the approximation. image

Mathematical Foundation:

  • Contours: Defined by level sets of the image intensity function, contours are critical in understanding the shape and geometry of objects within an image.
  • Circle Fitting: Using principles from computational geometry, the minimal enclosing circle is a fundamental geometric primitive, providing the best-fit approximation for closed contours.

4. Polygon Regularlization

This Python script utilizes OpenCV to identify and adjust the vertices of a polygonal shape in an image, making it a regular polygon while retaining the original vertex positions as close as possible. The process involves calculating the centroid, determining the radius, and adjusting vertex positions to form a regular polygon.

image

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.