ocvu

ocvu.imshow(window_name, image, border_color=(0, 0, 0))[source]

Improves default OpenCV imshow method by adding the possibility to correctly visualize small images without them being stretched. This is accomplished by adding a border around the image.

class ocvu.Video(filepath, grayscale=False)[source]

Bases: object

OpenCV Video.

fourcc

4-character code of codec.

fps

Frames per second.

frame_number

Number of the frame that will be read next.

frames

Returns an iterator with all frames.

generate_background_model(step=None, end=None, mse_min=50)[source]

Generates a background model using the median.

Only sufficiently different frames are considered, using the mean squared error method.

Parameters:
  • step – Step to iterate through the video. Default is video FPS rate.
  • end – Last frame to consider. Default is 2/3 of video length.
  • mse_min – The minimum error at wich the frame is selected. The lower the error, the more similar the two images are.
nframes

Returns the total number of frames.

play(begin=None, end=None, step=1, window=None, wait_time=None)[source]
read_frame(number=None, grayscale=False)[source]

Reads the current frame and returns it. You can also ask for a specific frame. Returns a Frame object.

Parameters:
  • number (int) – Number of the frame desired. If None, reads the current one.
  • grayscale (bool) – Convert the frame read to grayscale.
show_frame(number=None, window=None, resize=False)[source]

Shows frame number in an OpenCV window. Returns the frame read.

size

Returns the size of the video frames: (width, height).

class ocvu.Contour(contour)[source]

Bases: object

Provides a user-friendly object defining a contour in OpenCV. Adapted from: http://answers.opencv.org/question/6043/segmentation-and-contours/

approx

Lorem

area

Contour.area - Area bounded by the contour region

bounding_box

Lorem

centroid
diameter

EquivDiameter: diameter of circle with same area as region

draw(image, **kwargs)[source]

Draw contour. kwargs are passed to cv2.rectangle method. Default color is green.

draw_approx(image, **kwargs)[source]

Draw approximated contour. kwargs are passed to cv2.rectangle method. Default color is red.

draw_bounding_box(image, **kwargs)[source]

Draw contour’s bounding box on image. kwargs are passed to cv2.rectangle method. Default color is white.

draw_centroid(image, radius=3, **kwargs)[source]

Draw contour’s centroid. kwargs are passed to cv2.circle method.

draw_ellipse(image, **kwargs)[source]

Draw contour’s ellipse. kwargs are passed to cv2.circle method.

draw_hull(image, **kwargs)[source]

Draw convex hull. kwargs are passed to cv2.rectangle method. Default color is blue.

ellipse

Fits an ellipse and returns the rotated rectangle in which the ellipse is inscribed.

The returned value is the tuple: ((x, y), (major_axis, minor_axis), angle)

hull

Lorem

moments

Lorem

perimeter

Lorem

position

Top left coordinate of the contour bounding box.

ocvu.find_biggest_contours(mask, n=1, area_min=None, **kwargs)[source]

Returns the n biggest contours in mask. Returns a list of contours sorted by area in descending order. If you provide a minimal area value, contours returned will be filtered like so.