Module AmpliVision.src.objs.image.detectors

Sub-modules

AmpliVision.src.objs.image.detectors.contour_finder
AmpliVision.src.objs.image.detectors.corner_detector

Classes

class ContourFinder

ContourFinder

This class is responsible for finding contours of the grid in an image.

Methods

  • find_contours(image: np.ndarray) -> list
    • This method finds the contours of the grid in the given image and returns the top 5 contours sorted by area.

Example

import cv2 as cv
from src.objs.image.detectors.contour_finder import ContourFinder

image = cv.imread('path/to/image.jpg')
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (5, 5), 0)
edged = cv.Canny(blurred, 50, 150)
contours = ContourFinder.find_contours(edged)

reference

https://learnopencv.com/automatic-document-scanner-using-opencv/

Expand source code
class ContourFinder:
    """
    ## ContourFinder
    This class is responsible for finding contours of the grid in an image.

    ### Methods
    - `find_contours(image: np.ndarray) -> list`
        - This method finds the contours of the grid in the given image and returns the top 5 contours sorted by area.

    ### Example
    ```python
    import cv2 as cv
    from src.objs.image.detectors.contour_finder import ContourFinder

    image = cv.imread('path/to/image.jpg')
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    blurred = cv.GaussianBlur(gray, (5, 5), 0)
    edged = cv.Canny(blurred, 50, 150)
    contours = ContourFinder.find_contours(edged)
    ```

    ## reference
    https://learnopencv.com/automatic-document-scanner-using-opencv/
    """
    @staticmethod
    def find_contours(image: np.ndarray) -> list:
        """ This method finds the contours of the Grid in the given image and returns the top 5 contours sorted by area."""

        image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        blurred = cv.GaussianBlur(image, (11, 11), 0)

        # EDGE DETECTION
        canny = cv.Canny(blurred, 0, 200)
        canny = cv.dilate(canny, cv.getStructuringElement(
            cv.MORPH_ELLIPSE, (5, 5)))

        # CONTOUR DETECTION
        contours, _ = cv.findContours(
            canny, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
        return sorted(contours, key=cv.contourArea, reverse=True)[:5]

Static methods

def find_contours(image: numpy.ndarray) ‑> list

This method finds the contours of the Grid in the given image and returns the top 5 contours sorted by area.

class CornerDetector

CornerDetector

The CornerDetector class is responsible for detecting the corners of a given image given the contours.

Methods

  • detect_corners(contours: list, img: np.ndarray) -> list
    • This method detects the corners of a given image given the contours and returns the corners.

References

https://learnopencv.com/automatic-document-scanner-using-opencv/

Expand source code
class CornerDetector:
    """
    ## CornerDetector
    
    The `CornerDetector` class is responsible for detecting the corners of a given image given the contours.

    ### Methods
    - `detect_corners(contours: list, img: np.ndarray) -> list`
        - This method detects the corners of a given image given the contours and returns the corners.

    ## References
    https://learnopencv.com/automatic-document-scanner-using-opencv/
    """
    @staticmethod
    def detect_corners(contours: list, img:np.ndarray)->list:
        """
        This method detects the corners of a given image given the contours and returns the corners.
        Currently being used for the grid detection.
        """

        # Loop over the contours.
        for c in contours:
            # Approximate the contour.
            epsilon = 0.02 * cv.arcLength(c, True)
            corners = cv.approxPolyDP(c, epsilon, True)
            # If our approximated contour has four points
            if len(corners) == 4:
                break
    
        # Sorting the corners and converting them to desired shape.
        corners = sorted(np.concatenate(corners).tolist())

        return CornerDetector.order_points(corners)
    
    @staticmethod
    def order_points(pts: list)->list:
        # Initialising a list of coordinates that will be ordered.
        rect = np.zeros((4, 2), dtype='float32')
        pts = np.array(pts)
        s = pts.sum(axis=1)

        # Top-left point will have the smallest sum.
        rect[0] = pts[np.argmin(s)]

        # Bottom-right point will have the largest sum.
        rect[2] = pts[np.argmax(s)]

        # Computing the difference between the points.
        diff = np.diff(pts, axis=1)

        # Top-right point will have the smallest difference.
        rect[1] = pts[np.argmin(diff)]
        
        # Bottom-left will have the largest difference.
        rect[3] = pts[np.argmax(diff)]
        
        # Return the ordered coordinates.
        return rect.astype('int').tolist()

Static methods

def detect_corners(contours: list, img: numpy.ndarray) ‑> list

This method detects the corners of a given image given the contours and returns the corners. Currently being used for the grid detection.

def order_points(pts: list) ‑> list