Python imutils 模块,is_cv2() 实例源码

我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用imutils.is_cv2()

项目:autonomous_driving    作者:StatueFungus    | 项目源码 | 文件源码
def draw_text(self, image, text, size, color, position):
        '''
            Zeichnet einen Text auf das Bild.

            Parameter
            ---------
            text : String
                Anzuzeigender Text
            size : Integer
                Groesse des Textes
            color : Tupel
                Farbe des Textes >> (255,0,0)
            position : Tupel
                Position des Textes >> (x,y)

        '''
        if imutils.is_cv2():
            cv2.putText(image, text, position, cv2.FONT_HERSHEY_COMPLEX, size, color, 2, cv2.CV_AA)
        elif imutils.is_cv3():
                cv2.putText(image, text, position, cv2.FONT_HERSHEY_COMPLEX, size, color, 2, cv2.LINE_AA)
项目:pokedex-as-it-should-be    作者:leotok    | 项目源码 | 文件源码
def extract_color_histogram(image, bins=(8, 8, 8)):
    # extract a 3D color histogram from the HSV color space using
    # the supplied number of `bins` per channel
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    hist = cv2.calcHist([hsv], [0, 1, 2], None, bins, [0, 180, 0, 256, 0, 256])

    # handle normalizing the histogram if we are using OpenCV 2.4.X
    if imutils.is_cv2():
        hist = cv2.normalize(hist)

    # otherwise, perform "in place" normalization in OpenCV 3 (I
    # personally hate the way this is done
    else:
        cv2.normalize(hist, hist)

    # return the flattened histogram as the feature vector
    return hist.flatten()
项目:vision-code    作者:FIRST-Team-1699    | 项目源码 | 文件源码
def run(self):
    bytes='' 
    while not self.thread_cancelled:
      try:
        bytes+=self.stream.raw.read(1024) 
        a = bytes.find('\xff\xd8')
        b = bytes.find('\xff\xd9')
        if a!=-1 and b!=-1:
          jpg = bytes[a:b+2]
          bytes= bytes[b+2:]
          img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)

          # Convert BGR to HSV
          hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
          # define range of blue color in HSV
          #lower_blue = np.array([self.L_RED, self.L_GREEN, self.L_BLUE], np.uint8)
          #upper_blue = np.array([self.U_RED, self.U_GREEN, self.L_BLUE], np.uint8)

          # Threshold the HSV image to get only blue colors
          mask = cv2.inRange(hsv, np.array([53,187,37]), np.array([97,244,153]))

          # Bitwise-AND mask and original image
          res = cv2.bitwise_and(img,img, mask= mask)
####        blurred = cv2.GaussianBlur(mask, (5, 5), 0)
          blurred = cv2.boxFilter(mask, 0, (7, 7), mask, (-1, -1), False, cv2.BORDER_DEFAULT)
          thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
          cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
          cnts = cnts[0] if imutils.is_cv2() else cnts[1]
          cv2.filterSpeckles(mask, 0, 100, 25)
##          cv2.filterSpeckles(mask, 0, 50, 25)
##          cv2.filterSpeckles(mask, 0, 100, 100)
          for c in cnts:
              M = cv2.moments(c)
              if int(M["m00"]) != 0:
                  cX = int(M["m10"] / M["m00"])
                  cY = int(M["m01"] / M["m00"])
              else:
                  (cX, cY) = (0, 0)
              print(cX, cY)
              cv2.drawContours(res, [c], -1, (0, 255, 0), 2)
              cv2.circle(res, (cX, cY), 7, (255, 255, 255), 1)

             # table.putNumber("center X", cX)
          cv2.imshow('img',img)
          cv2.imshow('mask',mask)
          cv2.imshow('Final',res)
          cv2.imshow('cam',img)
          #sd.putNumber('Center X', cX) ##send the x value of the center
          #sd.putNumber('Center Y', cY) ##send the y value of the center
##          print(sd.getNumber('Center Y'), sd.getNumber('Center X'))
          if cv2.waitKey(1) ==27:
            exit(0)
      except ThreadError:
        self.thread_cancelled = True
项目:vision-code    作者:FIRST-Team-1699    | 项目源码 | 文件源码
def run(self):
    bytes='' 
    while not self.thread_cancelled:  ####see lines 18, 80, 88 ....
      try:        
        bytes+=self.stream.raw.read(1024) ##limit max bytes read in 1 itteration? need to read more on this
        a = bytes.find('\xff\xd8')##find start of stream of data  
        b = bytes.find('\xff\xd9')##find our end of data stream
        if a!=-1 and b!=-1:  ##so as long as we have a stream of data....do the following
          jpg = bytes[a:b+2]  ##converts to image or a specific variable...
          bytes= bytes[b+2:]  
          img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)  ##decode the data

          # Convert BGR to HSV
          hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  ##converting color format for easier proccessing/ math
          # define range of blue color in HSV
          #lower_blue = np.array([self.L_RED, self.L_GREEN, self.L_BLUE], np.uint8)
          #upper_blue = np.array([self.U_RED, self.U_GREEN, self.L_BLUE], np.uint8)

          # Threshold the HSV image to get only blue colors
          mask = cv2.inRange(hsv, np.array([53,187,37]), np.array([97,244,153]))  ##get colors in the range of these HSV values

          # Bitwise-AND mask and original image
          res = cv2.bitwise_and(img,img, mask= mask)

          blurred = cv2.boxFilter(mask, 0, (7, 7), mask, (-1, -1), False, cv2.BORDER_DEFAULT) ##the next few line create outlines and 
          thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]                      ##remove any noise
          cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  #find countors
          cnts = cnts[0] if imutils.is_cv2() else cnts[1]   
          cv2.filterSpeckles(mask, 0, 100, 25)      ##remove speckles aka random dots and white noise
          for c in cnts:
              M = cv2.moments(c)
              if int(M["m00"]) != 0:  ##Checks for division by zero
                  cX = int(M["m10"] / M["m00"])
                  cY = int(M["m01"] / M["m00"])
              else:
                  (cX, cY) = (0, 0)
              cv2.drawContours(res, [c], -1, (0, 255, 0), 2)  ##draw box/highlighting 
              cv2.circle(res, (cX, cY), 7, (255, 255, 255), 1)  ##draw box/highlighting 

              ##Try-Catch for appending cX to table
              try:
                self.table.putNumber('centerX', cX)  ##Adds cX to the networktables
              except KeyError:
                print("centerX failed.")

          cv2.imshow('img',img)   ##display original image
          cv2.imshow('mask',mask)  ##display masked image
          cv2.imshow('Final',res)  ##show final image


          cv2.imshow('cam',img)  ##see line 71/comments
          if cv2.waitKey(1) ==27:  ##now we close if esc key is pressed
            exit(0)
      except ThreadError:
        self.thread_cancelled = True