Robot Localization
perspective Namespace Reference

perspective

More...

Classes

class  InteractivePlotN
 Plot where N reference points can be picked by mouse-click using mpl_connect. More...
 

Functions

def gray_conversion (img)
 Create 1-channel CV_8U image.
 
def rgb_conversion (img)
 Convert BGR (opencv) to RGB (matplotlib)
 
def get_centroid (cnt)
 Get centroid of contour.
 
def visualization (imgs, n_cam, colorbar=0, switch=0)
 Visualize results in matplotlib figures with title given and colorbar. More...
 
def get_parameters ()
 Get filename from command line.
 
def create_summary (img_flat, pts_obj, p=np.zeros(1))
 Create flat image with reference points and robot position drawn in it. More...
 
def save_open_images (outputpath, loopcounter='')
 Saves all open images in files named after the image titles, and the loop counter. More...
 
def get_circles_count (img, contours, t, w, r)
 Circles detection by counting pixels around contour centers. More...
 
def extract_color (img, range_min, range_max, r)
 Finds contours in binary image created by applying color filter on original image, only keeping contours with large enough radius (bigger than 1/20 times expected area of points of interest in image) More...
 
def manual_calibration (img, n, R)
 Get points of interest in image and create mask leaving only circles around points of interest. More...
 
def automatic_calibration (img, range_min, range_max, zvalue=2)
 Determines shade of red in the region around chosen point of interest by calculating the mean color within the given color range and its standard deviation. More...
 
def get_histograms (img, img_mask, n)
 Plots histograms of colors of image within a given mask. More...
 
def imagepoints_auto (img, r1, n, t, col_min, col_max, points, r2)
 Get positions of robot automatically (ignoring reference points) More...
 
def imagepoints (img, r, n, t, col_min, col_max, reduced=0)
 Get positions of reference points or robot in picture. More...
 
def objectpoints (m, name)
 reads objectpoint positions from file. More...
 
def geometric_transformationN (img, pts_obj, pts_img, size)
 Find Homography for 4 points from pts_obj to pts_img. More...
 
def restore_order (original, moved, min_dist)
 Reorganize points of interest in order of clicking. More...
 
def format_points (pts_obj, margin, mask='all')
  Reset the cooridnate system of object points such that all points are in positive range. More...
 

Detailed Description

perspective

Contains all functions related to image processing for marker detection, object point reading and image reprojection functions.

created by Frederike Duembgen, July 2015

Function Documentation

def perspective.automatic_calibration (   img,
  range_min,
  range_max,
  zvalue = 2 
)

Determines shade of red in the region around chosen point of interest by calculating the mean color within the given color range and its standard deviation.

Parameters:

img: Image to be worked on

range_min: nparray of minimum of color range

range_max: nparray of maximum of color range

zvalue: interval of normal color function to be considered for the color refinement. (default 2)

Returns:

rmin: nparray of new minimum color to be applied to points of interest

rmax: nparray of new maximum color to be applied to points of interest

colors_clean: nparray of all the colors present around points of interest with standard deviation less than coll_diff.

def perspective.create_summary (   img_flat,
  pts_obj,
  p = np.zeros(1) 
)

Create flat image with reference points and robot position drawn in it.

Parameters: img_flat Flat image received from geometric_transformationN. pts_obj nparray of reference point positions in object space p nparray of robot points to be drawn in image

Returns: img_summary Image of same size as img_flat, wwhere reference points and robot positions are indicated.

def perspective.extract_color (   img,
  range_min,
  range_max,
  r 
)

Finds contours in binary image created by applying color filter on original image, only keeping contours with large enough radius (bigger than 1/20 times expected area of points of interest in image)

Parameters:

img: Image to work on

range_min: minimum of color range

range_max: maximum of color range

r: Radius of reference points in pixels

Returns:

img: Image with obtained contours drawn on it, contour of maximum area pointed out

contours_big: list of centers of contours that are bigger than min_area calculated from r (min_area = 1/20 * r^2*pi) cx,cy: Center of contour of biggest area

img_diff: Binary image obtained after applying the color filter.

def perspective.format_points (   pts_obj,
  margin,
  mask = 'all' 
)

 Reset the cooridnate system of object points such that all points are in positive range.

Parameters:

pts_obj: np.array of object points.

margin: Margin to add to leftmost and downmost points in final image.

mask: np.array of indicator for all reference points to be taken into consideration (1 or 0)

Returns:

img_test: Resulting image with object points drawn in it.

pts_obj: object points in new reference frame.

(w,h): Size or resulting image.

def perspective.geometric_transformationN (   img,
  pts_obj,
  pts_img,
  size 
)

Find Homography for 4 points from pts_obj to pts_img.

Parameters:

img: Image to work on

pts_obj: Object space positions of reference points

pts_img: Image space positions of reference points

size: Image size of resulting transformed image

Returns:

img_flat: "flat image" to which Homography has been applied.

M: npmatrix of homography matrix.

def perspective.get_circles_count (   img,
  contours,
  t,
  w,
  r 
)

Circles detection by counting pixels around contour centers.

Tests weather the contours obtained by extract_color() are indeed of circular shape by checking that in a square of given radius r, the average color is much brighter than in a square of given width w, surrounding the square. The average colors around the circle and inside the circle need to be separable by a color threshold t.

Parameters:

img: Image to work on

contours: centers of contours found by extract_color()

t: color threshold

w: width of test squares

r: radius of test circles

Returns:

cimg: Original image with drawn centers

centers: nparray of obtained centers.

def perspective.get_histograms (   img,
  img_mask,
  n 
)

Plots histograms of colors of image within a given mask.

Parameters:

img: Image to be worked on

img_mask: Defines regions of interest

n: Number of figure to be used for histograms.

Returns:

hist_h: Histogram of h-component of image in mask (nparray)

hist_s: Histogram of s-component of image in mask (nparray)

def perspective.imagepoints (   img,
  r,
  n,
  t,
  col_min,
  col_max,
  reduced = 0 
)

Get positions of reference points or robot in picture.

Parameters:

img: Image to work on

r: Radius of points of interest (in px)

n: Number of points to be detected

col_min: np.array of minimum of color range in HSV

col_max: np.array of maximum of color range in HSV

reduced: if set to 1, the whole picture will be taken into account. if set to 0, the user may choose regions of interest. (default 0)

Returns:

img_color: image showing region of interest and extracted circles

circ_color: image showing extracted color and detected circles

pos_color: np.array with positions of the extracted points

th: image of extracted color.

def perspective.imagepoints_auto (   img,
  r1,
  n,
  t,
  col_min,
  col_max,
  points,
  r2 
)

Get positions of robot automatically (ignoring reference points)

Parameters:

img,r1,n,t,col_min,col_max: Parameters used by imagepoints() for detecting the robot position (see imagepoints() for more details)

points: nparray of reference point locations r2: radius of reference points (in pxls) Returns:

output of imagepoints, but without manually selecting the robot's position. Instead, only color filtering is applied to the image, excluding the region of radius r around the referencepoints.

def perspective.manual_calibration (   img,
  n,
  R 
)

Get points of interest in image and create mask leaving only circles around points of interest.

Parameters:

img: Image to work on

n: Number of points of interest

R: Radius of circlearound points to be kept in img_mask

Returns:

img_mask: Image where all pixels but the ones in a circle around the chosen points of interest are white.

points: List of chosen points (x,y), used later by restore_order() to restore the order of clicking.

def perspective.objectpoints (   m,
  name 
)

reads objectpoint positions from file.

Parameters:

m: Number of points

name: File name of .csv or text sheet where objectpoints positions are stored (in form of euclidean distance matrix)

Returns:

pts_obj:Object positions of reference points (in cm)

M1 marker_calibration.MarkerSet Object.

def perspective.restore_order (   original,
  moved,
  min_dist 
)

Reorganize points of interest in order of clicking.

Parameters:

original: list of points of interest in order of clicking (returned by manual_calibration)

moved: list of refined reference point positions, unordered.

min_dist: minimum distance between original and moved position for points to be considered as corresponding. Returns:

pts: nparray of refined reference point positions, ordered in the order of clicking.

def perspective.save_open_images (   outputpath,
  loopcounter = '' 
)

Saves all open images in files named after the image titles, and the loop counter.

Parameters:

outputpath: path to folder where file is saved

loopcounter: integer, counter of robot steps (used for naming the images

Returns: Nothing

def perspective.visualization (   imgs,
  n_cam,
  colorbar = 0,
  switch = 0 
)

Visualize results in matplotlib figures with title given and colorbar.

Parameters:

imgs: image dictionary with keys: image titles (string) and values: corresponding image (np.array)

[colorbar]: if set to 1, a colorbar is shown in figure (default 0)

[swtich]: if set to 1, the picture is shown from bottom to top. (default 0)

Returns: None