ImageCropper package

Submodules

ImageCropper.AutoDetect module

This module is responsible for detecting a calculator display from a camera feed.

class ImageCropper.AutoDetect.AutoDetector

Bases: object

This class detects a calculator display from a camera feed automatically.

The user only supplies the class with an image and the class detects a frame. For proper operation it is recommended to have a clear view of the display with a blank background.

static detect(image)

Detects a frame from an image

The detect method performs the following actions to detect a display from an image.

  • pyrMeanShiftFiltering - This filters the image so that it can be
    segmented.
  • cvtColor YCrCb and HLD - This converts the filtered image to YCbCr
    and HLS color spaces.
  • split - Splits the color channels.
  • equalizeHist - Normalizes the brightness and increases the contrast
    of the split images.
  • merge - Merges the split images and inverts them.
  • Threshold - Thresholds the image.
  • morphologyEx - Opens the image (erosion followed by dilation)
  • findContours - Finds contours in a binary image.
  • Box - The found contours are used to determine the rectangle
    coordinates for cropping.

Coordinates are returned in the following form: [[(rec1_x1, rec1+y1),(rec1_x2, rec1+y2),(rec1_x3, rec1+y3),(rec1_x4, rec1+y4)], [(rec2_x1, rec2+y1),(rec2_x2, rec2+y2),(rec2_x3, rec2+y3),(rec2_x4, rec2+y4)], [etc…]]

Parameters:- RGB image in which a display can be detected. (image) –
Returns:Array of coordinates describing rectangles.

..todo:: Make sure that the found contour is not outside the image. This can cause errors in the cropping (which are now worked around).

ImageCropper.Crop module

This module is responsible for cropping an image based on coordinates.

class ImageCropper.Crop.Cropper

Bases: object

This class allows users to crop an image.

The user supplies the class with an image and coordinates and the class will return a cropped image.

static crop_image(image, cords)

Crops the image based on supplied coordinates.

This method extracts the full image from the coordinates and the uses the coordinates to calculate the angle, determine the orientation and warp the image accordingly.

It does this through the following actions:

  • It extracts crops the image to include every coordinate.
  • It determines the bottom side of the display.
  • It calculates the angle based on the orientation of the rectangle.
  • It rotates the rectangle using warpAffine.

Coordinates are given in the following form:

Parameters:
  • - RGB image as source to cut display out of (image) –
  • - Array of coordinates describing rectangles (cords) –
Returns:

RGB image of the display only (cropped out of the original image and rotated for a horizontal image) If the cords are outside the image, a string (‘failed to crop’) is returned.

ImageCropper.FixedDetect module

This module is responsible for providing the crop module with coordinates based on user-input regarding the location of a calculator display.

class ImageCropper.FixedDetect.FixedDetector

Bases: object

This class provide coordinates for the cropper based on an image and the user-provided location info.

static get_cords(image, x, y, w, h, a)

Converts user location input into coordinates for the crop module.

The user provides the location, size and angle of the display within the image.

The rectangle is constructed using basic trigonometry. The rectangle is then used to construct the coordinates for the crop module.

This is accomplished by drawing 4 lines based on the coordinates and the angle on a white copy of the image and then using findContours to get the coordinates.

Coordinates are returned in the following form: [[(rec1_x1, rec1+y1),(rec1_x2, rec1+y2),(rec1_x3, rec1+y3),(rec1_x4, rec1+y4)], [(rec2_x1, rec2+y1),(rec2_x2, rec2+y2),(rec2_x3, rec2+y3),(rec2_x4, rec2+y4)], [etc…]]

Parameters:
  • - RGB image in which a display can be detected. (image) –
  • - Location of display on x-axis in pixels. (x) –
  • - Location of display on y-axis in pixels. (y) –
  • - Width of display in pixels. (w) –
  • - Height of display in pixels. (h) –
  • - Angle of display in degrees. (a) –
Returns:

Array of coordinates describing rectangles.

ImageCropper.ManualDetect module

The ManualDetect module allows the user to select a region of interest to crop out of an image.

class ImageCropper.ManualDetect.ManualDetector

Bases: object

This class lets the user select three points on the supplied image in order to get the four coordinates needed to make a rectangle.

click_and_detect(event, x, y, *flags, **param)

Listens for mouse click events.

Records the (x,y) coordinates when the left mouse button is clicked. Lines are drawn between selected points.

Args:
event - The keyboard or mouse event that took place. x - The x coordinate of said event. y - The y coordinate of said event. flags - Relevant flags that are passed by OpenCV. param - Extra parameters supplied by OpenCV.
manual_detect()

Makes an array of the coordinates that come from the click_and_detect function.

manual_detect_setup(img)

Uses the click_and_crop function to crop an image.

The ‘r’ and ‘c’ keys reset and crop the image respectively. Furthermore, the cropped image is shown in a separate window.

Args:
img - RGB image in which a display can be detected.
Returns:
Array of coordinates describing rectangles.
static init_manual_detect(img)

Initializes the manual detector.

Parameters:- RGB image in which a display can be detected. (img) –
Returns:Array of coordinates describing rectangles.

Module contents