Package pyforms :: Package Utils :: Module ImageBlobTools |
[hide private]
[frames] | no frames]

# Source Code for Module pyforms.Utils.ImageBlobTools

     1  #!/usr/bin/python
     2  # -*- coding: utf-8 -*-
     3
     4  __author__      = "Ricardo Ribeiro"
     5  __credits__     = ["Ricardo Ribeiro"]
     6  __license__     = "MIT"
     7  __version__     = "0.0"
     8  __maintainer__  = "Ricardo Ribeiro"
     9  __email__       = "ricardojvr@gmail.com"
    10  __status__      = "Development"
    11
    12
    13  from numpy import *
    14  import cv2, math
    15
    16 -def biggestContour(contours, howmany=1):
    17      biggest = []
    18      for blob in contours: area = cv2.contourArea(blob); biggest.append( (area, blob) )
    19      if len(biggest)==0: return None
    20      biggest = sorted( biggest, key=lambda x: -x[0])
    21      if howmany==1: return biggest[0][1]
    22      return [x[1] for x in biggest[:howmany] ]
    23
    24 -def getBiggestContour(image, howmany=1):
    25      (blobs, dummy) = cv2.findContours( image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE )
    26      return biggestContour(blobs, howmany)
    27
    28 -def cutBiggestContour(image, howmany=1):
    29      contour = getBiggestContour(image)
    30
    31      mask = zeros_like(image);cv2.drawContours(mask, array([contour]), -1, (255,255,255), -1 )
    32      tmp = cv2.bitwise_and( image, mask )
    33
    34      rect = cv2.boundingRect(contour)
    35      x, y, w, h = rect
    36      x, y, xx, yy = x,y,x+w,y+h
    37      return tmp[y:yy, x:xx]
    38