Module: processfield

Processfield is the image analysis workhorse module behind detecttrails. It contains all the functionality required to detect lines independend of the source of data, only the ingoing format and type.

Wrapping the functionality in this module for various different catalog and image sources so that the correct order of operations is ensured for different directory structures is what makes detecttrails capable of processing variety of different images.

lfd.detecttrails.processfield.process_field_bright(img, lwTresh, thetaTresh, dilateKernel, contoursMode, contoursMethod, minAreaRectMinLen, houghMethod, nlinesInSet, lineSetTresh, dro, debug)[source]

Function detects bright trails in images. For parameters explanations see DetectTrails documentation for help.

  1. pixels with values bellow minFlux are set to 0
  2. Scale the image to 8 bit integer, 1 channel
  3. histogram equalization
  4. dilate to expand features
  5. fit minimal area rectangles. Function aborts if no minAreaRect are found, see: fit_minAreaRect help
  6. fit Hough lines on image and on found rectangles
  7. compare lines, see: check_theta help
  8. if evaluation passed write the results into file and return True, else returns False.
Parameters:
  • img (np.array) – numpy array representing gray 32 bit 1 chanel image.
  • lwTresh (float) – treshold for the ratio of rectangle side lengths that has to be satistfied
  • thetatresh (float) – treshold for the difference of angles, in radians, that each fitted set of lines has to satisfy
  • dilateKernel (np.array) – kernel used to dilate the image
  • contoursMode (cv2.const) – cv2.RETR_LIST should be used, but any return type of fitted contours supported by OpenCV is allowed
  • contoursMethod (cv2.const) – cv2.CHAIN_APPROX_NONE should be used, but any return type of fitted contours supported by OpenCV is allowed
  • minAreaRectMinLen (int) – treshold for minimal allowed length of fitted minimal area rectangles
  • houghMethod (int) – treshold for minimum number of votes lines need to get in Hough space to be returned
  • nlinesInSet (int) – number of most voted for lines that will be considered for colinearity tests
  • lineSetTresh (float) – treshold for maximal allowed angle deviation between two sets of fitted lines
  • dro (int) – treshold for maximal allowed distance between the average x-axis intersection coordinates of the two sets of lines
lfd.detecttrails.processfield.process_field_dim(img, minFlux, addFlux, lwTresh, thetaTresh, erodeKernel, dilateKernel, contoursMode, contoursMethod, minAreaRectMinLen, houghMethod, nlinesInSet, dro, lineSetTresh, debug)[source]

Function detects dim trails in images. See DetectTrails documentation for more detailed explanation of parameters

  1. pixels with values bellow minFlux are set to 0
  2. addFlux is added to remaining pixels
  3. Scale the image to 8 bit 1 chanel
  4. histogram equalization
  5. erode to kill noise
  6. dilate to expand features that survived
  7. fit minimal area rectangles. Function aborts if no minAreaRect are found, see: fit_minAreaRect help
  8. fit Hough lines on image and on found rectangles
  9. compare lines, see: help(check_theta)
  10. if evaluation passed write the results into file and return True, else returns False.
Parameters:
  • img (np.array) – numpy array representing gray 32 bit 1 chanel image.
  • minFlux (float) – treshold for maximal allowed pixel brightness value under which pixel values will be set to zero
  • addFlux (float) – the brightness that will be added to all pixels above minFlux
  • lwTresh (float) – treshold for the ratio of rectangle side lengths that has to be satistfied
  • thetatresh (float) – treshold for the difference of angles, in radians, that each fitted set of lines has to satisfy
  • dilateKernel (np.array) – kernel used to erode the image
  • dilateKernel – kernel used to dilate the image
  • contoursMode (cv2.const) – cv2.RETR_LIST should be used, but any return type of fitted contours supported by OpenCV is allowed
  • contoursMethod (cv2.const) – cv2.CHAIN_APPROX_NONE should be used, but any return type of fitted contours supported by OpenCV is allowed
  • minAreaRectMinLen (int) – treshold for minimal allowed length of fitted minimal area rectangles
  • houghMethod (int) – treshold for minimum number of votes lines need to get in Hough space to be returned
  • nlinesInSet (int) – number of most voted for lines that will be considered for colinearity tests
  • lineSetTresh (float) – treshold for maximal allowed angle deviation between two sets of fitted lines
  • dro (int) – treshold for maximal allowed distance between the average x-axis intersection coordinates of the two sets of lines
lfd.detecttrails.processfield.setup_debug()[source]

Sets up the module global variables - paths to where the debug output is saved. Invoked when ‘debug’ key is set to True for any of the detection steps. Generally there would be no need to call this function otherwise.

lfd.detecttrails.processfield.check_theta(hough1, hough2, navg, dro, thetaTresh, lineSetTresh, debug)[source]

Comapres colinearity between lines in each set of lines provided and between the two sets. If the lines in each set are nearly parallel and the two sets are nearly parallel and if the lines are intersecting the x axis at nearly the same point then they are nearly colinear.

Warning

Function returns True when a test is satisfied - this means that the lines are not colinear.

Calculates the difference between max and min angle values of each set of fitted lines. If these diffs. are larger than thetaTresh, returns True.

dtheta = abs(theta.max()-theta.min())
if  dtheta2> theta_tresh:  return True

Difference of averages of angles in both sets are compared with linesetTresh. If the diff. is larger than linesetTresh a True is returned.

dtheta = abs(numpy.average(theta1-theta2))
if numpy.average(dtheta)> lineset_tresh: return True

If the average x axis intersection of the two line sets are not close enough True is returned

if abs(np.average(ro1)-np.average(ro2))>dro: return True
Parameters:
  • hough1 (cv2.HoughLines) – 2D array of line parameters representing the first set of lines that will be compared. Line parameters are stored as tuples, i.e. hough1[0][i] –> tuple(ro, theta)
  • hough2 (cv2.HoughLines) – second set of lines to be compared
  • navg (int) – number of lines that will be averaged together
  • thetaTresh (float) – treshold, in radians, that each line set must not be bigger than
  • linesetTresh (float) – treshold, in radians, that the difference of the two line sets should not be bigger than.
  • debug (bool) – produces a verboose output of calculated values.
lfd.detecttrails.processfield.draw_lines(hough, image, nlines, name, path=None, compression=0, color=(255, 0, 0))[source]

Draws hough lines on a given image and saves it as a png.

Parameters:
  • hough (cv2.HoughLines) – 2D array of line parameters, line parameters are stored in [0][x] as tuples.
  • image (np.array or cv2.image) – image will now be altered
  • nlines (int) – number of lines to draw
  • name (str) – name of the file without extension
  • path (str) – the path will be set by default when DetectTrails debug params are set to True. Otherwise supply your own.
  • compression (int) – cv2.IMWRITE_PNG_COMPRESSION parameter from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 0.
lfd.detecttrails.processfield.fit_minAreaRect(img, contoursMode, contoursMethod, minAreaRectMinLen, lwTresh, debug)[source]

Fits minimal area rectangles to the image. If no rectangles can be fitted it returns False. Otherwise returns True and an image with drawn rectangles.

  1. finds edges using canny edge detection algorithm

  2. finds all contours among the edges

  3. fits a min. area rectangle to a contour only if:

    1. both sides of the rect. are longer than minAreaRectMinLen
    2. the ratio of longer vs, shorter rect. side is smaller than lwTresh

5. if no rectangles satisfying sent conditions are found function returns False and an empty (black) image

For more details on the parameters see documentation.

Parameters:
  • img (np.array) – numpy array representing gray 8 bit 1 chanel image.
  • lwTresh (float) – ratio of longer/shorter rectangle side that needs to be satisfied
  • contoursMode (cv2.const) – cv2.RETR_LIST should be used, but any of the contour return modes
  • contoursMethod (cv2.const) – cv2.CHAIN_APPROX_NONE should be used, but any of the contour return modes supported by OpenCV can be used
  • minAreaRectMinLen (int) – length, in pixels, of allowed shortest side to be fitted to contours
lfd.detecttrails.processfield.dictify_hough(shape, houghVals)[source]

Function converts from hough line tuples (rho, theta) into a dictionary of pixel coordinates on the image.

Parameters:
  • shape (tuple, list) – shape (dimensions) of the image. Used in order to scale Hough-space coordinates into pixel-space coordinates
  • houghVals (tuple, list) – (rho, theta) values of lines