Diff of /CvImage.py [000000] .. [2c38ce]

Switch to unified view

a b/CvImage.py
1
import cv2
2
import imutils
3
import numpy as np
4
from math import sqrt
5
6
def abs (n):
7
  if n >= 0:
8
    return n
9
  return abs((-1 * n))
10
11
class CvImage:
12
  def __init__ (self, image, name):
13
    self.__name = name
14
    self.__originalImage = image
15
    self.__image = image
16
    self.__contours = False
17
    self.__contoursFeatures = False
18
  
19
  def getName (self):
20
    return self.__name
21
22
  def getOriginalImage (self):
23
    return self.__originalImage
24
25
  def getImage (self):
26
    return self.__image
27
28
  def drawCircle (self, center, radious = 1, color = (0, 255, 0), thickness = 2):
29
    cv2.circle(self.__image, center, radious, color, thickness)
30
31
  def drawContours (self, contours, color = (0, 255, 0), thickness = 2):
32
    cv2.drawContours(self.__image, contours, -1, color, thickness)
33
34
  def __show (self, name, image):
35
    cv2.imshow(name, image)
36
    cv2.waitKey(0)
37
38
  def showOriginal (self):
39
    self.__show(self.__name + "-original", self.getOriginalImage())
40
  
41
  def show (self):
42
    self.__show(self.__name, self.__image)
43
44
  def morphOperations (self, kernelSize = 3):
45
    # Open and close morph operations
46
    kernel = np.ones((kernelSize, kernelSize),np.uint8)
47
    self.__image = cv2.morphologyEx(self.__image, cv2.MORPH_OPEN, kernel)
48
    self.__image = cv2.morphologyEx(self.__image, cv2.MORPH_CLOSE, kernel)  
49
50
  def hsvFilter (self, lowerHSV, higherHSV):
51
    # Convert to HSV
52
    self.__image = cv2.cvtColor(self.__image, cv2.COLOR_BGR2HSV)
53
54
    # InRange filter
55
    self.__image = cv2.inRange(self.__image, lowerHSV, higherHSV)
56
    
57
58
  def getContours(self):
59
    if self.__contours == False:
60
      self.__contours = self.__findCountours()
61
    return self.__contours
62
63
  def getContoursFeatures(self, n = 5):
64
    if self.__contoursFeatures == False:
65
      self.__contoursFeatures = self.__findContoursFeatures(n)
66
    return self.__contoursFeatures
67
68
  def gray2bgr (self):
69
    self.__image = cv2.cvtColor(self.__image, cv2.COLOR_GRAY2BGR)
70
71
  def bgr2gray (self):
72
    self.__image = cv2.cvtColor(self.__image, cv2.COLOR_BGR2GRAY)
73
74
  def __findCountours(self):
75
    # ConvertToGrayScale and filter
76
    gray = cv2.bilateralFilter(self.__image, 11, 17, 17)
77
    
78
    # Edge algoritm
79
    edged = cv2.Canny(gray, 30, 200)
80
81
    cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
82
    cnts = imutils.grab_contours(cnts)
83
84
    # Sort by larger areas
85
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)
86
    return cnts
87
88
  def __findContoursFeatures (self, minArea):
89
    contoursFeatures = []
90
    cnts = self.getContours()
91
    for i in range(0, len(cnts)):
92
      if cv2.contourArea(cnts[i]) >= minArea:
93
        # Compute the center of the contour
94
        M = cv2.moments(cnts[i])
95
        cx = int(M["m10"] / M["m00"])
96
        cy = int(M["m01"] / M["m00"])
97
98
        # Get some basic features
99
        perimeter = cv2.arcLength(cnts[i], True)
100
        epsilon = 0.1*perimeter
101
        approx = cv2.approxPolyDP(cnts[i],epsilon,True)
102
103
        # Calculate eccentricity
104
        (x, y), (MA, ma), angle = cv2.fitEllipse(cnts[i])
105
        a = ma/2
106
        b = MA/2
107
        
108
        if (a > b):
109
          eccentricity = sqrt(pow(a, 2)-pow(b, 2))
110
          eccentricity = round(eccentricity/a, 2)
111
        else:
112
          eccentricity = sqrt(pow(b, 2)-pow(a, 2))
113
          eccentricity = round(eccentricity/b, 2)
114
115
        # Append features
116
        contoursFeatures.append({
117
          "area": cv2.contourArea(cnts[i]),
118
          "perimeter": perimeter,
119
          "eccentricity": eccentricity,
120
          "centroid": (cx, cy),
121
          "approxDp": np.squeeze(approx),
122
          "convexHull": np.squeeze(cv2.convexHull(cnts[i]))
123
        })
124
      else:
125
        break
126
    return contoursFeatures